【发布时间】: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 ")
如果你们中的任何人都可以帮助我编写这段代码,我们会很高兴。
【问题讨论】: