help-octave
[Top][All Lists]

## Re: using a function as a parameter

 From: William Kreamer Subject: Re: using a function as a parameter Date: Mon, 18 Mar 2002 09:21:24 -0500

```The method of passing a function and plotting it is pretty well spelled out
in the Octave documentation. Here is an example:

q=0;  # A dummy statement --  if the first statement of an Octave script
starts with
# "function", the whole script will be interpreted as a function
function y = xpto(x)
y = x .* x;    # Remember to end a statement with a semicolon, unless
you actually
# want to see the data. Also, the period in the
statement is very important
endfunction
x = (0:0.05:3)';   # Octave plots data that is assembled in arrays or
matrices
y = xpto(x);
gset grid;
axis([min(x) max(x) 0 10]);
plot(x,y)          # Octave then pipes the data to gnuplot, where it is
displayed

To calculate the first derivative, use the formula (dy/dx) = (f(x +
deltax) - f(x))/ deltax
What I wrote is NOT Octave code, but the basic formula. Have f(x) tabulated
as an array, and the formula will calculate the derivative as another array.

----- Original Message -----
From: "José Miguel Pereira Tavares" <address@hidden>
Sent: Friday, March 15, 2002 17:25
Subject: using a function as a parameter

>
> Hi all!
>
> I'm new to octave (and never used mathlab) and I would like to know if
> it's possible to pass a function as a parameter to another function, and
> if sou how?
>
> For example, if I define a function as:
>
> function xpto = f(x)
> xpto = x^2;
> endfunction
>
> How could I plot this function? Something like
>
> plot(f(x)) ????
>
> Another question, is octave capable of finding the derivative of a
> function (can it find it's expression? or it's value for a given x?)
>
>
> Miguel Tavares
>
> --
> I love the whooshing sound they make as they pass by.
>
>
>
>
> -------------------------------------------------------------
> 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
> -------------------------------------------------------------
>
>

-------------------------------------------------------------
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
-------------------------------------------------------------

```