Merge three initial values ​​into one code by runge-kutta

조회 수: 5 (최근 30일)
Mariam Gasra
Mariam Gasra 2019년 5월 8일
답변: KSSV 2019년 5월 8일
h=0.5; % step size
x = 0:h:100; % Calculates upto y(3)
Y = zeros(1,length(x));
%y(1) = [0.2;0.3;0.2];
y(1) = 0.2
; % redo with other choices here.
% initial condition
F_xy = @(t,r) 3.*exp(-t)-0.4*r; % change the function as you desire
for i=1:(length(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,100]; y0 = -0.5;
[tx, yx] = ode45(F_xy, tspan, y0)
plot(x,y,'o-', tx, yx, '--')
how can i run this code for three initial value and plot it in same graph?

채택된 답변

KSSV
KSSV 2019년 5월 8일
h=0.5; % step size
x = 0:h:100; % Calculates upto y(3)
ibc = [0.2;0.3;0.2];
Y = zeros(length(ibc),length(x));
for j = 1:length(ibc)
y = zeros(1,length(x)) ;
y(1) = ibc(j) ;
% redo with other choices here.
% initial condition
F_xy = @(t,r) 3.*exp(-t)-0.4*r; % change the function as you desire
for i=1:(length(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
Y(j,:) = y ;
% validate using a decent ODE integrator
tspan = [0,100]; y0 = -0.5;
[tx, yx] = ode45(F_xy, tspan, y0) ;
plot(x,y,'o-', tx, yx, '--')
hold on
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by