two-point boundary value problem + plot

Hello,
I am trying to solve this question: Solve the two-point boundary value problem utilizing LU factorization for tridiagonal matrices. Make two plots: one with h = 1/4 and the other with h = 1/16. On each plot, graph the numerical solution with circles connected by lines and the real solution. Include a legend on each graph.
−y'' = 25 sin(πx), 0 ≤ x ≤ 1, y(0) = 0, y(1) = 1
I am using this code and I let n=4. How can I fix my code to plot both n=4 and n=16 together.
%-y''=25sin(pix), y_0=0, y_1=1
clc
r=inline('25.*sin(pi.*x)','x');
alpha=0; beta=1;
n=4;
h=1/n;
x=[alpha+h:h:(n-1)*h];
A=2*eye(n-1)-diag(ones(n-2,1),-1)-diag(ones(n-2,1),1);
A=(1/h^2)*A;
b=r(x);
b(n-1)=b(n-1)+beta/h^2;
b(1)=b(1)+alpha/h^2;
w=A\b'
soln=[alpha, w' beta]
xval=[alpha, x, beta]
plot(xval,soln,'o-')

2 commentaires

Hi, is that what you need ?
%-y''=25sin(pix), y_0=0, y_1=1
clc
r=inline('25.*sin(pi.*x)','x');
alpha=0; beta=1;
n=[4 16];
color='br'; %blue for n=4 and red for n=16
figure
hold on
for i=1:2
h=1/n(i);
x=[alpha+h:h:(n(i)-1)*h];
A=2*eye(n(i)-1)-diag(ones(n(i)-2,1),-1)-diag(ones(n(i)-2,1),1);
A=(1/h^2)*A;
b=r(x);
b(n(i)-1)=b(n(i)-1)+beta/h^2;
b(1)=b(1)+alpha/h^2;
w=A\b';
soln=[alpha, w' beta];
xval=[alpha, x, beta];
plot(xval,soln,'o-','Color',color(i))
legend_text{i}=['n= ',num2str(n(i))];
end
legend(legend_text);
ateq alsaadi
ateq alsaadi le 3 Avr 2015
Yes it works :)
Thank you.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by