Having a function input be a function handle
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Geoff Hayes
2015년 3월 17일
- 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
Geoff Hayes
2015년 3월 18일
If you want to define your own function handle, you could try
func = @(x)x.^2;
Desmond(func,10);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!