Graph can not show correct point

1 view (last 30 days)
Carey n'eville
Carey n'eville on 28 Dec 2020
Edited: Mischa Kim on 28 Dec 2020
Dear friends, I have code below but when I run the code, it gives me this graph. But it should colour the graph at point x=10, x=20 and x=40. I guess it shift and there is a problem. How could I fix it?
CODE:
XS0=[2, 1000];
tspan = [0 40];
[t,XS]=ode45(@BSfunction, tspan, XS0);
figure
hold on
plot(t,XS(:,1),t,XS(:,2))
plot(t(1:10),XS(1:10,1),'r',t(10+1:20),XS(10+1:20,1),'b', t(20+1:end),XS(20+1:end,1),'g')
xlabel('time (year)')
ylabel('Concentration (mg/L)')
function dXSdt=BSfunction(~,XS)
X = XS(1); S = XS(2);
X0=2;
Sin=1000;
So=0;
S0=So+Sin;
V=20;
TH=40;
ko=0.2;
kd=0.01;
Ks=150;
Y=0.5;
S=S0-((ko/Y)*X0*TH);
biomassgrowth=((ko*S*X)/(Ks+S))-(kd*X);
substratutilize = -((ko/Y)*X0);
dXSdt = [biomassgrowth; substratutilize];
end

Answers (1)

Mischa Kim
Mischa Kim on 28 Dec 2020
Edited: Mischa Kim on 28 Dec 2020
Hi Carey, ode45() picks its own time steps to do the integration. However, you can prescribe the times at which you want integration results. E.g. use
tspan = linspace(0,40,41);

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!