简体   繁体   English

如何在Matlab中求解常微分方程组(ODE)

[英]how to solve a system of Ordinary Differential Equations (ODE's) in Matlab

I have to solve a system of ordinary differential equations of the form: 我必须解决以下形式的常微分方程组:

dx/ds = 1/x * [y* (g + s/y) - a*x*f(x^2,y^2)]
dy/ds = 1/x * [-y * (b + y) * f()] - y/s - c

where x, and y are the variables I need to find out, and s is the independent variable; 其中x和y是我需要找出的变量,s是自变量; the rest are constants. 其余的都是常数。 I've tried to solve this with ode45 with no success so far: 我试图用ode45来解决这个问题,到目前为止没有成功:

y = ode45(@yprime, s, [1 1]);

function dyds = yprime(s,y)
    global g a v0 d
    dyds_1 = 1./y(1) .*(y(2) .* (g + s ./ y(2)) - a .* y(1) .* sqrt(y(1).^2 + (v0 + y(2)).^2));
    dyds_2 = - (y(2) .* (v0 + y(2)) .* sqrt(y(1).^2 + (v0 + y(2)).^2))./y(1) - y(2)./s - d;
   dyds = [dyds_1; dyds_2];
return

where @yprime has the system of equations. 其中@yprime有方程组。 I get the following error message: 我收到以下错误消息:

YPRIME returns a vector of length 0, but the length of initial conditions vector is 2. The vector returned by YPRIME and the initial conditions vector must have the same number of elements. YPRIME返回长度为0的向量,但初始条件向量的长度为2.YPRIME返回的向量和初始条件向量必须具有相同数量的元素。

Any ideas? 有任何想法吗? thanks 谢谢

Certainly, you should have a look at your function yprime . 当然,你应该看看你的功能yprime Using some simple model that shares the number of differential state variables with your problem, have a look at this example. 使用一些与您的问题共享差异状态变量数量的简单模型,看一下这个例子。

function dyds = yprime(s, y)
    dyds = zeros(2, 1);
    dyds(1) = y(1) + y(2);
    dyds(2) = 0.5 * y(1);
end

yprime must return a column vector that holds the values of the two right hand sides. yprime必须返回一个包含两个右边值的列向量。 The input argument s can be ignored because your model is time-independent. 输入参数s可以忽略,因为您的模型与时间无关。 The example you show is somewhat difficult in that it is not of the form dy/dt = f(t, y). 您展示的示例有点困难,因为它不是dy / dt = f(t,y)形式。 You will have to rearrange your equations as a first step. 作为第一步,您必须重新排列方程式。 It will help to rename x into y(1) and y into y(2) . x重命名为y(1)y重命名为y(2)

Also, are you sure that your global ga v0 d are not empty? 此外,你确定你的global ga v0 d不是空的吗? If any one of those variables remains uninitialized, you will be multiplying state variables with an empty matrix, eventually resulting in an empty vector dyds being returned. 如果这些变量中的任何一个仍然未初始化,您将使用空矩阵乘以状态变量,最终导致返回空向量dyds This can be tested with 这可以测试

assert(~isempty(v0), 'v0 not initialized');

in yprime , or you could employ a debugging breakpoint. yprime ,或者您可以使用调试断点。

the syntax for ODE solvers is [sy]=ode45(@yprime, [1 10], [2 2]) ODE求解器的语法是[sy]=ode45(@yprime, [1 10], [2 2])

and you dont need to do elementwise operation in your case ie instead of .* just use * 并且您不需要在您的情况下进行元素操作,而不是.*只需使用* y(:,1)相对s绘制初始条件[2 2]和参数值:g = 1; a = 2; v0 = 1; d = 1.5;

y(:,2)对比s

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

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