Discrepancy with easy ODE.

1 次查看(过去 30 天)
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 个评论
Mischa Kim
Mischa Kim 2021-1-14
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 2021-1-14
Thanks Mischa. All clear.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by