Problem with finite difference method.

조회 수: 3 (최근 30일)
Vipin  Padinjarath
Vipin Padinjarath 2019년 5월 21일
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)

답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by