Structuring Code for Global Parameters in Octave

In summary, global variables are dangerous and should be avoided. A better strategy is to follow a functional programming style where each function has inputs and outputs but no external variables like globals. This makes programs harder to write but easier to debug.
  • #1
Dr.D
2,412
720
TL;DR Summary
How do I define system constants once at the beginning of a script, and then have them available inside the function that calculates the derivatives for an ODE solution and also after solving the ODE?
I'm trying to do a fairly complete system analysis script in Octave that involves these steps:
1) Assign values to all fixed system parameters
2) Make various preliminary calculations based on system parameters
3) Solve a system of differential equations
4) Make various post-ODE solution calculations
5) Make various plots
I'd like to have all the results of the first two steps available for use in the last three steps. It would seem like a global declaration would be the way to do this, but the documentation on that is unclear.

How do I structure the code to accomplish these objectives? I really do not want to have to bury all the system parameters and related constants inside the function that defines the derivative. Surely there is a better way?
 
Last edited:
Physics news on Phys.org
  • #2
Here's what Octave docs have to say on it:

https://octave.org/doc/v4.2.1/Global-Variables.html
There are dangers to using global variables so programmers usually avoid them. A better strategy is to follow a functional programming style where each function has inputs and outputs but no external variables like globals. It makes programs harder to write but easier to debug.

The two common issues are:
  • defining a local with the same name and essentially hiding the global from consideration.
  • some routine changes the global variable and that effect now ripples through your processing stream causing unanticipated consequences later on.
EDIT added "no external..."
 
Last edited:
  • #3
jedishrfu said:
A better strategy is to follow a functional programming style where each function has inputs and outputs but external variables like globals.
@jedishrfu I don't understand this sentence. Would you explain, please? It seems like the second clause is missing something -- "but external variables like globals ... what?"

For those old enough to remember Fortran, what I'd really like is a COMMON block. This certainly always raised the danger of the second point jedishfru raised above, but is was extremely useful. I still don't see any neat way around it without have to repeat the definition of all my constants in each subroutine and then evaluate them everytime the routine is called.

PS: I did read the Octave documentation first, and that's what seems particularly unclear and I might say, pointless. I fail to see why anyone would have any use for that.
 
  • #5
One aspect of Functional programming means writing functions which have no side effects ie given the same inputs you will get the same outputs. A consequence of that style of programming is no global variables are used to provide information to the function or extract info from the function once run.

It’s harder to write an application using that restriction but it results in better code that is easier to test and debug in the long run.
 
  • #6
@jedishrfu : OK, but this bring me back to my original question. How do you get all of the problem constants into a derivative function for ODE solution without embedding them? If they are embedded in the function, they will have to be evaluated over and over again, many times in a numerical solution of an ODE. That is horribly inefficient! If they have to be both embedded and also stated outside the routine for use outside, then if the problem parameters change , it it necessary to change them everywhere they are embedded, not just once. Is that really the best that can be done in Octave?
 
  • #7
Variables in Octave that are declared global have to be re-declared global inside functions in order to be passed properly. So if you have a main program that looks like:
Code:
## Calculate E=mc^2 from m.
global c;
c = 3e+8;
mass = input("Enter mass");
function E = energy (m)
  E = m*c^2;
endfunction
printf("Energy = %g", energy(mass));
it will flag an error because the global variable isn't declared inside the function. Instead, this works:
Code:
## Calculate E=mc^2 from m.
global c;
c = 3e+8;
mass = input("Enter mass");
function E = energy (m)
  global c;
  E = m*c^2;
endfunction
printf("Energy = %g", energy(mass));
Notice you only have to redeclare that c is a global variable (line 6); you don't have to declare the value of c. Octave will take that from the main program.
 
  • #8
Thank you, @TeethWhitener What you describe is just about what I have come to on my own. In many respects, this is much like the Fortran COMMON block usage, which is what I had in mind. Thanks for the help.
 

1. What are global parameters in Octave?

Global parameters in Octave are variables that can be accessed and modified from any function or script in the Octave workspace. They have a global scope and are not restricted to a specific function or script.

2. How are global parameters defined in Octave?

Global parameters can be defined using the global keyword followed by the name of the variable, for example: global x. This tells Octave that the variable x should be accessible from any function or script.

3. Can global parameters be modified from within a function?

Yes, global parameters can be modified from within a function as long as they have been declared as global within that function. This can be done by using the global keyword followed by the name of the variable within the function, for example: global x.

4. How are global parameters useful in Octave?

Global parameters are useful in Octave because they allow for the sharing of data between different functions and scripts. This can reduce the need for passing variables as arguments between functions, making the code more concise and easier to read.

5. Are there any downsides to using global parameters in Octave?

One potential downside to using global parameters in Octave is that it can make code less modular and harder to debug. It is important to use them sparingly and only when necessary to avoid potential conflicts and confusion.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
11
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • Programming and Computer Science
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Replies
0
Views
2K
Back
Top