1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| clc;clear all;close all;
pkg load symbolic
syms x y
% y'=2*x*y
fg= 2*x*y; % Eqt we interest to solve
fex=exp(x^2); % Exact solution
f=fg;
a=0;
b=1;
n=2;
h=(b-a)/n;
y0=1; % Initial cdt
f= y0+subs(int(f,x,0,x),y,1);
for j=a:h:b
% i=i+1;
fn=subs(fg,y,f)
f=y0+int(fn,x);
end
i=0;zp(1)=0; zex(1)=0;
for j=a:h:b
i=i+1;
zp(i+1)=zp(i)+subs(f,x,j);
zex(i+1)=zex(i) +subs(fex,x,j);
xx(i)=j;
end
plot(xx,zp(1:n+1),xx,zex(1:n+1))
legend('Picard method', 'Exact solution')
grid on |
Partager