4th order Runge-Kutta Problem in Special ranges

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

Answers (1)

Change
y(0) = -1;
to
y(1) = -1;
Matlab indices start at 1 not zero.

12 Comments

I did it but doesn't work.
Please explain "doesn't work" for us. Did you get an error? An answer you didn't expect? Or ...?
According to the assignment, the x values should range between 0.0 and 0.6, so maybe you should be using this:
x = 0:h:0.6;
ans =
Empty state-space model.
Sir, I changed it but still same answer. x = 0:h:0.6;
I don't know what your "Empty state-space model" message means in the context of the code you have posted thus far.
When I make these changes to your code:
y(1) = -1;
x = 0:h:0.6;
and run your code, this
plot(x,y)
produces this:
It comes out in the same way, but the answer must be numerical. For example answer is: =28.9306
The answer is in the y vector, which is numerical. I still don't understand your problem. E.g.,
>> x
x =
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000
>> y
y =
-1.0000 -0.9145 -0.8562 -0.8225 -0.8110 -0.8196 -0.8464
Sir, I added example about it.
This is a different DE than you originally posted. Also your arithmetic is wrong. E.g., your very first k1:
k1 = h * f(x0,y0) = 1 (x0 + 2y0 + 1) = 1*(0+2*2+1) = 5
But you have erroneously written 1*(0+2+1) = 3
So you need to re-examine all of your arithmetic for this.
Sir, i added another different example.
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.
Sir, thank you so much. The answer is probably like you said.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 18 Jan 2021

Commented:

on 18 Jan 2021

Community Treasure Hunt

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

Start Hunting!