vuoi
o PayPal
tutte le volte che vuoi
Calcolo Numerico - Elaborato 2
Esempi d'uso:
% GAUSS con determinante
>> [x,d]=gauss(rand(5,5),rand(1,5))
x =-0.79400921648986 -0.21185732449647 0.03698826581038 2.48223534857465 -0.32381167895828
d =0.04854891685187
% GAUSS senza determinante
>> x=gauss(rand(5,5),rand(1,5))
x =-0.97264425632274 -0.72129250503284 1.86803314120408 0.34216944189633 0.05605382576106
Situazioni di errore:
>> [x,d]=gauss(rand(5,5),rand(1,5),rand(1,5))
??? Error using ==> gaussToo many input arguments.
>> [x,y,d]=gauss(rand(5,5),rand(1,5))
??? Error using ==> gaussToo many output arguments.
>> [x,d]=gauss
??? Error using ==> espoNessun input specificato.
>> [x,d]=gauss(rand(4,5),rand(1,5))
??? Error using ==> gaussMatrice non quadrata.
>> [x,d]=gauss(rand(5,5),rand(1,4))
??? Error using ==> gaussUtilizzare matrici NxN e vettori 1xN.
>> [x,d]=gauss(rand(5,5),rand(2,5))
??? Error using ==> gaussVettore pluridimensionale.
>> [x,d]=gauss(ones(5,5),rand(1,5))
??? Error using ==> gauss
Error using ==> gaussSistema singolare.
berragazzo 2/5Calcolo Numerico - Elaborato 2
Test dei casi funzionanti:
% GAUSS con determinante
>> [x,d]=gauss(rand(5,5),rand(1,5))
x =
0.30787703065830 -0.64159754351901 -0.08666380789320 0.48375517501043 0.62635117908815
d =
-0.05116330414697
% GAUSS senza determinante
>> x=gauss(rand(5,5),rand(1,5))
x =
0.81718944077268 10.03235448475336 -14.44552798303867 -6.27593098132794 3.48161963104765
Confronto con il Matlab:
>> A=rand(7,7);
>> b=rand(1,7);
>> [x,d]=gauss(A,b)
x =
Columns 1 through 5
0.32678601543795 0.29645596425731 0.21483592145287 -0.77679523070997 0.97237122889521
Columns 6 through 7
-0.53464082251967 0.04955257812158
d =
-0.12901949540796
>> b=b';
>> (A\b)'
ans =
Columns 1 through 5
0.32678601543795 0.29645596425731 0.21483592145287 -0.77679523070997 0.97237122889521
Columns 6 through 7
-0.53464082251967 0.04955257812158
>> det(A)
ans =
-0.12901949540796
berragazzo 3/5Calcolo Numerico - Elaborato 2File
<pre><code>gauss.m:function[x,d]=gauss(A,b)% GAUSS calcola la soluzione di un sistema del tipo Ax=b%% Parametri di ingresso:%% A = Matrice dei coefficienti del sistema (NxN)% b = Vettore dei termini noti (1xN)%% Parametri di uscita:%% x = Vettore con le soluzioni del sistema% d = Determinante della matrice A (facoltativo)%% Esempio d'uso: tenendo conto di [x,d]=gauss(A,b) si può avere%% >> [x,d]=gauss(rand(5,5),rand(1,5))%% x =%% Columns 1 through 3%% 1.86811067945846 1.69348271548917 1.35507405114225%% Columns 4 through 5%% -2.71083980957990 -1.00471453788528%%% d =%% 0.03974489824553format long;if (nargin==0) input specificato.');error('Nessunendbsize=size(b);asize=size(A);N=asize(1);if asize(1)~=asize(2)error('Matrice non quadrata.')endif bsize(2)~=asize(1)error('Utilizzare matrici NxN e vettori 1xN.')endif bsize(1)~=1 pluridimensionale.')error('Vettoreendc=0; % Controllore matriceif triu(A)==Ac=1; % Matrice triangolare superioreendif</code></pre>
tril(A)==Ac=-1; % Matrice triangolare inferiore endif c==0 % Algoritmo di eliminazione di Gauss con pivoting parziale for k=1:(N-1) r=min(find((abs(A([1:N],k)))==max(abs(A([k+1:N],k))))); % Determinare il più piccolo r:|A(r,k)|=max|A(i,k)|, i>=k if A(r,k)~=0 [A(k,:),A(r,:)]=deal(A(r,:),A(k,:)); % Scambia riga k con riga r [b(k),b(r)]=deal(b(r),b(k)); % Scambia b(r) con b(k) A([k+1:N],k)=A([k+1:N],k)/A(k,k); berragazzo 4/5