Runge Kutta for system of differential equations

Hello,
I have to solve the following system with method of Runge Kutta:
y1' = y2
y2'=f(x,y1,y2) with y1(0)=0 and y2(0)=y20
where f(x,y1,y2) = -axy1-y1, a=0.03 and y20 = 0.25

6 Comments

What have you done so far? What specific problems are you having with your code? Do you have to manually code the RK method or are you allowed to use ode45?
Thanks for your interest.
Here is the task with corrections:
y1' = y2
y2'=f(x,y1,y2) with y1(0)=0 and y2(0)=y20
where f(x,y1,y2) = -axy2-y1, a=0.03 and y20 = 0.25
And here is my solution. What is im not sure about is x = 0 : h : 2*pi; and figure(2), plot(x, y(1,:), x, ??, ‘*’)
from code and if my code is correct at all. I have been solved similar task but without any given , a=0.03 and y20 = 0.25.
Function main
h = pi/10;
x = 0 : h : 2*pi;
n = length(x);
y(1,1)=0; y(2,1) = 0,25;
for k = 1: (n-1)
k1 = h * rung(x(k), y(1:2,k));
k2 = h * rung(x(k) + h/2; y(1:2,k) + k1/2);
k3 = h * rung(x(k) + h/2; y(1:2,k) + k2/2);
k4 = h * rung(x(k) + h; y(1:2,k) + k3);
y(1:2,k+1)=y(1:2,k) + (k1 + 2*k2 + 2*k3 + k4)/6;
end
figure(1), plot(x, y(2,:)) %everything from second row
figure(2), plot(x, y(1,:), x, ??, ‘*’)
function f=rung(x,y)
f(1,1) = y(2);
f(2,1) = -0,03*x*y(2) – y(1);
@Melda: Fine. Now what exactly is your question?
I already mentioned that im not sure about
1) x = 0 : h : 2*pi; and
2)figure(2), plot(x, y(1,:), x, ??, ‘*’).
Sooo the question is what have to be there where is the question marks and is this is correct x = 0 : h : 2*pi
or is there another way to write 2)
I cannot know, what you want to insert for the questionmarks in
plot(x, y(1,:), x, ??, ‘*’)
because you did not explain, what you want to achieve. x=0:h:2*pi can be correct of false depending on what you want as values for x. It is impossible to decide if something is correct or not only based on the code. Knowing its purpose is essential.
By the way, use single quotes instead of the apostrophes:
plot(x, y(1,:), x, ??, ‘*’)
% ^ ^ Use '
Oh yees, thank u for apostrophes.
In the example that i have instead of question marks is the exact solution. But here in this example i dont know the exact solution what is. Also i dont know if without exact solution will be correct.

Sign in to comment.

Answers (0)

Asked:

on 2 May 2019

Commented:

on 3 May 2019

Community Treasure Hunt

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

Start Hunting!