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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| clear all
close all
h=input('Donnez le pas espace h:');
a=0;b=1;
N=round((b-a)/h)
k=input('donnez le pas tmps k:');
a=k/((pi^2)*(h^2));
for i=1:N
x(i)=i*h;
end
for j=1:N
t(j)=j*k;
end
for j=1:N
Uh(1,j)=0;
end
for j=1:1:N
Uh(N,j)=0;
end
for i=2:1:N-1
Uh(i,1)=sin(pi*x(i));
end
%solution exacte
for i=1:N
for j=1:N
U(i,j)=exp(-t(j))*sin(pi*x(i));
end
end
%solutions approchée
fprintf('\t\t\tsolution approcheé a laide de shcema explicite \n');
for i=2:1:N-1
for j=2:1:N
Uh(i,j)=a*Uh(i+1,j-1)+(1-2*a)*Uh(i,j-1)+a*Uh(i-1,j-1);
end
end
% Erreur
for i=1:1:N
for j=1:1:N
EC(i,j)=abs(U(i,j)-Uh(i,j));
end
end
if N==10
V=[Uh(1,:) Uh(2,:) Uh(3,:) Uh(4,:) Uh(5,:) Uh(6,:) Uh(7,:) Uh(8,:) Uh(9,:) Uh(10,:)];
Z=[U(1,:) U(2,:) U(3,:) U(4,:) U(5,:) U(6,:) U(7,:) U(8,:) U(9,:) U(10,:) ];
P=[EC(1,:) EC(2,:) EC(3,:) EC(4,:) EC(5,:) EC(6,:) EC(7,:) EC(8,:) EC(9,:) EC(10,:) ];
subplot(3,1,1)
plot(Z);
title('solution exacte');
subplot(3,1,2)
plot(V);
title('solution approche');
subplot(3,1,3)
plot(P);
title('Erreur de convergence');
else
subplot(3,1,1)
plot(U);
title('solution exacte');
subplot(3,1,2)
plot(Uh);
title('solution approche');
subplot(3,1,3)
plot(EC);
title('Erreur de convergence');
end |