简体   繁体   English

在Matlab中使用@或''作为ode45?

[英]Using @ or '' for ode45 in Matlab?

I am a little confused as to when to use ode45(@functionname, tspan, initialvalues,...) or ode45('functionname', tspan, initial values,...). 关于何时使用ode45(@ functionname,tspan,initialvalues,...)或ode45('functionname',tspan,initial values,...),我有点困惑。 I have seen examples using both but sometimes one works and the other doesn't. 我见过使用这两种情况的例子,但有时一种是有效的,另一种则没有。

eg [t,y]=ode45(@MM2, tspan, y0,[],k1,k2,k3) works, but [t,y]=ode45('MM2', tspan, y0,[],k1,k2,k3) doesn't. 例如[t,y]=ode45(@MM2, tspan, y0,[],k1,k2,k3)有效,但[t,y]=ode45('MM2', tspan, y0,[],k1,k2,k3)没有。

Many thanks in advance. 提前谢谢了。

As I have understood, you will use "@" when the function you are going to integrate is in another text file. 据我所知,当你要集成的函数在另一个文本文件中时,你将使用“@”。 If the function is in the same text file, you don't need to use "@". 如果该函数在同一文本文件中,则不需要使用“@”。

For example: let's calculate the horizontal coordinate of a Van Der Pol pendulum. 例如:让我们计算Van Der Pol摆的水平坐标。

In file 1: xdot_van_der_pol.m 在文件1中:xdot_van_der_pol.m

function dxdt = xdot_van_der_pol(t, x)
global u;
if size(u,1) == 0
    u = 1
end
dx1 = x(2);
dx2 = u*(1 - x(1)^2)*x(2) - x(1);
dxdt = [ dx1 ; dx2 ];

In file 2: integration.m 在文件2中:integration.m

u = 1;
tf = 20;
xo = [2 ; 0];
[t,x]=ode45(@xdot_van_der_pol, [0 tf], xo);
subplot(221); plot(x(:,1), t(:,1)); hold on;
subplot(224); plot(t(:,1), x(:,2)); hold on;
subplot(223); plot(x(:,1), x(:,2)); hold on;

Another case would be to write everything in the same text file, so you wouldn't have to use "@" to call the function. 另一种情况是将所有内容写入同一文本文件中,因此您不必使用“@”来调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM