Solving Ordinary Differential Equations (0DEs)

In this lesson, we will cover the following topics:

1. Solving First Order Ordinary Differential Equations (ODEs)

2. Solving Higher Order ODEs (Second Order, etc)

3. Solving Systems of ODEs

video coming soon

Solving First Order Ordinary Differential Equations (ODEs)

ODE’s are not typically introduced in an Introduction to MATLAB course, but knowing how to solve ODE’s in MATLAB is useful in higher level engineering classes and projects (or so I’ve seen). This is why I have covered this topic within this course – for those of you who have probably taken an introductory MATLAB course, but are having trouble solving ODE’s here. Now I won’t get into what an ODE is (like I have written in the title, it’s an Ordinary Differential Equation) because it’s covered in the entire Differential Equations course! For now, I will assume you have taken that course and have knowledge of what this is when it comes to the mathematical terms, and we’ll get right into learning how to actually solve ODE’s in MATLAB.

We will start with a first order differential equation – let’s try the following example, with the differential equation, the initial value, and the bounds given:

First we will create a function with an input of our initial y-value that’s given, and no output arguments. The output arguments don’t really matter in this case, since we are creating a graph to see how the values change over time with different values of x.

I will now add in a second function within the first one, and this will accept the inputs x and y, since we are given dy/dx. I named it dydx. Make sure you have an end line for each function.

Now we will add in (to Line 3) the code that uses ode45, which is the command that solves the differential equation. There are other commands as well (such as ode23), but ode45 is the one you will likely be using for the most part. I have added on the left side of the equals sign [x,y] since those are our given variables. On the right side of the equals sign, we will use ode45 with three inputs in the brackets. The first input is @deriv – you need the @ symbol followed by the name of the second function, which was deriv for us. The second input will be the limits you are solving between – for us this was [0 1]. The last input is the initial value given for the y-axis, which is y0. You will input the initial y-value given of 1, which will correspond to the first x-value from [0 1], which is 0. This covers the given initial condition that y(0) = 1.

Now on Line 8, I will add in the differential equation we were given, which is y*x + y. This is set equal to the output of the second function, dydx in our case.

Lastly, we add in the plot command to plot x vs. y on Line 5.

Now running the function, we will type the function name into the Command Window (recall that is how you run functions) with the input of y0 = 1, since our given conditions were y(0) = 1 (which means that when x = 0, y = 1).

This is the graph that comes up, plotting the solutions of the ODE with the input values of x within the boundaries of 0 to 1 that we had specified:

Now let’s solving higher order differential equations, rather than first order.

Solving Higher Order ODEs

Say we are given the following example:

Because this equation above is a second order ODE, we run into a bit of a problem – MATLAB’s ode45 function can only work with first order ODEs! But not to worry, we can actually convert these into two first order ODE’s rather than one second order ODE.

The first thing we need to do is move over all the terms to the right hand side of the equation, leaving only the second order derivative term on the left side. It will look like this:

Now we can’t keep the second derivative term (d^2y/dx^2) so we need to change it to something that is a derivative less than second order. This is where we bring in another variable, let’s call it z. This variable will be the first derivative of y, which is the variable we were differentiating. This is what the variable z will equal:

Now taking the second derivative of dy/dx will give us the second order term we had above, which we wanted to get rid of. This is where we bring in the z, and set dz/dx equal to the d^2y/dx^2 term from earlier. Since the first derivative of y, dy/dx, is just z, then the second derivative of it will be dz/dx:

Now we can use the same steps as the first order ODE above to solve! Rewriting the question down here with our changed equations to incorporate z gives:

Say we were given the following conditions as well (same as the conditions in the first example):

Now using the same code that I had in the example with the first order ODE, I can replace all the y’s with z’s and replace the equation there with the equation we have (I’m changing these lines, circled from the screenshot above):

After making the changes to reflect z instead of y, as well as the correct equation for this question, we get this:

This is what you would type to run it in the Command Window with the condition given, y0 = 1.

This is the output graph:

Solving Systems of ODEs

Now sometimes we will be given multiple Ordinary Differential Equations, and we have to solve the whole system! Let’s try an example, if we were given these two ODE’s:

We now put this into MATLAB. The first line creates y(t) and x(t) as symbolic variables, so when we use them later on without defining them to actual numbers, it won’t give us an error. x and y are functions of time (these variables change relative to the time) so that’s why we define them as x(t) and y(t):

Now we define the equations in the second line. We set them equal to the variable eqns to the left of the equals sign. On the right side of the equation, we use square brackets to create a vector (this is just the way it’s solved), and within the square brackets we have two parts – one for dy/dt, one for dx/dt. The first part is diff(y,t) == y^2+x. We are defining the differential equation dy/dt by using diff(y,t), then using the assignment operator, == , to assign y^2+x to dy/dt. This makes the first equation! Then I have repeated this for the second term within the square brackets – for dx/dt. I also used a semicolon at the end of the line so this is not shown in the final answer when we run the code.

In the third and last line, we set the left side of the equation to [ySol(t),xSol(t)], and the right side to dsolve(eqns).

[ySol(t),xSol(t)] will take the specific x- and y-values from dsolve (if you don’t put this on the left side of the equals sign, you won’t get the actual answer).

dsolve(eqns) is used to solve the set of equations, where you type in the variable that you had set the differential equations equal to in Line 2. That variable for us was eqns, which was why we use dsolve(eqns).

This is what shows in the Command Window as the output when the code is run: The second solution xSol is really long, and also uses the Bessel functions (eg. Besselj) within it. I won’t go in-depth with what all of it means, but that will be your final answer: