Filter löschen
Filter löschen

reorganize plot for runge kutta

3 Ansichten (letzte 30 Tage)
Mariam Gasra
Mariam Gasra am 20 Mai 2019
Kommentiert: Mariam Gasra am 20 Mai 2019
% 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?

Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 20 Mai 2019
Bearbeitet: Sulaymon Eshkabilov am 20 Mai 2019
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 Kommentar
Mariam Gasra
Mariam Gasra am 20 Mai 2019
how can i correct it?
i am a new user in matlab

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by