Taking in a function as an argument

2 views (last 30 days)
I'm trying to make a function which takes in a function and then uses it. Right now my code looks like this:
function out = func(inputfunc, constant1, constant2)
inputfuncval1 = inputfunc(constant1);
...code...
end
When I then try to call it with a function like this:
out = func(@(x) testfunc(x),x1 ,x2);
It interpretets my input as an array instead of a function.
Anyone got any ideas on how to solve this issue?
  2 Comments
Guillaume
Guillaume on 8 Nov 2019
Can you give us the full text of the error you see. The code you show should work although
out = func(@testfunc, x1, x2);
would be simpler.
Jakob Grunditz
Jakob Grunditz on 8 Nov 2019
To understand the error I must give you the whole code so... you're welcome
function svar = bisekt(f, x1, x2)
fx1 = f(x1);
fx2 = f(x2);
if fx1>0 && fx2<0
if abs(x1-x2)>10^-6
m = (x1+x2)/2;
fm = f(m);
if fm == 0
svar = m;
return
elseif fm>0
bisekt(m, x2)
elseif fm<0
bisekt(x1, m)
end
else
svar = x1;
return
end
elseif fx1<0 && fx2>0
if abs(x1-x2)>10^-6
m = (x1+x2)/2;
fm = f(m);
if fm == 0
svar = m;
return
elseif fm<0
bisekt(m, x2)
elseif fm>0
bisekt(x1, m)
end
else
svar = x1;
return
end
end
end
svar = bisekt(@(x) exp(x)+x^2-1, -2, -0.5)
error code
Array indices must be positive integers or logical values.
Error in bisekt (line 2)
fx1 = f(x1);
Error in bisekt (line 14)
bisekt(m, x2)

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 8 Nov 2019
Seems clear enough:
Error in bisekt (line 14)
bisekt(m, x2)
Within bisekt, you call the function again, this time with input m which is a scalar value, not a function, and only one argument. So, the input f is then your m scalar, and of course, m(x2) is going to fail if x2 is anything but 1.
Perhaps, the call should be:
bisekt(f, m, x2);
same for the other recursions...
  1 Comment
Jakob Grunditz
Jakob Grunditz on 8 Nov 2019
Yes that seemed to be the problem x)... Thank you for spotting that.

Sign in to comment.

More Answers (1)

M
M on 8 Nov 2019
How is testfunc defined ?
Here is a simple working example:
function out = func(inputfunc, constant1, constant2)
out = inputfunc(constant1, constant2);
end
out = func(@(x,y) x+y,1,2)
out =
3
  2 Comments
Jakob Grunditz
Jakob Grunditz on 8 Nov 2019
In my case I tried inputting the function:
inputfunc = @(x) exp(x)+x^2-1
that I then try to evaluate in two places constant1 and constant2 for later use in the function.
function out = func(inputfunc, constant1, constant2)
inputfuncval1 = inputfunc(constant1);
inputfuncval2 = inputfunc(constant2);
...code...
end
out = func(@(x) exp(x)+x^2-1, x1, x2)
Guillaume
Guillaume on 8 Nov 2019
As I commented in your question, if the above doesn't work, give us the full text of the error message.
With the following function:
function out = func(inputfunc, constant1, constant2)
out(1) = inputfunc(constant1);
out(2) = inputfunc(constant2);
end
I get:
>> out = func(@(x) exp(x)+x^2-1, 1, 2)
out =
2.71828182845905 10.3890560989307
No problem there.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!