简单的脚本,计算线性ODE的解决方案,给出错误的结果

时间:2016-03-27 01:44:01

标签: matlab differential-equations

这是一个既涉及编程又涉及数学的问题。所以,我试图编写一个代码来计算由system描述的线性ODE系统的一般解决方案。上面显示的数学公式为general solution

在等式中出现的希腊符号\ PHI是expm(A*t)

 clear all

 A=[-2]; %system matrix

 t0=1; %initial time of simulation

 tf=2; %final time of simulation

 syms t x_0 

 x0=x_0;

 hom=expm(A*t); %hom means "homogeneous solution"

 hom_initialcond=hom*x0;%this is the homogeneous solution multiplied by the initial conditon

 invhom=inv(hom); %this is the inverse of the greek letter at which, multiplied by the input of the system, composes the integrand of the integral

 g=5*cos(2*t); %system input

 integrand=invhom*g; %computation of the integrand

 integral=int(integrand,t0,t); %computation of the definite integral from t0 to t, as shown by the math formula

 partsol=hom*integral; %this is the particular solution

 gen_sol=partsol+hom_initialcond %this is the general solution

 x_0=1; %this is the initial condition

 t=linspace(t0,tf); %vector of time from t0 to tf

 y=double(subs(gen_sol)); %here I am evaluating my symbolic expression

 plot(t,y)

问题是我的ODE解决方案的情节看起来不太好,你可以看到: plot

解决方案错误,因为图中显示的曲线不是从初始值开始等于1。但它的形状与MATLAB ODE求解器给出的图非常相似: enter image description here

但是,如果我设置t0=0,那么我的代码和MATLAB解算器提供的图表就会非常相似。所以,我的代码对于t0=0来说很好,但是对于任何其他值我的代码出错了。

1 个答案:

答案 0 :(得分:3)

基本矩阵方面的一般解决方案是

Equation giving the solution of an ODE system with an arbitrary initial condition vector

或更常见的

Equation giving the solution of an ODE system with an initial condition vector for linear, constant coefficient problems

但由于初始时间通常为零,因此基本矩阵的倒数通常被省略,因为它是零(即expm(zeros(n)) == eye(n))和c矢量的线性,常系数问题的同一性。相当于初始条件向量。

将符号声明附近的一些线交换到此

 syms t x_0 c_0
 hom             = expm(A*t)                ;
 invhom          = inv(hom)                 ;
 invhom_0        = subs(invhom,t,sym(t0))   ;
 c_0             = invhom_0 * x_0           ;
 hom_initialcond = hom    * c_0             ;

应为非零初始时间提供正确的解决方案。