[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: function
From: |
Ben Abbott |
Subject: |
Re: function |
Date: |
Sat, 08 Nov 2008 19:14:13 -0500 |
On Nov 8, 2008, at 5:58 PM, Steve MC Han wrote:
Hi,
I am a beginner of Octave and I was wondering if someone could help
me.
I followed direction described here and typed one by one at the
prompt and it works nicely and produce the graph;
http://www.delorie.com/gnu/docs/octave/octave_11.html
octave:8> function xdot = f (x, t)
>
> r = 0.25;
> k = 1.4;
> a = 1.5;
> b = 0.16;
> c = 0.9;
> d = 0.8;
>
> xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
> xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
>
> endfunction
x0 = [1; 2];
t = linspace (0, 50, 200)';
x = lsode ("f", x0, t);
plot(t,x);
But if I make script file exactly the same then it produce a lot of
error
messages
function xdot = f (x, t)
r = 0.25;
k = 1.4;
a = 1.5;
b = 0.16;
c = 0.9;
d = 0.8;
xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
endfunction
x0 = [1; 2];
t = linspace (0, 50, 200)';
x = lsode ("f", x0, t);
plot(t,x);
The error messages are:
error 'x' udefined near line 15 column 14
error evaluating binary operator '*' near line 15 column 13
---- etc
Can someone figure out what's going on?
Thanks,
I think I see the problem. Please try ...
(1) Create a function file called "f.m" which contains the lines from
"function xdot = f (x, t)" through "endfunction".
---------------------
function xdot = f (x, t)
r = 0.25;
k = 1.4;
a = 1.5;
b = 0.16;
c = 0.9;
d = 0.8;
xdot(1) = r*x(1)*(1 - x(1)/k) - a*x(1)*x(2)/(1 + b*x(1));
xdot(2) = c*a*x(1)*x(2)/(1 + b*x(1)) - d*x(2);
endfunction
---------------------
(2) Create a script file called "my_script.m" which contains
-------------------------------
x0 = [1; 2];
t = linspace (0, 50, 200)';
x = lsode ("f", x0, t);
plot (t, x)
-------------------------------
(3) Type "my_script" from the Octave window
That you can type a define a function from Octave's command line is a
nice convenience. However, you cannot do so in a script file. Rather
the function must be defined in a function file (at least that is my
understanding, I'm sure someone will correct me if I'm wrong).
Ben
- function, Steve MC Han, 2008/11/08
- Re: function,
Ben Abbott <=
- Re: function, Ivan Sutoris, 2008/11/08