在 Octave 中使用 ode45 求解 ODE

问题描述

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

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

初始条件 x(0) = 0.5

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

enter image description here

我认为带红叉的图代表的是 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 ")

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

解决方法

ode45 期望 ODE 函数具有顺序为 (time,state) 的参数,正好相反。你有效地做的是对 t-t^2/2 进行积分,结果函数 0.5+t^2/2-t^3/6 就是你在图中得到的。