Loop to iterate ode45 function

조회 수: 44 (최근 30일)
Hami the Penguin
Hami the Penguin 2021년 3월 7일
댓글: Hami the Penguin 2021년 3월 7일
Hello, I'm having some issue using the ode45 function. In the function F, I assigned e = 0, but I want to make a loop that assigns e = 0:0.2:1 and use the ode45 function to plot the equation for each e in one plot. The code below is what I currently have, but I keep on getting errors when I try looping the function. Any ideas?
%e = 0:0.2:1;
[t,x]=ode45(@F,[0,20],[0,1]);
plot(t,x(:,1))
function xp = F(t,x)
e = 0;
xp = zeros(2,1);
xp(1) = x(2);
xp(2) = -x(1) - e*x(1)^3;
end

채택된 답변

Jan
Jan 2021년 3월 7일
편집: Jan 2021년 3월 7일
figure;
axes('NextPlot', 'add');
for e = 0:0.2:1
[t, x] = ode45(@(t, y) F(t, y, e), [0, 20], [0, 1]);
plot(t, x(:,1))
end
function xp = F(t, x, e)
xp = zeros(2,1);
xp(1) = x(2);
xp(2) = -x(1) - e*x(1)^3;
end
Here you create an "anonymous function", which appends the 3rd input argument e.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by