[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: error with function handles
From: |
simone marras |
Subject: |
Re: error with function handles |
Date: |
Wed, 19 Aug 2009 14:50:34 +0200 |
Hi everyone, thanks for helping.
I corrected the errors that you pointed out but still obtained exactly
the same error that I would get initially. To summarize the steps,
this is what we have:
1)
>> function y = fun(x)
> y = 10*exp(-50.0*abs(x));
> endfunction
>>
2)
function int = midpntc(a, b, m, fun)
h=(b-a)/m
x=[a+h/2:h:b];
k = max(size(x));
y = fun(x);
if size(y) == 1, y = diag(ones(k))*y; end
int = h*sum(y);
end
3) And finally, call to midpntc as:
octave:72> midpntc(0, 1,10, @fun)
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'
I am lost
S.
On Wed, Aug 19, 2009 at 2:45 PM, Martin Helm<address@hidden> wrote:
> 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
>
>
--
Simone Marras, Ph.D. candidate
Barcelona Supercomputing Center
Universitat Politecnica de Catalunya
Avd. Diagonal 647,
Edificio H, planta 10
Barcelona, 08028
SPAIN
Tel.: (+34) 93 4011744
web: www.cranfield.ac.uk/~c086030