如何求解具有时变参数的常微分方程组 [英] How solve a system of ordinary differntial equation with time-dependent parameters

查看:593
本文介绍了如何求解具有时变参数的常微分方程组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决一个常微分方程系统..一个初值问题.....一个参数取决于时间或自变量的问题? 说出我拥有的方程式

How solve a system of ordinary differential equation ..an initial value problem ....with parameters dependent on time or independent variable? say the equation I have

 Dy(1)/dt=a(t)*y(1)+b(t)*y(2);
 Dy(2)/dt=-a(t)*y(3)+b(t)*y(1);
 Dy(3)/dt=a(t)*y(2);

其中a(t)是向量,b(t)= c * a(t);其中a和b的值随时间变化,而不是以单调的方式而不是每个时间步长变化. 我尝试使用此帖子 ....但是当我应用相同的原理时...我收到了错误消息

where a(t) is a vector and b(t) =c*a(t); where the value of a and b are changing with time not in monotone way and each time step. I tried to solve this using this post....but when I applied the same principle ...I got the error message

使用griddedInterpolant时出错点坐标不 严格按单调顺序排列."

"Error using griddedInterpolant The point coordinates are not sequenced in strict monotonic order."

有人可以帮我吗?

推荐答案

请仔细阅读直到结尾,看看答案的第一部分还是第二部分与您相关:

Please read until the end to see whether the first part or second part of the answer is relevant to you:

第1部分: 首先用一个描述您的计算的函数创建一个.m文件,该函数将给出ab.例如:创建一个名为fun_name.m的文件,其中将包含以下代码:

Part 1: First create an .m file with a function that describe your calculation and functions that will give a and b. For example: create a file called fun_name.m that will contain the following code:

function Dy = fun_name(t,y)

Dy=[ a(t)*y(1)+b(t)*y(2); ...
    -a(t)*y(3)+b(t)*y(1); ...
     a(t)*y(2)] ;
end

    function fa=a(t);
        fa=cos(t); % or place whatever you want to place for a(t)..
    end

    function fb=b(t);
        fb=sin(t);  % or place whatever you want to place for b(t)..
    end

然后使用具有以下代码的第二个文件:

Then use a second file with the following code:

t_values=linspace(0,10,101); % the time vector you want to use, or use tspan type vector, [0 10]
initial_cond=[1 ; 0 ; 0];  
[tv,Yv]=ode45('fun_name',t_values,initial_cond);
plot(tv,Yv(:,1),'+',tv,Yv(:,2),'x',tv,Yv(:,3),'o');
legend('y1','y2','y3');

当然,对于我写的fun_name.m案例,您不必为a(t)b(t)使用子函数,只要可能,您就可以使用Dy中的显式函数形式(例如cos(t)等) ).

Of course for the fun_name.m case I wrote you need not use sub functions for a(t) and b(t), you can just use the explicit functional form in Dy if that is possible (like cos(t) etc).

第2部分:如果a(t)b(t)只是您所遇到的数字矢量,而不能表示为t的函数(如第1部分中所述),则您'还需要有一个时间向量,每个时间向量都针对该时间向量发生,当然这可以与ODE使用的时间相同,但是只要插值有效,就不必如此.当它们具有不同的时间跨度或分辨率时,我将处理一般情况.然后,您可以执行以下操作,创建fun_name.m文件:

Part 2: If a(t) , b(t) are just vectors of numbers you happen to have that cannot be expressed as a function of t (as in part 1), then you'll need to have also a time vector for which each of them happens, this can be of course the same time you'll use for the ODE, but it need not be, as long as an interpolation will work. I'll treat the general case, when they have different time spans or resolutions. Then you can do something of the following, create the fun_name.m file:

function Dy = fun_name(t, y, at, a, bt, b) 

a = interp1(at, a, t); % Interpolate the data set (at, a) at times t
b = interp1(at, b, t); % Interpolate the data set (bt, b) at times t

Dy=[ a*y(1)+b*y(2); ...
    -a*y(3)+b*y(1); ...
     a*y(2)] ;

要使用它,请参见以下脚本:

In order to use it, see the following script:

%generate bogus `a` ad `b` function vectors with different time vectors `at` and `bt`

at= linspace(-1, 11, 74); % Generate t for a in a generic case where their time span and sampling can be different
bt= linspace(-3, 33, 122); % Generate t for b    
a=rand(numel(at,1));
b=rand(numel(bt,1));
% or use those you have, but you also need to pass their time info...

t_values=linspace(0,10,101); % the time vector you want to use 
initial_cond=[1 ; 0 ; 0];  
[tv,Yv]= ode45(@(t,y) fun_name(t, y, at, a, bt, b), t_values, initial_cond); % 
plot(tv,Yv(:,1),'+',tv,Yv(:,2),'x',tv,Yv(:,3),'o');
legend('y1','y2','y3');

这篇关于如何求解具有时变参数的常微分方程组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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