Discrepancy with easy ODE.

4 visualizzazioni (ultimi 30 giorni)
Bobby Fischer
Bobby Fischer il 14 Gen 2021
Commentato: Bobby Fischer il 14 Gen 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 Commenti
Mischa Kim
Mischa Kim il 14 Gen 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 il 14 Gen 2021
Thanks Mischa. All clear.

Accedi per commentare.

Risposta accettata

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

Più risposte (0)

Categorie

Scopri di più su Historical Contests in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by