简体   繁体   English

Matlab微分方程系统

[英]Matlab system of differential equations

I have here a little program that I do not know how to tackle, I have previously written a code that would solve for me a system of two differential equations which works fine. 我这里有一个我不知道如何解决的小程序,我以前写过一个代码,可以为我求解两个很好的微分方程组。

However, I now have to solve a system of three differential equations: 但是,我现在必须解决一个由三个微分方程组成的系统:

d(alphai)/dt = ui = k * alphai*f(tethai) d(alphai)/ dt = ui = k * alphai * f(tethai)

d(tethai)/dt = alphai*f(tethai) - (tethai - tethaex) d(泰泰)/ dt = alphai * f(泰泰)-(泰泰-tethaex)

d(tethaex)/dt = sigma*(Sum(1.N)(tethai - tethaex)) - phi*tethaex d(tethaex)/ dt = sigma *(总和(1.N)(tethai-tethaex))-phi * tethaex

Here are my codes so far but I get some errors: 到目前为止,这是我的代码,但出现一些错误:

%Script for differential derivatives specifcation

   function dy = NRateDE(T,Y,p)
   %Extract parameters from P
   u0 = p(1);
   g = p(2);
   k = p(3);
   a = p(4);
   b = p(5);
   N = p(6);

   %Format output as a column vector
   dy = zeros(3,1);

   %Set differential equations
   dy(1) = (u0*exp(-g*T)) - (k*Y(1)*exp(Y(2)));
   dy(2) = Y(1)*exp(Y(2)) - (Y(2)) - Y(3);
A = Sum(Y,N);
dy(3) = (a .* A) - (b .* Y(3));
end

function A = Sum(Y,N)
    for i=1:N
        A = A + (Y(2) - Y(3));
    end
end

And finally here is the main function: 最后是主要功能:

%% Initialize the environment
close all;
clear all;
clc;

%%Define parameters and initial conditions
u0 = 0.12; g = 0; k = 0.09; N = 2; a = 0; b = 0.1; z = 0;A=0; %Parameters
MaxTime = 300;
T = 0:0.01:MaxTime; %integration time
Yinitial = 0; Yminitial = 0; %intial conditions

%%Solve and generate equations
%#Numerical approximation
    for z=1:N
        %Create random numbers to change the value of u0
        a1 = - 0.01;
        b1 = 0.01;
        r = (b1-a1).*rand(1) + a1;
        u0 = u0 +r; %value changes on each iteration
        [T,Y] = ode23s(@NRateDE,[T],[Yminitial Yinitial],[],[u0 g k a b N A]);
    end   
%#Analytical approximation
    tau = 0:0.2:MaxTime;              
    tetha = (u0*exp(-g*tau))/k;
    alpha = u0*exp(-g*tau).*exp(tetha);

With N = 2, I should get two set of matrices of [T,Y] but I have no clue of how should I correct the code. 在N = 2的情况下,我应该获得两组[T,Y]的矩阵,但是我不知道如何纠正代码。 I am fairly new to Matlab so please forgive any silly mistake haha'. 我对Matlab相当陌生,所以请原谅任何愚蠢的错误哈哈。

Any idea, hint? 有任何想法吗?

Thank you for your help in advance! 提前谢谢你的帮助!

EDIT: I Have slightly changed the first function and the "RateDE" was not intentional. 编辑:我已经稍微改变了第一个功能和“ RateDE”不是故意的。 Also here is the error message: 这也是错误消息:

Attempted to access Y(3); 试图访问Y(3); index out of bounds because numel(Y)=2. 索引超出范围,因为numel(Y)= 2。

Error in NRateDE (line 18) dy(2) = Y(1)*exp(Y(2)) - (Y(2)) - Y(3); NRateDE(第18行)中的错误dy(2)= Y(1)* exp(Y(2))-(Y(2))-Y(3);

Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); odgeguments中的错误(第88行)f0 = feval(ode,t0,y0,args {:}); % ODE15I sets args{1} to yp0. %ODE15I将args {1}设置为yp0。

Error in ode15s (line 149) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... ode15s中的错误(第149行)[neq,tspan,ntspan,next,t0,tfinal,tdir,y0,f0,odeArgs,odeFcn,...

Error in RunRateNparticles (line 20) [T,Y] = ode15s(@NRateDE,[T],[Yminitial Yinitial],[],[u0 gkab NA]); RunRateNparticles中的错误(第20行)[T,Y] = ode15s(@NRateDE,[T],[Yminitial Yinitial],[],[u0 gkab NA]);


Right! 对!

I understand the error messages and have corrected them by adding an element on Y: 我了解错误消息,并通过在Y上添加元素来更正了它们:

[T,Y] = ode23s(@NRateDE,[T],[Yminitial Yinitial Ylinitial],[],[u0 g k a b N A]);

However, I am still unable to solve the set of three differential equations given on the first message. 但是,我仍然无法解决第一条消息中给出的三个微分方程组。

Here are the current error messages: 这是当前的错误消息:

Undefined function or variable "A". 未定义的函数或变量“ A”。

Error in NRateDE (line 19) A = A + (Y(2) - Y(3)); NRateDE中的错误(第19行)A = A +(Y(2)-Y(3));

Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); odgeguments中的错误(第88行)f0 = feval(ode,t0,y0,args {:}); % ODE15I sets args{1} to yp0. %ODE15I将args {1}设置为yp0。

Error in ode23s (line 120) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... ode23s中的错误(第120行)[neq,tspan,ntspan,next,t0,tfinal,tdir,y0,f0,odeArgs,odeFcn,...

Error in RunRateNparticles (line 20) [T,Y] = ode23s(@NRateDE,[T],[Yminitial Yinitial Ylinitial],[],[u0 gkab NA]); RunRateNparticles(第20行)中的错误[T,Y] = ode23s(@NRateDE,[T],[Yimitial Yiinitial Ylinitial],[],[u0 gkab NA]);

The solution to the original problem is outlined in your own answer already. 您自己的答案中已经概述了原始问题的解决方案。
Y needs to have 3 elements, so you can just add another initial condition. Y需要具有3个元素,因此您可以添加另一个初始条件。

The new problem is a missing assignment. 新问题是缺少作业。 The Sum function tries to add something to A , but this variable doesn't exist yet. Sum函数尝试向A添加一些内容,但该变量尚不存在。 Fixing your function like this should work: 像这样修复您的功能应该可以:

function A = Sum(Y,N)
    A = 0;
    for i=1:N
        A = A + (Y(2) - Y(3));
    end
end

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

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