MATLAB Answers

# Runge Kutta 4th Order Example

21 views (last 30 days)
Benjamin Moore on 19 Feb 2021
Answered: Alan Stevens on 19 Feb 2021
I am pretty confused how to even start solving this Runge Kutta 4th order example problem in MatLab. I do know that Runge Kutta formula below, but I dont understand how to apply this example to that formula in MatLab.
Formula:
Xn+1 = Xn +1/6(k1 + 2k2 +2k3 + k4)
k1 = f(xn) t
k2 = f(xn + 1/2k1) t
k3 = f(xn + 1/2k2) t
k4 = f(xn + k3) t
Example Problem:
x(double dot) + 9x + 0.1x 3 = 0 (This is a special equation called the Duffing oscillator)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Alan Stevens on 19 Feb 2021
First, write your equation as two first order equations; xdot = v, say, and vdot = -9xdot - 0.1x^3
Then define two functions fx = @(v) v; and fv = @(x,v) -9*v-0.1*x^3;
Now you need to define two sets of k1 to k4: k1x = fx(v), k1v = fv(x,v), etc.
Then you will have X(n+1) = X(n) + 1/6*(k1x + 2*k2x + 2*k3x + k4x) and V(n+1) = V(n) + 1/6*(k1v + 2*k2v + 2*k3v + k4v)
Put the whole lot in a loop representing the times for which you want the values.
If the above doesn't make any sense to you then you need to do some introductory Matlab training.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!