save an algorithm and call it

1 Ansicht (letzte 30 Tage)
Nima Vali
Nima Vali am 21 Okt. 2020
Kommentiert: Nima Vali am 21 Okt. 2020
hello all
I want to save the Thomas algorithm and backward substitution algorithm and call it whenver I need it in my code. How to do this?
solving linear system with Thomas algorithm*******************************
for k = 1:p-1
i = k +1;
l(i,k)=Ac(i,k)/Ac(k,k);
for j = k :k+1
Ac(i,j) = Ac(i,j)- l(i,k)*Ac(k,j);
end
b(i) = b(i)-l(i,k)*b(k);
end
%apply backward substitution
for k = p:-1:1
g(k)=b(k);
for j=k+1: min(p,k+1)
g(k)=g(k)-Ac(k,j)*g(j);
end
g(k)=g(k)/Ac(k,k);
end
T_cn(2:n-1)=g(1:p);
end
  2 Kommentare
Rik
Rik am 21 Okt. 2020
Do you mean you want to make this a function?
Nima Vali
Nima Vali am 21 Okt. 2020
Yes, I want to call it like z=thomas(Ac,b,p-2);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 21 Okt. 2020
Store this in thomas.m
function z=thomas(Ac,b,p_minus_2)
%solving linear system with Thomas algorithm
%
% More explanation about this function and its syntax go here.
p=p_minus_2+2;
for k = 1:p-1
i = k +1;
l(i,k)=Ac(i,k)/Ac(k,k);
for j = k :k+1
Ac(i,j) = Ac(i,j)- l(i,k)*Ac(k,j);
end
b(i) = b(i)-l(i,k)*b(k);
end
%apply backward substitution
for k = p:-1:1
g(k)=b(k);
for j=k+1: min(p,k+1)
g(k)=g(k)-Ac(k,j)*g(j);
end
g(k)=g(k)/Ac(k,k);
end
T_cn(2:n-1)=g(1:p);
end

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by