RK4 for a system of 2 second order DEs

In summary, the conversation discusses the design of a MatLab/Octave code to calculate and plot the orbit of a satellite. It also involves converting a set of second order equations into a system of four first order differential equations and using the classical 4th order Runge-Kutta method to approximate the position of the satellite over time. The code provided in the conversation uses initial values and a for loop to iterate through the calculations and plot the orbit.
  • #1
aoeuDvorakhtn
2
0
1. For this problem, we are asked to design a MatLab/Octave code that will calculate and plot the orbit of a sattelite



2. We are given the following 2nd order equations
[itex] x''= \frac{-x}{( \sqrt{x^{2}+y^{2}})^{3}}[/itex]
[itex]y''= \frac{-y}{( \sqrt{x^{2}+y^{2}})^{3}}[/itex]

We can convert this into the following system of 4 1st order DEs by substitiuting z=x' and w=y'
[itex]x'=z[/itex]
[itex]y'=w[/itex]
[itex]z'= \frac{-x}{( \sqrt{x^{2}+y^{2}})^{3}}[/itex]
[itex]w'= \frac{-y}{( \sqrt{x^{2}+y^{2}})^{3}}[/itex]

We use the following initial vallues
[x0 y0 z0 w0] = [4 0 0 0.5]


The Attempt at a Solution


I managed to build a program that works well enough that, after adding annotations, I could hand it in. However, as you can see below, the lines within the for loop could use a little help as far as elegance goes (I've attached the .m file if my copy/paste is too difficult to read). Ideally, I'd like to be able to make those lines a little more compact. Any ideas?

X0=[4 0 0 0.5];
t0=0;
tmax=50;
h=0.25;
SOL=[t0 X0;];
f1=inline('z','z');
f2=inline('w','w');
f3=inline('-x/((sqrt(x^2+y^2))^3)','x','y');
f4=inline('-y/((sqrt(x^2+y^2))^3)','x','y');
for i = 1:tmax/h
g1=[f1(X0(3)) f2(X0(4)) f3(X0(1),X0(2)) f4(X0(1),X0(2))];
g2=[f1(X0(3)+0.5*h*g1(3)) f2(X0(4)+0.5*h*g1(4)) f3(X0(1)+0.5*h*g1(1),X0(2)+0.5*h*g1(2)) f4(X0(1)+0.5*h*g1(1),X0(2)+0.5*h*g1(2))];
g3=[f1(X0(3)+0.5*h*g2(3)) f2(X0(4)+0.5*h*g2(4)) f3(X0(1)+0.5*h*g2(1),X0(2)+0.5*h*g2(2)) f4(X0(1)+0.5*h*g2(1),X0(2)+0.5*h*g2(2))];
g4=[f1(X0(3)+h*g3(3)) f2(X0(4)+h*g3(4)) f3(X0(1)+h*g3(1),X0(2)+h*g3(2)) f4(X0(1)+h*g3(1),X0(2)+h*g3(2))];
X1=X0+(h/6)*(g1+2*g2+2*g3+g4);
t1=t0+h;
SOL=[SOL;t1 X1];
X0=X1;
t0=t1;
end
plot(SOL(:,2),SOL(:,3))
axis('equal')
 

Attachments

  • rkorbit2.m
    769 bytes · Views: 378
Last edited:
Physics news on Phys.org
  • #2
In case anyone is interested, I did figure something out. It added a few lines to the code, but it did make the loop a bit prettier.

%sets initial position and velocity values for x and y
%(z and w are the velecities of x and y, respectively)
X0=[4 0 0 0.5];
%initializes the time variable (x, y, z, and w are functions of time)
%and sets a maximum t for which to approximate
t0=0;
tmax=50;
%stepsize
h=0.25;
%initializes array in which to store approximated valies.
SOL=[t0 X0];
%these inline functions are our system of 4 1st order DEs, x', y', z', and w'
f1=inline('z','z');
f2=inline('w','w');
f3=inline('-x/((x^2+y^2)^1.5)','x','y');
f4=inline('-y/((x^2+y^2)^1.5)','x','y');
%approximates x, y, z, and w in increments of 0.0025 from t=0 to t=50 using the
%classical 4th order Runge-Kutta method
for i = 1:tmax/h
g1=[f1(X0(3)) f2(X0(4)) f3(X0(1),X0(2)) f4(X0(1),X0(2))];
k1=X0+0.5*h*g1;
g2=[f1(k1(3)) f2(k1(4)) f3(k1(1),k1(2)) f4(k1(1),k1(2))];
k2=X0+0.5*h*g2;
g3=[f1(k2(3)) f2(k2(4)) f3(k2(1),k2(2)) f4(k2(1),k2(2))];
k3=X0+h*g3;
g4=[f1(k3(3)) f2(k3(4)) f3(k3(1),k3(2)) f4(k3(1),k3(2))];
X1=X0+(h/6)*(g1+2*g2+2*g3+g4); %RK4
t1=t0+h;
SOL=[SOL;t1 X1];
X0=X1;
t0=t1;​
end
%plots our approximation to the position functions, x and y and sets the axes
%to use equal scales
plot(SOL(:,2),SOL(:,3))
axis('equal')
 
Last edited:

What is RK4 for a system of 2 second order DEs?

RK4 (Runge-Kutta fourth order) is a numerical method used to solve systems of differential equations (DEs). It involves using a series of steps to approximate the solution to the DEs, making it a useful tool for complex systems that cannot be solved analytically.

How does RK4 work for a system of 2 second order DEs?

RK4 works by breaking down the second order DEs into a system of first order DEs, which can then be solved using a set of equations and initial conditions. The method involves calculating four different approximations at each step, using the previous approximations to improve accuracy.

What are the advantages of using RK4 for a system of 2 second order DEs?

RK4 is a widely used and reliable method for solving systems of DEs. It is relatively simple to implement and provides accurate results for a wide range of problems. It also allows for adaptive step sizes, making it useful for systems with varying dynamics.

What are the drawbacks of using RK4 for a system of 2 second order DEs?

One drawback of RK4 is that it can be computationally expensive, requiring a large number of calculations to obtain accurate results. It also may not be suitable for stiff systems, where the solution changes rapidly over a small range of values.

Are there any alternatives to using RK4 for a system of 2 second order DEs?

Yes, there are several other numerical methods for solving systems of DEs, such as Euler's method, the midpoint method, and the Runge-Kutta fifth order method. Each method has its own advantages and limitations, and the choice of method will depend on the specific problem being solved.

Similar threads

  • Calculus and Beyond Homework Help
Replies
21
Views
847
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
962
  • Calculus and Beyond Homework Help
Replies
9
Views
975
Replies
6
Views
749
  • Calculus and Beyond Homework Help
Replies
2
Views
516
  • Calculus and Beyond Homework Help
Replies
1
Views
979
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
Back
Top