Search code examples
matlabmathodescientific-computingdifferential-equations

using ode45 to solve y'=y adnd y'=t in Matlab


I first defined functions for dy/dt=y and dy/dt=t:

function dy=d(y):
    dy=y
end

function ddy=dd(t):
    ddy=t
end

And then I used ode45, respectively:

[t,y]=ode45('d',[1 10],1)
[t,y]=ode45('dd',[1 10],1)

which returns the following error: Error using d Too many input arguments.

My question is:

  1. Where did I go wrong?
  2. How does Matlab know whether y or t is the independent variable? When I define the first function, it could be reasonably interpreted as dt/dy=y instead of dy/dt=y. Is there a built-in convention for defining functions?

Solution

  • First things first: the docs on ode45 are on the mathworks website, or you can get them from the console by entering help ode45.

    The function you pass in needs to take two variables, y then t. As you noticed, with just one it would be impossible to distinguish a function of only y from a function of only t. The first argument has to be the independent, the second is the dependent.

    Try defining your function as dy = d(t, y) and ddy = dd(t, y) with the same bodies.

    one other note, while using a string representing the function name should work, you can use @d and @dd to reference the functions directly.