# Thomas algorithm - tridiagonal matrix

20 views (last 30 days)
Cesar Cardenas on 1 Mar 2023
Commented: Cesar Cardenas on 2 Mar 2023
Is there any other way to code and solve the tridiagonal matrix? the idea would be to try to get the plot shown. Matlab beginner, so, no sure how to do it. Any help will be greatly appreciated. Thanks
clear
cm=1/100;
delta = 1*cm;
nu=1e-6;
Uinf=1;
H=2*delta;
y=linspace(0,H,40);
u=Uinf*erf(3*y/delta);
dy=mean(diff(y));
dx=100*dy;
Q=nu*dx/Uinf/dy^2;
a=-Q;
c=-Q;
b=1+2*Q;
N=length(y)-2;
M = diag(b*ones(1,N)) + diag(c*ones(1,N-1),1) + diag(a*ones(1,N-1),-1);
%Constant Ue
Ue = @(x) Uinf;
u = u(2:end-1);
x=0;
uall=[0,u,Ue(x)];

Torsten on 2 Mar 2023
Edited: Torsten on 2 Mar 2023
clear
cm=1/100;
delta = 1*cm;
nu=1e-6;
Uinf=1;
H=2*delta;
y=linspace(0,H,40);
u=Uinf*erf(3*y/delta);
dy=mean(diff(y));
dx=100*dy;
Q=nu*dx/Uinf/dy^2;
a=-Q;
c=-Q;
b=1+2*Q;
N = length(y);
M = diag(b*ones(1,N)) + diag(c*ones(1,N-1),1) + diag(a*ones(1,N-1),-1);
M(1,:) = [1,zeros(1,N-1)];
M(end,:) = [zeros(1,N-1),1];
%Constant Ue
Ue = @(x) Uinf;
u = u(2:end-1);
x=0;
uall=[0,u,Ue(x)];
sol = M\uall.';
plot(y,sol)
grid on
Torsten on 2 Mar 2023
You can assign values to certain elements in a matrix by using a loop. But if the above line to define M is correct, it's elegant, isn't it ? Why do you want to define it differently ?
Cesar Cardenas on 2 Mar 2023
Right thank you, just wanted to have a different version. Thanks