help-octave
[Top][All Lists]
Advanced

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

Re: error with function handles


From: Martin Helm
Subject: Re: error with function handles
Date: Wed, 19 Aug 2009 14:45:13 +0200
User-agent: KMail/1.10.3 (Linux/2.6.27.25-0.1-default; KDE/4.1.3; x86_64; ; )

Am Mittwoch, 19. August 2009 14:18:30 schrieb simone marras:
> 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.

Hello,

if you really want to pass the function name as string you should use feval 
instead of eval to evaluate it.

y = feval(fun, x);

Or you simply use a function handle instead of a string 

midpntc(0,1,10,@myfun)

and in midpntc

y = fun(x);

instead of eval

Hope that helps.

- mh



reply via email to

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