Discrepancy with easy ODE.

2 vues (au cours des 30 derniers jours)
Bobby Fischer
Bobby Fischer le 14 Jan 2021
Commenté : Bobby Fischer le 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 commentaires
Mischa Kim
Mischa Kim le 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 le 14 Jan 2021
Thanks Mischa. All clear.

Connectez-vous pour commenter.

Réponse acceptée

J. Alex Lee
J. Alex Lee le 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
  1 commentaire
Bobby Fischer
Bobby Fischer le 14 Jan 2021
Oh. my. G.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by