필터 지우기
필터 지우기

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

조회 수: 82 (최근 30일)
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
Stephen23
Stephen23 2017년 7월 5일
편집: Stephen23 2017년 7월 5일
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:

댓글을 달려면 로그인하십시오.

채택된 답변

Adam
Adam 2017년 7월 5일
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
julian navas
julian navas 2017년 7월 5일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2017년 7월 5일
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
julian navas
julian navas 2017년 7월 5일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by