help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Global variables in function


From: Carlo de Falco
Subject: Re: Global variables in function
Date: Wed, 12 Nov 2008 06:12:56 +0000


On 12/nov/08, at 05:42, Corsair wrote:

Hi all.  I have the follow code:

  global w = 2;
  global C = 1;
  global I = 1;

  function vr = r(t)
    vr = [sin(w * t), cos(w*t), t];
  endfunction

  function vt = tang(t)
    vt = [w*cos(w*t), -w * sin(w*t), 1];
  endfunction

  function db = dB(t, r)
    db = I * cross(tang(t), f(t) - r) / norm(f(t) - r)^3;
  endfunction

  dB(1, [1,2,3])

When I run this script, Octave told me that

  error: `w' undefined near line 12 column 9
  error: evaluating binary operator `*' near line 12, column 10
  error: evaluating assignment expression near line 12, column 6
  error: called from `tang'
  error: evaluating argument list element number 1
  error: evaluating binary operator `*' near line 16, column 10
  error: evaluating binary operator `/' near line 16, column 37
  error: evaluating assignment expression near line 16, column 6
  error: called from `dB'
  error: near line 19 of file `test.oct'

Anyone know the problem?


(...)
It is necessary declare a variable as global within a function body
in order to access it.  For example,

    global x
    function f ()
      x = 1;
    endfunction
    f ()

does _not_ set the value of the global variable `x' to 1.  In order to
change the value of the global variable `x', you must also declare it
to be global within the function body, like this

    function f ()
      global x;
      x = 1;
    endfunction
(...)


The explanation above was obtained by typing "doc global" at the octave prompt.

c.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]