使用ode45和deval时出错 [英] Error using ode45 and deval

查看:378
本文介绍了使用ode45和deval时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整合一组常微分方程,并计算给定点的解及其一阶导数. Matlab提供了"ode45"来求解方程式,并提供了"deval"来计算解及其一阶导数,但是,在我的情况下,我无法使用它们.

Matlab给出以下示例:

sol = ode45(@ vdp1,[0 20],[2 0]);

x = linspace(0,20,100);

y = deval(sol,x,1);

图(x,y);

我猜Matlab已经定义了"vdp1",这个例子很好用.但是,当我尝试使用以下代码时:

sol = ode45('vdp1',[0 20],[2 0]);

x = linspace(0,20,100);

y = deval(sol,x,1);

图(x,y); 它声称错误:

使用deval时出错(第46行) sol必须是微分方程求解器返回的结构."

所以我认为我必须在"ode45"中使用"@ vdp1",而不是'vdp1'",否则"deval"将不起作用.

但就我而言,我在文件中将派生定义如下:

function dv = der( ~,vec,~,alpha, eta, m )
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here

vx=vec(1);
vy=vec(2);
vz=vec(3);

dv(1,1)=(alpha-eta*vx)/m;
dv(2,1)=(alpha-eta*vy)/m;
dv(3,1)=(alpha-eta*vz)/m;

end

我可以使用"ode45"通过以下代码来求解方程:

clear all;

% Parameters 
alpha=10;
eta=0.5;
m=1;

% Inititial conditions
vec_ini=[0,0,0]';

% Integration time
Tf=10;

OPTIONS=odeset('abstol',1e-6,'reltol',1e-6);

sol=ode45('der',[0,Tf],vec_ini,OPTIONS,alpha,eta,m);

代码有效.但是当我再添加两行以使用"deval"时,如下所示:

tvec=linspace(0,Tf,10);

[sxint,spxint]=deval(sol,tvec);

它说:

使用deval时出错(第46行)

sol必须是由微分方程求解器返回的结构.

如果我将ode45部分更改为

sol=ode45(@der,[0,Tf],vec_ini,OPTIONS,alpha,eta,m);

使用der时出错(第9行)

输入参数不足.

涂饰错误(第88行)

f0 = feval(ode,t0,y0,args {:}); %ODE15I将args {1}设置为yp0.

ode45错误(第114行)

[neq,tspan,ntspan,next,t0,tfinal,tdir,y0,f0,odeArgs,odeFcn,...

那么有人可以告诉我如何使用"ode45"和"deval"来计算解决方案及其带有客户定义的导数的一阶导数吗?

解决方案

我认为您没有正确使用函数句柄.我建议如下重写您的der函数:

function dv = der( t,y,params )

vx=y(1);
vy=y(2);
vz=y(3);

dv(1,1)=(params(1)-params(2)*vx)/params(3);
dv(2,1)=(params(1)-params(2)*vy)/params(3);
dv(3,1)=(params(1)-params(2)*vz)/params(3);

end

,然后按照以下方式使用ode:

params = [alpha eta m];
sol=ode45(@(t,y) der(t,y,params),[0,Tf],vec_ini,OPTIONS);
tvec=linspace(0,Tf,10);
[sxint,spxint]=deval(sol,tvec);

I am trying to integrate a set of ordinary differential equations and calculate the solution and its first order derivative at given points. Matlab provides "ode45" to solve the equations and "deval" to calculate the solution and its first derivative, however, I am not able to use them in my case.

Matlab gives the following example:

sol = ode45(@vdp1,[0 20],[2 0]);

x = linspace(0,20,100);

y = deval(sol,x,1);

plot(x,y);

where I guess "vdp1" has been defined by Matlab and this example worked well. However, when I tried to use the following codes:

sol = ode45('vdp1',[0 20],[2 0]);

x = linspace(0,20,100);

y = deval(sol,x,1);

plot(x,y); it claims error:

"Error using deval (line 46) sol must be a structure returned by a differential equation solver."

So I think I have to use "@vdp1" in "ode45" instead of "'vdp1'", otherwise "deval" would not work.

But in my case I defined my derivative in a file as the following:

function dv = der( ~,vec,~,alpha, eta, m )
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here

vx=vec(1);
vy=vec(2);
vz=vec(3);

dv(1,1)=(alpha-eta*vx)/m;
dv(2,1)=(alpha-eta*vy)/m;
dv(3,1)=(alpha-eta*vz)/m;

end

I can use "ode45" to solve the equation with the following codes:

clear all;

% Parameters 
alpha=10;
eta=0.5;
m=1;

% Inititial conditions
vec_ini=[0,0,0]';

% Integration time
Tf=10;

OPTIONS=odeset('abstol',1e-6,'reltol',1e-6);

sol=ode45('der',[0,Tf],vec_ini,OPTIONS,alpha,eta,m);

The codes worked. But when I added two more lines to use "deval" as follows

tvec=linspace(0,Tf,10);

[sxint,spxint]=deval(sol,tvec);

it saied:

Error using deval (line 46)

sol must be a structure returned by a differential equation solver.

If I change the ode45 part as

sol=ode45(@der,[0,Tf],vec_ini,OPTIONS,alpha,eta,m);

it said

Error using der (line 9)

Not enough input arguments.

Error in odearguments (line 88)

f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 114)

[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

So can anyone tell me how to use "ode45" and "deval" to calculate the solution and its first derivative with a customer defined derivative?

解决方案

I think you are not using the function handle correctly. I would suggest rewriting your der function as follows:

function dv = der( t,y,params )

vx=y(1);
vy=y(2);
vz=y(3);

dv(1,1)=(params(1)-params(2)*vx)/params(3);
dv(2,1)=(params(1)-params(2)*vy)/params(3);
dv(3,1)=(params(1)-params(2)*vz)/params(3);

end

and then olving the ode as follows:

params = [alpha eta m];
sol=ode45(@(t,y) der(t,y,params),[0,Tf],vec_ini,OPTIONS);
tvec=linspace(0,Tf,10);
[sxint,spxint]=deval(sol,tvec);

这篇关于使用ode45和deval时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆