Search code examples
matlabodedifferential-equations

How to run ode45 for different angles


I am trying to solve a second order differential equation. I have managed to solve it for one angle (there is a factor in the function depending on an angle) using ode45 in matlab. In my programscript there is also the same angle given as input arguments to ode45.

What I want is to solve the equation for different angles. I tested with a for-loop where I simply run through ode45 for given angles (as input arguments) and this plots out different curves like it should. However I'm not really sure how correct it is since I have not changed the angle in the function that ode45 solves for.

So the question is how I for each loop also change the angle inside my function.


Solution

  • I believe your question is really related to passing parameters to a function used by ode45. Your function that defines the right hand side of your ODE should take the angle as an input parameter and should be of the form

    f(t,y,angle)
    

    You should then wrap this function inside another function within your for loop that changes the angle with each iteration. For example:

    for angle=linspace(0,pi,10)
        odefun = @(t,y) f(t,y,angle);
        [t_out, y_out] = ode45(odefun,[t0, tf],y0);
        plot(t_out,y_out)
    end
    

    Also see this document from Mathworks on parameterizing functions.