简体   繁体   English

为什么我不断收到这一小段代码的错误 (ode45)

[英]Why do I keep receiving error with this little piece of code (ode45)

function dy = g2(x, y) function dy = g2(x, y)

dy = -0.1 * y; dy = -0.1 * y;

ym = ode45('g2', 0, 5, 4) ym = ode45('g2', 0, 5, 4)

end结尾

I receive the following message:我收到以下消息:

g2(0.5,4) Error using odearguments (line 83) The last entry in tspan must be different from the first entry. g2(0.5,4) 使用 odearguments 时出错(第 83 行)tspan 中的最后一个条目必须与第一个条目不同。

Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin); ode45 错误(第 115 行)odearguments(FcnHandlesUsed,solver_name,ode,tspan,y0,options,varargin);

Error in g2 (line 9) ym = ode45('g2', 0, 5, 4); g2 错误(第 9 行) ym = ode45('g2', 0, 5, 4);

I might add that this one works well:我可能会补充说这个效果很好:

function dy = g1(x, y) function dy = g1(x, y)

dy = 3 * x ^ 2; dy = 3 * x ^ 2;

ym = ode45('g1', 2, 4, 0.5) ym = ode45('g1', 2, 4, 0.5)

end结尾

I am not understanding what you are trying to do, however I will give you an example.我不明白你想做什么,但我会给你一个例子。

Usually your functions are defined at the bottom and you call ODE like that:通常您的函数定义在底部,您可以这样调用 ODE:

t=linspace(0,7,1000);
initial_value_for_y = 0;
[t,y] = ode45(@myfunction, t, initial_value_for_y);

function dy = myfunction(t, y)
    dy = exp(-t);
end

so in the first line we define a vector for time using linspace.所以在第一行中,我们使用 linspace 定义了一个时间向量。 at the second line we set the initial value of our integration the third line calls ODE45 with a function handle, the time span and an initial value在第二行,我们设置积分的初始值 第三行使用 function 句柄、时间跨度和初始值调用 ODE45

the rest of the lines are for the definition of your function这些行的 rest 用于定义您的 function

My concern for now is that you question is not clear.我现在担心的是你的问题不清楚。 Instead of asking "why isn't it working", tell us what you are trying to achieve.不要问“为什么它不起作用”,而是告诉我们您想要实现的目标。

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

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