## Syntax

These differential equations can be solved analytically but practice with numerical solvers is kind of a cool thing.

Anyway, the syntax for the ode solver is pretty straight forward and standard for the different solvers:

ode23 (@fun, [t_0 t_1], initial, options)

with the inputs, in order from left to right, being a **Matlab function**, time interval, initial conditions and options (which is a place for specifying precision and some other stuff). What really needs to be emphasized is that this functional input **needs** to be a **Matlab function** stored in the **current working directory **as a **.m file** that **has the same name as the Matlab function**.** **Matlab has kind of a slick way of brushing that under the carpet in their help file but the point is that your DE(s) need(s) to be stored that way; meaning what? You have to use a combination of the command line and .m file editor to solve your differential equation. An example function:

%Matlab function f.mfunction dy = f(t,y) k1 = 2; k2 = .5; dy = zeros(2,1); dy(1) = -k1*y(1); dy(2) = -k2*y(2) + k1*y(1); end

Here we are solving and . Save this as **f.m**. Then solve the system at the command prompt:

ode23(@f, [0 20], [1 0])

A quick check, if this is not working have you:

- opened and saved your function containing your DE(s) as
**f.m** *cd*at the Matlab command prompt.- been prompted to “Add to path”, “Make current path”, etc? Adding to the path is easiest.
- any variables that you were using previous to this that would have the names “y” , “t”, “k1” or “k2”? Try
*clear variables*at the command prompt. - copied and pasted the code. Sometimes it’s easy to small stuff.

Probably the oddest part of this form is the ‘@’ call. This is used for designating a function, so if our differential equation is represented by function *fun* then *@fun* tells Matlab to use the **saved **function *fun*. This is probably the way that Matlab separates a variable and function by syntax. There’s more to be said about this, if your interested skip down the page to Functions.

## Functions

Functions in Matlab must be saved as **.m files**. This is a unique feature in Matlab and is mostly a consequence of the fact that Matlab is hardcore numerical. There are a couple of different kinds of functions; specifically I will only talk about anonymous and “regular” functions (sorry about that, I really did try to find out what Matlab called these things… I just didn’t see anything about it).

Regular functions are those which must be saved in **.m** files… which suggests that there are some functions that don’t have to be saved in **.m **files. Ok, so I lied… but for good reason: in ALMOST all cases functions must be saved as **.m** files and while it is inaccurate to say ALL functions must be saved as .m files, there are VERY few cases where anonymous functions can be used. However, anonymous functions are a slick way to get something done quickly. For the differential equation:

We would have to build a **.m file** for something very very simple. The .m file:

%Matlab function simple.m function dy = simple(t,y) k = 1; dy = -k*y end

The fact that you would have to generate this .m file for something so simple is kind of frustrating, huh? If you’re not convinced take a look at how much work you would have to go through to try and change your k for say 50 or 100 values of k. That’s a lot of clicks. Wouldn’t be great if there was something that allowed you to do all of this from the command prompt without the accessory .m file? Anonymous functions will let you. In the command prompt:

%command line k = 1; dy = @(t,y) -k*y; ode23(dy, [0 20], 1)

Ok, that wasn’t that great. Let’s do something better:

%command line for k=.5:.01:1.5 dy = @(t,y) -k*y; ode23(dy, [0 20], 1) hold on end

Here’s what I got:

Ha… cool. That’s it.

## Leave a Reply