Problem with finite difference method.

4 Ansichten (letzte 30 Tage)
Vipin  Padinjarath
Vipin Padinjarath am 21 Mai 2019
I am trying to implement Finite difference method. I created the tri diagonal matrix, then solved using the command y=A\b. But the same problem, when done through the Thomas algorithm, answer is different from the first one. The curve looks the same, but there are differences in the numerical values. Why is that so?
x=0.0:pi/6:pi;
N=length(x);
h=x(2)-x(1);
%%Creating tridiagonal matrix
A=zeros(N,N);
a=-2/h^(2);%main diagonal
b=(1/h^(2));%sub diagonal
c=(1/h^(2));%super diagonal
r=(x.*sin(x))';
for i=1:N
A(i,i)=a;
end
for j=2:N
A(j,j-1)=b;
A(j-1,j)=c;
end
%%Solving for y
y=A\r;
y(1)=0;
y(N)=0;
xf=0:pi/50:pi;
sol=2*(1-cos(xf))-xf.*sin(xf)-4*xf/pi;
clf
plot (xf,sol,'r')
hold on
plot(x,y)

Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by