help-octave
[Top][All Lists]

## Re: How to plot an user-defined function?

 From: Eric Chassande-Mottin Subject: Re: How to plot an user-defined function? Date: Fri, 1 Aug 2003 20:49:28 +0200 (CEST)

```>
> function y = f(t)
>     y = exp(-0.5*t*t);
> endfunction
>
> function z = g(t)
>     z = (1.0/sqrt(2.0*pi))*quad('f',-10.0, t, 1e-12);
> endfunction
>
>       Now I want to plot g(t). However, an attempt like:
>
> t = linspace(-10,10,2000);
> plot(t, g(t))
>
>       fails. What I want to do is to plot several values for
> the integration of the probability function (but it could be
> anything else) from -10 to an arbitrary "t".

when I run your code, I don't get the same error message

octave2.1:14> plot(t, g(t))
error: invalid conversion from real matrix to real scalar
error: quad: expecting third argument to be a scalar
error: evaluating binary operator `*' near line 2, column 27
error: evaluating assignment expression near line 2, column 7
error: called from `g'
error: evaluating argument list element number 2

your functions f and g work only for arguments that are scalar (ie.,
single number)

octave2.1:6> g(1)
ans = 0.84134

but not for vectors

octave2.1:6> g([1 2])
error: invalid conversion from real matrix to real scalar
error: quad: expecting third argument to be a scalar
error: evaluating binary operator `*' near line 2, column 27
error: evaluating assignment expression near line 2, column 7
error: called from `g'

this is why octave complains.
you need to modify your functions in order to do so.

in the line :
plot(t, g(t))
octave expects two vectors of same size.

check this with 'size(t)' and 'size(g(t))' before making the plot.

hope this helps, é.

PS: BTW, you can get the plot of the ERF() function with :
t=linspace(-10,10,2000);plot(t,erf(t))

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

```