4th order Runge-Kutta Problem in Special ranges

2 Ansichten (letzte 30 Tage)
Rooter Boy
Rooter Boy am 18 Jan. 2021
Kommentiert: Rooter Boy am 18 Jan. 2021
I want to solve this question below in Matlab but i didn't do it. This is simple question but i can't do it. If someone will help me, i will be very happy.
Question: Write a Matlab code that finds the approximate solution of the initial-value problem y '= - 2x-y, y (0) = - 1 using the 4th order Runge-Kutta method for the h = 0.1 step interval in the range [0.0,0.6].
Note: The 4th Order Runge-Kutta method is expressed as follows;
..................................................................................................................................................
Original Question:
I tried this code block, but it didn't work:
clc; % Clears the screen
clear all;
h=0.1; % step size
x = 0:h:3; % Calculates upto y(3)
y = zeros(1,length(x));
%y(0) = [0.0;0.6]
y(0) = -1; % redo with other choices here. % initial condition
F_xy = @(x,y) -2*x-y; % 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

Antworten (1)

Alan Stevens
Alan Stevens am 18 Jan. 2021
Change
y(0) = -1;
to
y(1) = -1;
Matlab indices start at 1 not zero.
  12 Kommentare
James Tursa
James Tursa am 18 Jan. 2021
So when I make these changes
h=0.1;
x = 0:h:0.6;
y(1) = 0.2;
F_xy = @(x,y) y-y^2;
and run your code I get the following:
>> y(2)
ans =
0.2165
So, again, it looks like things are working to me.
Rooter Boy
Rooter Boy am 18 Jan. 2021
Sir, thank you so much. The answer is probably like you said.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by