Creating a Sin Function Handle

5 次查看(过去 30 天)
NDA001
NDA001 2019-10-4
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 个评论
Stephen23
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
John D'Errico 2019-10-4
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?"

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by