Discrepancy with easy ODE.

4 views (last 30 days)
Bobby Fischer
Bobby Fischer on 14 Jan 2021
Commented: Bobby Fischer on 14 Jan 2021
Hello, don't know why the discrepancy.
function fcno
y0=linspace(0,2,20);
t0=linspace(0,3,100);
figure(1)
close(1)
figure(1)
options=odeset('RelTol',1e-5);
subplot(1,3,1)
hold on
for k=1:20
[t,y]=ode45(@fun,t0,y0(k),options);
plot(t,y,'b')
end
title('ode')
subplot(1,3,2)
hold on
t=t0;
for k=1:20
f2=y0(k)*exp(t)./(1+(exp(t)-1)*y0(k));
plot(t,f2,'r')
end
title('solution')
%%
lon=101;
t=linspace(0,3,lon);
h=3/(lon-1);
y0=linspace(0,2,20);
subplot(1,3,3)
hold on
for m=1:20
y=zeros(1,lon);
y(1)=y0(m);
for k=1:100
y(k+1)=y(k)+h*y(k)*(1-y(k));
end
plot(t,y,'g')
end
plot([0 3],[0 0],'m--','linewidth',3)
plot([0 3],[1 1],'m--','linewidth',3)
title('my ode')
%%
function [t,yp]=fun(t,y)
yp=y*(1-y);
end
end
  2 Comments
Mischa Kim
Mischa Kim on 14 Jan 2021
Hi Bobby, can you please provide more information of what you are trying to accomplish and the (unexpected) results you receive?
Bobby Fischer
Bobby Fischer on 14 Jan 2021
Thanks Mischa. All clear.

Sign in to comment.

Accepted Answer

J. Alex Lee
J. Alex Lee on 14 Jan 2021
You have a syntax error on what your odefun is returning. To fix:
function [yp]=fun(t,y)
yp=y*(1-y);
end

More Answers (0)

Categories

Find more on Historical Contests 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!