如何在Matlab中求解常微分方程(ODE)系统 [英] how to solve a system of Ordinary Differential Equations (ODE's) in Matlab

查看:132
本文介绍了如何在Matlab中求解常微分方程(ODE)系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解决以下形式的常微分方程组:

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

其中x和y是我需要找出的变量,而s是自变量;其余的是常量.到目前为止,我尝试用ode45解决此问题,但未成功:

where x, and y are the variables I need to find out, and s is the independent variable; the rest are constants. I've tried to solve this with ode45 with no success so far:

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

其中@yprime具有方程组.我收到以下错误消息:

where @yprime has the system of equations. I get the following error message:

YPRIME返回长度为0的向量,但初始长度为 条件向量是2.YPRIME返回的向量和初始 条件向量必须具有相同数量的元素.

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.使用一个与您的问题共享微分状态变量数量的简单模型,看看这个例子.

Certainly, you should have a look at your function 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必须返回一个包含两个右手边值的列向量.输入参数s可以忽略,因为您的模型与时间无关.您显示的示例有些困难,因为它的形式不是dy/dt = f(t,y).第一步,您将不得不重新排列方程式.它将有助于将x重命名为y(1)y重命名为y(2).

yprime must return a column vector that holds the values of the two right hand sides. The input argument s can be ignored because your model is time-independent. The example you show is somewhat difficult in that it is not of the form 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).

此外,您确定您的global g a v0 d不为空吗?如果这些变量中的任何一个仍未初始化,则将状态变量与一个空矩阵相乘,最终导致返回一个空向量dyds.可以用

Also, are you sure that your global g a v0 d are not empty? 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. This can be tested with

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

中的

,或者您可以使用调试断点.

in yprime, or you could employ a debugging breakpoint.

这篇关于如何在Matlab中求解常微分方程(ODE)系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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