Matlab:Crank Nicolson方法求解线性抛物方程

Matlab:Crank Nicolson方法求解线性抛物方程

 tic;
clear
clc
M=[,,,,,,];%x的步数
K=M; %时间t的步数
for p=:length(M)
hx=/M(p);
ht=/K(p);
r=ht/hx^; %网格比
x=:hx:;
t=:ht:;
numerical=zeros(M(p)+,K(p)+);
numerical(:,)=exp(x); %初始值
numerical(,:)=exp(t); %边值
numerical(M(p)+,:)=exp(t+); %边值
a=-r/*ones(M(p)-,);b=(+r)*ones(M(p)-,);c=-r/*ones(M(p)-,);
fun1=inline('exp(x+t)','x','t');
for i=:length(x)
for j=:length(t)
Accurate(i,j)=fun1(x(i),t(j));
end
end
d=r/*ones(M(p)-,);e=(-r)*ones(M(p)-,);f=r/*ones(M(p)-,);
B=diag(d,-)+diag(e,)+diag(f,);
fun2=inline('','x','t');
for i=:M(p)-
for k=:K(p)
f(i,k)=ht*fun2(x(i+),t(k)+ht/);
end
end
for k=:K(p)
f(,k)=r/*(numerical(,k+)+numerical(,k));
f(M(p)-,k)=r/*(numerical(M(p)+,k+)+numerical(M(p)+,k));
end
for k=:K(p)
right_vector=f(:,k)+B*numerical(:M(p),k);
numerical(:M(p),k+)=chase(a,b,c,right_vector);
end
error=numerical(:M(p),:K(p))'-Accurate(2:M(p),2:K(p))';
error_inf(p)=max(max(error));
figure(p)
[X,Y]=meshgrid(x,t);
subplot(,,)
mesh(X,Y,Accurate');
xlabel('x'),ylabel('t');zlabel('Accurate');
title('the image of Accurate result');
grid on
subplot(,,)
mesh(X,Y,numerical');
xlabel('x'),ylabel('t');zlabel('numerical');
title('the image of numerical result');
grid on
subplot(,,)
mesh(X,Y,numerical'-Accurate');
xlabel('x'),ylabel('t');zlabel('error');
title('the image of error result');
grid on
end
for k=:length(M)
H=error_inf(p-)/error_inf(p);
E_inf(k-)=log2(H);
end
figure(length(M)+)
plot(:length(M)-,E_inf,'-r v');
ylabel('误差阶数');
title('Crank nicolson 误差阶数');
grid on
toc;

Matlab:Crank Nicolson方法求解线性抛物方程

上一篇:SAP的表类型和行类型


下一篇:SAP物料主数据不同视图的标志字段