用差分法求出数值解之后,求与精确解误差出问题了。
Show older comments
求误差得到的结果是原函数的图不知道那里的语句出问题了,图片左为数值解图像右边误差除了问题,求解
程序如下:
clear all; close all;
a=4; h=0.05; x=[0:h:1];
tau=0.0125; t=[0:tau:1];
s=a*tau/h;
N=length(x)-1; M=length(t)-1;
[T X]=meshgrid(t,x);
%构造矩阵
e=s^2*ones(N-1,1);
A=spdiags([e 2*(1-e) e],[-1 0 1],N-1,N-1);
%设置初始条件和边界条件
u=zeros(N+1,M+1);
u(:,1)=sin(pi*x); u(:,2)=0;
u(1,:)=0; u(end,:)=0;
%有限差分
for n=2:M
u(2:N,n+1)=A*u(2:N,n)-u(2:N,n-1);
u(2,n+1)=u(2,n+1)+s^2*u(1,n);
u(N,n+1)=u(N,n+1)+s^2*u(end,n);
end
%画图
subplot(2,2,1)
mesh(t,x,u), view(10,10), xlabel t, ylabel x, zlabel u
subplot(2,2,2)
%与解析解的误差
m=sin(pi*X).*cos(4*pi*T)
mesh(t,x,u-m), view(10,8), axis([0 1 0 1 -10 10])
xlabel t, ylabel x, zlabel Error
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!