How to find value of function for array of inputs ?

19 views (last 30 days)
Lets say f(x) is user inputed function and I have some values of x stored in array, Now I have to find the value of the function for each value of x.
i.e, if I have f(x) as x*2 [This is user-input]
then If i have x = [1,3,4,6]
and I need an other array which the value of f(x)
y= [1,9,16,36] as my answer.
Is there any function that will let evalute the same at once ?

Answers (1)

David Hill
David Hill on 3 Apr 2021
f=@(x)x.^2;
x=[1 3 4 6];
y=f(x);

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!