Having a function input be a function handle

Hi I am trying to make it so one of my inputs is a function handle so for example make it sin(x). Here is the code I have written so far. Thank you in advance for any help you can offer.
function []=Desmond(myfunction,n)
mytest=@(x) sin(x)
mytest=myfunction
x=-10:10;
y=myfunction(x).^n;
plot(x,y)
end

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 17 Mär. 2015

0 Stimmen

- if you want to pass a function handle into your code, then just prefix the function name with @. For example, if your code is
function Desmond(hFunc,n)
x=-10:10;
y= hFunc(x).^n;
plot(x,y)
end
then to pass sin into this function you would simply call the above as
Desmond(@sin,10)

3 Kommentare

Desmond Johnson
Desmond Johnson am 17 Mär. 2015
Thank you so much for the help. I am still confused though as to where you set the value of sin x (ex sin(3). Also when I run your example I get this error. Conversion to double from function_handle is not possible.
Desmond Johnson
Desmond Johnson am 17 Mär. 2015
Actually I figure out the problem. My other question is what if you want the function input to be ^2?
If you want to define your own function handle, you could try
func = @(x)x.^2;
Desmond(func,10);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by