help-octave
[Top][All Lists]
Advanced

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

Re: Problem defining global vars


From: Stefan van der Walt
Subject: Re: Problem defining global vars
Date: Wed, 30 Jun 2004 10:33:31 +0200
User-agent: Mutt/1.5.6+20040523i

Hi Christian

The variables 'a' and 'b' are outside the scope of function xdot, so
you need to explicitly define them to be global:

function xdot = f(x,t)
  global a;
  global b;
  xdot = a*x + b;
endfunction

Regards
Stefan

On Mon, Jun 28, 2004 at 05:03:31PM -0500, Christian Renz wrote:
> When I run the following script, octave complains that a and b are 
> undefined. Once I move the definitions inside the function f, 
> everything works fine, however. Is there a way I can define a and b so 
> they are recognized by f (when used via lsode)?
> 
> ----------8<----------8<----------
> global a = [ -1, 0.5 ; 1, -1];
> global b = [ 0.5 ; 0.5 ];
> 
> function xdot = f(x, t)
>     xdot = a*x + b;
> endfunction
> 
> x0 = [ 5; 0 ];
> t = linspace(0, 10, 100)';
> x = lsode("f", x0, t);
> 
> plot (t, x);
> ----------8<----------8<----------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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