Problem with finite difference method.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!