Matlab ODE-Solver and Constant Params

In summary, the conversation discusses a code for a pizza making simulation using the ode45 function in MATLAB. The code includes parameters such as a, b, and d, which can be set outside the function for better organization. One approach is to use a nested function, while another is to use global variables. The conversation concludes with the idea that as long as the code works, it doesn't matter which approach is taken.
  • #1
S. Moger
53
2
Hey,

Code:
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:
[t,y] = ode45(@pizzaode, [0 100], [u(1);v(1)]);

This works but it's crap.

Why?

Code:
    a=2;
    b=0.5;
    d=4;

- Ugly code.

I want to set the params outside the function.

So ok, I did this:

Code:
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?
 
Physics news on Phys.org
  • #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;
 
  • #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.
 
  • #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?
 
  • #5
sokrates said:
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?

:) Indeed...
 

1. What is a Matlab ODE-solver?

A Matlab ODE-solver is a numerical method used to solve ordinary differential equations (ODEs) in the Matlab software. It is a powerful tool for simulating and analyzing dynamic systems in various fields such as engineering, physics, and biology.

2. How does a Matlab ODE-solver work?

A Matlab ODE-solver uses numerical algorithms to approximate the solution of an ODE by breaking it into smaller, simpler steps. It starts with an initial value and calculates the value of the function at each step, using the previous step's value as a starting point. The accuracy of the solution depends on the chosen algorithm and step size.

3. What is the difference between an explicit and implicit ODE-solver in Matlab?

An explicit ODE-solver in Matlab uses a fixed step size and evaluates the function at each step. It is efficient for solving simple ODEs but may not be accurate for complex systems. On the other hand, an implicit ODE-solver adjusts the step size dynamically and uses a more sophisticated algorithm to solve the ODE. It can handle stiff problems and provide more accurate solutions.

4. How do I specify constant parameters in a Matlab ODE-solver?

In a Matlab ODE-solver, constant parameters can be specified by defining them as symbolic variables using the "syms" command. These parameters can then be used in the ODE function as coefficients or constants. Alternatively, they can be assigned numerical values using the "subs" command before calling the ODE-solver.

5. Can I use a Matlab ODE-solver for systems of ODEs with time-varying parameters?

Yes, a Matlab ODE-solver can handle systems of ODEs with time-varying parameters by using the "odefun" function. This function allows for the specification of time-dependent parameters in the ODE function, which can be updated at each step of the solver. This feature is useful for simulating dynamic systems with changing parameters, such as in control systems or population models.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
992
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
933
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
3
Views
576
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top