help-octave
[Top][All Lists]
Advanced

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

Re: error with function handles


From: Jaroslav Hajek
Subject: Re: error with function handles
Date: Wed, 19 Aug 2009 14:36:59 +0200

On Wed, Aug 19, 2009 at 2:18 PM, simone marras<address@hidden> wrote:
> Hi,
>
> I am trying to call a function that accepts a function handle as
> argument. My functions are all defined within the same directory where
> myfun.m rerturns a simple expression as:
>
> function y = myfun(x)
>     y = 10*exp(-50.0*abs(x)) - 0.01/((x-0.5).*(x-0.5) + 0.001) +
> 5.0*sin(5.0*x);
> endfunction
>
> and midpntc is defined as:
>
> function [Int] = midpntc(a, b, m, function)
>
>    h=(b-a)/m
>        x=[a+h/2:h:b];
>
>        k = max(size(x));
>        y = eval(fun);
>
>        if size(y) == 1, y = diag(ones(k))*y; end
>
>        int = h*sum(y);
>
>
>
> when I call midpntc in octave as:
>
>>> Int = midpntc(0, 1, 10, "myfun")
>
> I obtain the following error:
>
>  midpntc(0,1,10,"myfun")
> h =  0.10000
> error: `x' undefined near line 2 column 9
> error: evaluating binary operator `.^' near line 2, column 10
> error: evaluating binary operator `-' near line 2, column 13
> error: evaluating binary operator `-' near line 2, column 17
> error: evaluating binary operator `./' near line 2, column 6
> error: evaluating assignment expression near line 2, column 3
> error: called from `myfun'
> error: evaluating assignment expression near line 13, column 4
> error: called from `midpntc' in file
> `/home/simone/Documents/scientific/CFD/myCFD_library/ATMOSPHERE_miscellanea/TMP/midpntc.m'
>
>
> while, if I use the syntax
>
>>> Int = midpntc(0, 1, 10, @myfun)
>
> the error that I get is:
>
> h =  0.10000
> error: octave_base_value::convert_to_str_internal (): wrong type
> argument `function handle'
> error: eval: expecting std::string argument
> error: evaluating assignment expression near line 13, column 4
> error: called from `midpntc' in file
> `/home/simone/Documents/scientific/CFD/myCFD_library/ATMOSPHERE_miscellanea/TMP/midpntc.m'
>
>
> Could anyone help me with this issue?
> Thank you very much in advance,
>
> S.
>

eval is not meant for evaluating function handles. use fun (...) or
feval (fun, ...).
"help" and "doc" are your friends :)

cheers

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

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