Matlab ODE-Solver and Constant Params

  • Context: MATLAB 
  • Thread starter Thread starter S. Moger
  • Start date Start date
  • Tags Tags
    Constant Matlab
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a MATLAB ODE solver, specifically focusing on how to manage constant parameters within the function. Participants explore different approaches to improve code organization and readability while addressing the challenges of variable scope in MATLAB.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an initial implementation of an ODE function and expresses dissatisfaction with the code structure, particularly the nesting of functions.
  • Another participant explains that variables in functions are local and suggests using global variables to access parameters across functions.
  • A different participant expresses a preference for passing parameters through function pointers instead of using global variables, indicating a desire for cleaner code practices.
  • Some participants argue that using global variables is a practical solution, emphasizing that MATLAB's flexibility allows for less strict coding practices compared to languages like C++.

Areas of Agreement / Disagreement

There is no consensus on the best approach to manage constant parameters in the ODE function. Participants present differing views on the use of global variables versus passing parameters, indicating an ongoing debate about coding practices in MATLAB.

Contextual Notes

Participants have not resolved the implications of using global variables versus function pointers, and there are no definitive conclusions about the best coding practices in this context.

S. Moger
Messages
52
Reaction score
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
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;
 
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.
 
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?
 
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...
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
1
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K