Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab ODE-Solver and Constant Params

  1. Nov 22, 2009 #1
    Hey,

    Code (Text):
    function dy = pizzaode(t, y)
        a=2;
        b=0.5;
        d=4;
        u=y(1);
        v=y(2);
        dy = nan(2,1);
        dy(1) = u*(1-u) - a*u*v/(u+d);
        dy(2) = b*v*(1-v/u);
    end
    Code (Text):
    [t,y] = ode45(@pizzaode, [0 100], [u(1);v(1)]);
    This works but it's crap.

    Why?

    Code (Text):
        a=2;
        b=0.5;
        d=4;
    - Ugly code.

    I want to set the params outside the function.

    So ok, I did this:

    Code (Text):
    function [t,y] = pizzamaker(a, b, d, u0, v0)
       

    [t,y] = ode45(@pizzaode, [0 100], [u0;v0]);

    function dy = pizzaode(t, y)

        u=y(1);
        v=y(2);
        dy = nan(2,1);
        dy(1) = u*(1-u) - a*u*v/(u+d);
        dy(2) = b*v*(1-v/u);
    end


    end

    And that's nice because it works. But how would I do this without this strange nesting?
     
  2. jcsd
  3. Nov 24, 2009 #2
    Of course function has its own workspace. Variables are local to the function.

    I'm not sure whether this will work. Try insert the statement
    global a b d

    in your function pizzaode.

    Then before you call the function ode45 you must have the following statements
    global a b d
    a=2; b=0.5; d=4;
     
  4. Nov 24, 2009 #3
    I would ideally want to pass them through the function pointer (or what it is) in the ode45 function call. Global variables are no fun thing either.
     
  5. Nov 29, 2009 #4
    Doing it globally is the best solution I could find for a similar problem.

    Don't be obsessive this is MATLAB - not C++, if it does your job, why bother?
     
  6. Dec 1, 2009 #5
    :) Indeed...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab ODE-Solver and Constant Params
  1. Matlab solver question (Replies: 0)

  2. Matlab and ODE's (Replies: 2)

  3. Ode 45 (matlab) (Replies: 0)

Loading...