Solving problem with ODE45

조회 수: 1 (최근 30일)
Yi Li
Yi Li 2021년 10월 17일
댓글: Yi Li 2021년 11월 8일
clc;
clear all;
close all;
[t1,y1] = ode45(@vdp1,[0 20],[2; 0]);
t2(1,:)=t1(1,:);
y2(1,:)=y1(1,:);
figure
plot(t1,y1(:,1),'-o',t1,y1(:,2),'-o')
for i=2:size(t1)
[a,b]=ode45(@vdp1,[0 t1(i) 2*t1(i)],y2(i-1,:));
t2(i,:)=a(2,:);
y2(i,:)=b(2,:);
end
figure
plot(t2,y2(:,1),'-o',t2,y2(:,2),'-o')
Hey guys, when I am solving a function step by step with ODE45, the result is completely different from the direct solution. Can someone tell me why?
  댓글 수: 2
Jan
Jan 2021년 10월 17일
편집: Jan 2021년 10월 17일
Note: for i=2:size(t1) is fragile. size() replies a vector, but the colon operator uses the first element only. Better: for i=2:size(t1, 1) or for i=2:numel(t1).
Please provide vdp1 to allow use to run your code.
Yi Li
Yi Li 2021년 10월 17일
편집: Yi Li 2021년 10월 17일
Thank you!
function dydt = vdp1(t,y)
%VDP1 Evaluate the van der Pol ODEs for mu = 1
%
% See also ODE113, ODE23, ODE45.
% Jacek Kierzenka and Lawrence F. Shampine
% Copyright 1984-2014 The MathWorks, Inc.
dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 10월 17일
In the loop you are calculating the integration from 0 to 2*t(i) with the initial value set to y2(i-1, :). But this is another integration, because you start from t=0 with another initial value. Instead of the time interval [0 t1(i) 2*t1(i)] the interval [t1(i-1), t1(i)] would create the same output:
for i = 2:numel(t1)
[a,b] = ode45(@vdp1, [t1(i-1), t1(i)], y2(i-1, :));
t2(i,:) = a(end, :);
y2(i,:) = b(end, :);
end
figure
plot(t2, y2(:, 1), '-o', t2, y2(:, 2), '-o');

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by