[Math] Runge Kutta Method Matlab code

analysisMATLABnumerical methodsordinary differential equations

So I have a programming assignment with the following instructions:

Consider the nth-order differential equation $$Ax^n (t) = x ^{(n-1)}(t) +
x^{(n-2)}(t) + … + x(t)$$ where $A$ is a real-valued input parameter.
Write a Matlab program to solve this equation using the Runge-Kutta
method of order $4$. Your program cannot use the matlab built-in
functions for solving differential equations.

There are more instructions but I'm not looking for the answer so It's not relevant. I was told that $A$ is a scalar. and our function is supposed to have the form

"The file should be a function of the form function $$\texttt{Xout = RKxx(n, T0
Tfinal, A, h, X)}.$$ The inputs are $n$ (a positive that is the order of
the equation), $T_0$ (the initial starting time), $T_{\mathrm{final}}$ (the ending
time), $A$ (the value of $A$), $h$ (the step-size) and $X$ (a vector of length
$n$ with the initial values of \begin{align}X(1) &= x(t_0),\\ X(2) &= x^{(1)} (t_0),\\ &\vdots\\ X(n) &= x^{(n−1)}(t_0).\end{align} The output is just the approximated value of $x(t)$ at $T_{\mathrm{final}}$."

But I know we first have to make it into a first-order ODE but I'm not sure how to go about doing that if $A$ is a scalar.. are we supposed to divide by it?

Please help!

Best Answer

You are exactly correct in your assumption. You should make this into a system of first-order ODEs. The straight forward way to transform this equation into a first order ODE is to create the vector $X(t) \in \mathbb{R}^{n}$ where each element is defined as $X(i) := x^{(i)}(t)$.

Then you have a first-order equation defined as

$$X' = M X$$

where $M \in \mathbb{R}^{nxn}$. You can fully describe $M$ noting that for every $i$, by construction:

$$(X(i-1))' = (x^{(i-1)}(t))' = x^{(i)}(t) = X'(i)$$

This defines $n-1$ equations, with the last equation in $M$ being your original ODE re-written in terms of $X$. Note also that you have $X(t_0)$ as an input. This is all the information you need to solve your ODE using RK4.