用MATLAB写了一个解扩散方程的程序(用的是老师给的程序),可是运行显示出错,哪位高手帮帮忙!!
题是:假设扩散方程du/dt=adu^2/dx^2,(这是偏微分方程,应为打不出那个符号,用dt,dx代替),取dx=pi/15,u(x,0)=sin(x),u(0,t)...
题是:假设扩散方程du/dt=a du^2/dx^2,(这是偏微分方程,应为打不出那个符号,用dt,dx代替),取dx=pi/15,u(x,0)=sin(x),u(0,t)=0,u(pi,t)=0
这是源程序:
function demo_MOL
clc;clear all;format long;
n=15;dpi=pi/n;
x=dpi:dpi:pi-dpi;u=sin(x);
t=0.:0.4:40.;
[t u]=ode45('myfun',t,u);
uu(:,2:n)=u(:,1:n-1);
uu(:,1)=0;uu(:,n+1)=0;
x=[0. x pi];[xx yy]=meshgrid(x,t);
surf(xx,yy,uu);
xlabel('x');ylabel('t');zlabel('u(x,t)');
end
function y=myfun(~,u)
n=15;
x(1)=-2*u(1)+u(2);
x(n-1)=u(n-2)-2*u(n-1);
for i=2:n-2
x(i)=u(i-1)-2*u(i)+u(i+1);
end
y=x';
end
显示错误是
??? Error using ==> feval
Undefined function or method 'myfun' for input arguments of type
'double'.
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Error in ==> demo_MOL at 6
[t u]=ode45('myfun',t,u);
我用的是r2009b,请高手帮帮忙!! 展开
这是源程序:
function demo_MOL
clc;clear all;format long;
n=15;dpi=pi/n;
x=dpi:dpi:pi-dpi;u=sin(x);
t=0.:0.4:40.;
[t u]=ode45('myfun',t,u);
uu(:,2:n)=u(:,1:n-1);
uu(:,1)=0;uu(:,n+1)=0;
x=[0. x pi];[xx yy]=meshgrid(x,t);
surf(xx,yy,uu);
xlabel('x');ylabel('t');zlabel('u(x,t)');
end
function y=myfun(~,u)
n=15;
x(1)=-2*u(1)+u(2);
x(n-1)=u(n-2)-2*u(n-1);
for i=2:n-2
x(i)=u(i-1)-2*u(i)+u(i+1);
end
y=x';
end
显示错误是
??? Error using ==> feval
Undefined function or method 'myfun' for input arguments of type
'double'.
Error in ==> odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Error in ==> demo_MOL at 6
[t u]=ode45('myfun',t,u);
我用的是r2009b,请高手帮帮忙!! 展开
1个回答
展开全部
%只修改了一点语法错误,修正了向量维数,没对数据做任何修改:
function demo_MOL
clc;clear all;format long;
n=15;dpi=pi/n;
x=dpi:dpi:pi;
size(x)
u=sin(x);
t=0.:0.4:40.;
[t u]=ode45(@myfun,t,u);
uu(:,2:n)=u(:,1:n-1);
uu(:,1)=0;uu(:,n+1)=0;
x=[0. x];[xx yy]=meshgrid(x,t);
surf(xx,yy,uu);
xlabel('x');ylabel('t');zlabel('u(x,t)');
end
function y=myfun(~,u)
n=15;
x=zeros(1,n);
x(1)=-2*u(1)+u(2);
x(n-1)=u(n-2)-2*u(n-1);
for i=2:n-2
x(i)=u(i-1)-2*u(i)+u(i+1);
end
y=x';
end
function demo_MOL
clc;clear all;format long;
n=15;dpi=pi/n;
x=dpi:dpi:pi;
size(x)
u=sin(x);
t=0.:0.4:40.;
[t u]=ode45(@myfun,t,u);
uu(:,2:n)=u(:,1:n-1);
uu(:,1)=0;uu(:,n+1)=0;
x=[0. x];[xx yy]=meshgrid(x,t);
surf(xx,yy,uu);
xlabel('x');ylabel('t');zlabel('u(x,t)');
end
function y=myfun(~,u)
n=15;
x=zeros(1,n);
x(1)=-2*u(1)+u(2);
x(n-1)=u(n-2)-2*u(n-1);
for i=2:n-2
x(i)=u(i-1)-2*u(i)+u(i+1);
end
y=x';
end
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询