【问题标题】:Solving an ODE using ode45 in Octave在 Octave 中使用 ode45 求解 ODE
【发布时间】:2021-08-05 21:52:30
【问题描述】:

我正在尝试使用 Octave 解决以下 ODE,尤其是函数 ode45。

dx/dt = x(1-x/2), 0

初始条件 x(0) = 0.5

但我得到的图表不是我所期望的。

我认为带红叉的图表代表 x' vs x 而不是 x vs t。

代码如下:

clear all
close all
% Differential Equation: x' = x(1-x/2)
function dx = f(x,t)
   dx = x*(1-x./2);
endfunction 
% Exacte Solution: 2*e^t/(3+e^t) 
function xexac =solexac(t)
xexac =  (2*exp(t))./(3+exp(t));
endfunction
x0=0.5;   %%Initial condition
T=10;            %% maximum time T
t=[0:0.1:T];              %% we choose the times t(k) where is calculated 'y'  
sol=ode45(@f,[0,T],x0);   %% numerical solution of (E)
tt=sol.x;y=sol.y;         %% extraction of the results
clf;hold on  ;            %% plot the exact and numerical solutionss
plot(tt,y,'xr')
plot(t,solexac(t),'-b') 
xlabel('t')
ylabel('x(t)')
title('Chemostat Model')
legend("Numerical Solution","Exacte Solution ")

如果你们中的任何人都可以帮助我编写这段代码,我们会很高兴。

【问题讨论】:

    标签: octave numerical-methods


    【解决方案1】:

    ode45 期望 ODE 函数的参数顺序为 (time, state),因此正好相反。您实际上所做的是整合t-t^2/2,而生成的函数0.5+t^2/2-t^3/6 就是您在图中得到的。

    【讨论】:

    • 谢谢!我发现了错误。我应该写 f(t,x) 而不是 f(x,t)
    猜你喜欢
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多