Discrepancy with easy ODE.

조회 수: 2 (최근 30일)
Bobby Fischer
Bobby Fischer 2021년 1월 14일
댓글: Bobby Fischer 2021년 1월 14일
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