Creating a Sin Function Handle
5 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 4 Oct 2019
Edited: Stephen23
on 4 Oct 2019
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
on 4 Oct 2019
"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?"
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!