Creating a Sin Function Handle
조회 수: 5 (최근 30일)
이전 댓글 표시
I am attempting to create a function named "apply" that takes the function handle of @sin and runs it through a for loop to solve for [0 pi./2 pi 3*pi./2 2*pi].
I keep getting errors and having trouble formulating the loop.
Here is my current code

댓글 수: 2
Stephen23
2019년 10월 4일
편집: Stephen23
2019년 10월 4일
Not only is your code full of syntax errors, the entire concept seems pointlessly complex. Why do you need a loop at all? Is there a reason why you want to avoid simply calling the function with a vector?:
>> fun = @sin;
>> vec = [0,pi/2,pi,3*pi/2,2*pi];
>> fun(vec)
ans =
0.00000 1.00000 0.00000 -1.00000 0.00000
Note that the function arrayfun probably does exactly what you are trying to do, but even using arrayfun is likely superfluous if your functions are vectorized (like sin is).
John D'Errico
2019년 10월 4일
"I appreciate the feedback. I am in the process of learning matlab and this was one of the tasks I was given to build up specific skill sets. Knowing and understanding that it is not the most efficient way.
I am interested to know to make myself better at Matlab. If I proceeded with the for loop concept. Where did I go wrong?"
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!