Creating a Sin Function Handle

5 views (last 30 days)
NDA001
NDA001 on 4 Oct 2019
Commented: John D'Errico on 4 Oct 2019
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
apply.JPG
  2 Comments
Stephen23
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
John D'Errico on 4 Oct 2019
Please don't use an answer to make a comment. Answer by NDA001 moved into a comment here:
"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?"

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!