Global variable not seen by function: the function creates a new empty variable

109 views (last 30 days)
Hi everyone. I have an script that defines a global variable and a function defining a system of ODEs, for example
Script:
clc, clear
global T
%some lines
T=something;
[t,x]=ode23s(@func,time,x0)
%some lines
function
function dx=func(t,x)
global T
k1=5.35e18*exp(-15700/T);
k2=5.35e18*exp(-15700/T);
k3=75.45e18*exp(-15700/T);
r1= k1*x(1)*x(2))-k3*x(3);
r2=k2*x(2)*x(3);
dx=[-r1; -(r1-r2); r1-r2; r2];
The script and function are in separate files, same folder. The error message says
Error using /
Matrix dimensions must agree.
Indicating that the variable T in the function func workspace appears as empty [] Why is this, and how can I pass this T to that function? I do know functions can pass arguments but in this case it is not directly but ode23s is who's calling @func. Thanks in advance.
  1 Comment
Stephen23
Stephen23 on 5 Jul 2017
Edited: Stephen23 on 5 Jul 2017
This is why the MATLAB documentation states "Global variables can be troublesome, so it is better to avoid using them": because globals are buggy and hard to debug. Much better is to use an anonymous function, exactly as the MATLAB documentation recommends:

Sign in to comment.

Accepted Answer

Adam
Adam on 5 Jul 2017
Define your function as:
function dx=func(t,x,T)
...
Use it as:
T=something;
[t,x]=ode23s(@(t,x) func(t,x,T),time,x0)
I suspect you will still have your error as it is probably a dimension issue as the error indicates, but the debugger will tell you this instantly. Certainly it is always plenty possible that global variables can lead to a mess though. I have no idea if they can be picked up inside a file called by ode23s and would never wish to try!
  1 Comment
julian navas
julian navas on 5 Jul 2017
Hello Adam The variable T defined in "something" is a scalar, so it should not have troubles when called in the auxiliar function. In fact, when debugging, the variable in the base workspace appears no problem, but in the function workspace it's like the global T command created a new T, which baffles me. Anyways, your suggestion worked. Many thanks!

Sign in to comment.

More Answers (1)

Jan
Jan on 5 Jul 2017
While I agree, that globals should be avoided, you code should work:
function main
global T
T = something
...
function sub
global T
a = 1.23 / T
If you get the error according the size of T, this means, that something replies the empty matrix. If you use the recommended anonymous function (see also: http://www.mathworks.com/matlabcentral/answers/1971), this problem will still exist: There is a bug in "something".
  1 Comment
julian navas
julian navas on 5 Jul 2017
Hello Jan The variable T is a scalar, defined by a vector and a loop, nothing complicated to bug the code. The problem was solved passing the argument T to the function, like this
ode23s(@(t,x) func(t,x,T),time,x0)
which I did not know could be possible because I didn't have previously defined t and x. Thanks for your reply!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!