Problem with finite difference method.

2 ビュー (過去 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 Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by