'How to write a function in MATLAB

I want to write a function that returns the value of f(y) for any value of y:

f(y) =tan( sin(y) - sin(tan(y)) )

How can I write this as a function in MATLAB?



Solution 1:[1]

Here is an example function for your purpose

function y = f(x)
   y = tan(sin(x)-sin(tan(x)));
end

Solution 2:[2]

You could use an anonymous function, as elluded to in the comments by obchardon:

f = @(y) tan(sin(y) - sin(tan(y)))

% usage like any other function:
f(1)

Note that you do not include the (y) on the left hand side of the =, this syntax is reserved for indexing into variables, or symbolic math.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThomasIsCoding
Solution 2 Wolfie