Creating a Sin Function Handle

5 vues (au cours des 30 derniers jours)
NDA001
NDA001 le 4 Oct 2019
Commenté : John D'Errico le 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 commentaires
Stephen23
Stephen23 le 4 Oct 2019
Modifié(e) : Stephen23 le 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 le 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?"

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by