Aggiunto gauss naif e implementato alcuni script come funzioni.
This commit is contained in:
44
functions/convert_matrix_to_triangular_matrix_gauss_naif.m
Normal file
44
functions/convert_matrix_to_triangular_matrix_gauss_naif.m
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
% questo algoritmo converte una matrice quadrata generica in
|
||||
% triangolare superiore mediante metodo di gauss
|
||||
% i moltiplicatori sono salvati nella matrice inferiore
|
||||
|
||||
%U=[10,20,30;1,5,3;4,56,34]; matrice di input
|
||||
%b=[1;2;17]; termini noti
|
||||
|
||||
function [U,b] = convert_matrix_to_triangular_matrix_gauss_naif (U,b)
|
||||
|
||||
|
||||
n= length(b); % lunghezza del vettore dei termini noti o numero equazioni
|
||||
U= [U,b]; % uniamo la matrice con la colonna dei termini noti
|
||||
|
||||
|
||||
ok=1;
|
||||
%check 0 sulla diagonale
|
||||
for i=1:n
|
||||
if U(i,i) == 0
|
||||
ok=0;
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if ok == 0
|
||||
disp('impossibile convertire')
|
||||
b=[];
|
||||
U=[];
|
||||
else
|
||||
% convertiamo la matrice in una triangolare superiore
|
||||
for i=1:1:n-1
|
||||
for j=i+1:1:n
|
||||
m= U(j,i) / U(i,i);
|
||||
U(j,:) = U(j,:) + ( U(i,:) * (- m ) ) ;
|
||||
U(j,i)=m;
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
b=U(:,n+1);
|
||||
U=U(:,1:n);
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
% questo algoritmo converte una matrice quadrata generica in
|
||||
% triangolare superiore mediante metodo di gauss con pivot parziale
|
||||
% i moltiplicatori sono salvati nella matrice inferiore
|
||||
|
||||
%U=[10,20,30;1,5,3;4,56,34]; matrice di input
|
||||
%b=[1;2;17]; termini noti
|
||||
|
||||
function [U,b] = convert_matrix_to_triangular_matrix_gauss_pivoting (U,b)
|
||||
|
||||
|
||||
n= length(b); % lunghezza del vettore dei termini noti o numero equazioni
|
||||
U= [U,b]; % uniamo la matrice con la colonna dei termini noti
|
||||
|
||||
% convertiamo la matrice in una triangolare superiore
|
||||
for i=1:1:n-1
|
||||
x_max = max ( abs(U(i:n,i)) );
|
||||
if x_max == 0
|
||||
break
|
||||
else
|
||||
[x,y]= ind2sub(size(U), find (abs(U(i:n,i)) == x_max) );
|
||||
x= x + i -1;
|
||||
y = i;
|
||||
|
||||
if x~= i
|
||||
U([i x],:) = U([x i],:);
|
||||
|
||||
|
||||
end
|
||||
|
||||
for j=i+1:1:n
|
||||
|
||||
m= U(j,i) / U(i,i);
|
||||
U(j,:) = U(j,:) + ( U(i,:) * (- m ) ) ;
|
||||
U(j,i)=m;
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if x_max == 0 || U(n,n) == 0
|
||||
disp('errore')
|
||||
b=[];
|
||||
U=[];
|
||||
else
|
||||
b=U(:,n+1);
|
||||
U=U(:,1:n);
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
28
functions/float2bin.m
Normal file
28
functions/float2bin.m
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
function b = float2bin(f)
|
||||
%This function converts a floating point number to its binary form.
|
||||
|
||||
%Input error handling
|
||||
if ~isfloat(f)
|
||||
disp('Input must be a floating point number.');
|
||||
return;
|
||||
end
|
||||
|
||||
%Hex characters
|
||||
hex = '0123456789abcdef';
|
||||
|
||||
%Convert from float to hex
|
||||
h = num2hex(f);
|
||||
|
||||
%Convert to cell array of chars
|
||||
hc = num2cell(h);
|
||||
|
||||
%Convert to array of numbers
|
||||
nums = cellfun(@(x) find(hex == x) - 1, hc);
|
||||
|
||||
%Convert to array of binary numbers
|
||||
bins = dec2bin(nums, 4);
|
||||
|
||||
%Reshape into horizontal vector
|
||||
b = reshape(bins.', 1, numel(bins));
|
||||
end
|
||||
23
functions/linear_system_resolver_triangular_matrix.m
Normal file
23
functions/linear_system_resolver_triangular_matrix.m
Normal file
@@ -0,0 +1,23 @@
|
||||
%risoluzione matrice triangolare superiore con algoritmo
|
||||
% di sostituzione botton up (all'indietro)
|
||||
|
||||
%example input
|
||||
% U=[2,2,4;0,-7,-11;0,0,2];
|
||||
% b=[5;-8;-2];
|
||||
|
||||
|
||||
function[x] = linear_system_resolver_triangular_matrix(U,b)
|
||||
|
||||
x=[];
|
||||
n= length(b);
|
||||
x(n) = b(n)/U(n,n);
|
||||
|
||||
for i=n-1:-1:1
|
||||
somma = 0;
|
||||
for k=i+1:n
|
||||
somma=somma+U(i,k) * x(k);
|
||||
end
|
||||
|
||||
x(i) = (b(i) - somma)/ U(i,i);
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user