필터 지우기
필터 지우기

reorganize plot for runge kutta

조회 수: 3 (최근 30일)
Mariam Gasra
Mariam Gasra 2019년 5월 20일
댓글: Mariam Gasra 2019년 5월 20일
% It calculates ODE using Runge-Kutta 4th order method
% Author Ido Schwartz
clc; % Clears the screen
clear all;
h=0.1; % step size
x = 0:h:10; % Calculates upto y(3)
y = zeros(1,length(x));
y(1) = 0.2; % initial condition
F_xy = @(t) (0.32)+((0.24)*exp(-t))-2*(0.04*exp(-2*t)); % change the function as you desire
for i=1:(size(x)-1) % calculation loop
k_1 = F_xy(x(i),y(i));
k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; % main equation
end
% validate using a decent ODE integrator
tspan = [0,1]; y0 = 0;
[tx, yx] = ode45(F_xy, tspan, 0.5);
plot(x,y,'o-', tx,
How to make the equation start from zero then stop in the case steady state?

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 5월 20일
편집: Sulaymon Eshkabilov 2019년 5월 20일
Hi,
In your code there are several flaws:
(1) In your RK code: y(0) = 0.2 and in validation ode45 (validation part) y0 = 0.5. This has to be fixed.
(2) In your RK code: you're simulating within [0, 10] and in validation ode45 (validation part) within [0, 1]. It is better to make them unique. Moreover, in validation part, ode45 takes a variable step, but for comparison puposes, it is better to have a fixed step of size 0.1 as you set h=0.1 in part - RK.
To start your simulation, already your starting at x =0 or you wish to set the initial condition set at 0, then y(0) has to be set at 0 not at 0.2 or 0.5. Moreover, to stop simulation at steady state, you need to intoruduce error tolerance calculation in your code and employ break operator.
Good luck.
  댓글 수: 1
Mariam Gasra
Mariam Gasra 2019년 5월 20일
how can i correct it?
i am a new user in matlab

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

카테고리

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