Borrar filtros
Borrar filtros

Index issue with using a global variables that are vectors in a function

1 visualización (últimos 30 días)
I have written a function in which two global variables, both vectors with 2 entries, are used:
function TH=THETA(y, z);
global qv;
global dv;
TH=qv(y) + z*((qv(y))^2)/(1 - (dv(y))*z);
When I call the function, such as with THETA(1, 0.5) or THETA(2, 0.5) , I get the following error:
"Index exceeds matrix dimensions."
I am puzzled by this as the index in question, the first argument of THETA, is 1 or 2, and the vectors in the function both have two entries. What might be the problem?
  1 comentario
Stephen23
Stephen23 el 2 de Ag. de 2016
Editada: Stephen23 el 2 de Ag. de 2016
"What might be the problem?"
The decision to use global variables is the problem.
This is a classic example of code using globals: buggy, and hard to debug.
As Doug Hull says: "I have never seen MATLAB code where globals were the right thing to do"
Sadly, beginners keep on insisting on using globals for every little task with more than one workspace, and are then surprised when it turns out to be impossible to understand and impossible to debug.
If you need to pass parameters, then read this:

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 2 de Ag. de 2016
Why are you using global? you can simply write
function TH=THETA(y, z,qv,dv);
TH=qv(y) + z*((qv(y))^2)/(1 - (dv(y))*z);

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by