Reputation: 41
x=1;
f=@(x) x^3 - (5/x^2)-4*sin(x)-2;
fzero(f,x)
ans =
1.9227
I am supposed to find the root of the equation, x^3 - (5/x^2)-4*sin(x)-2, and the above code is the solution for it.
I don't understand the general mechanism of this code.
(1) What does @ operator do?
I know its something like function handle, but I don't understand what function handle is.
(2) How does it work when it includes x in the parenthesis?
(3) How can there be a function fzero(), when I haven't made a script for fzero()?
(4) why are there two variables inside fzero()? I don't understand that the variable 'f' does there
(5) Why did it declare x=1 in the beginning?
Please consider that I am pretty much new to MATLAB, and don't know much.
Upvotes: 0
Views: 695
Reputation: 101
f = @(x) ...
is the way to declare an anonymous function in MATLAB, actually not very different than creating a function normally in MATLAB such as function output = f(input) ...
. It is just the pratical way especially when you are working with mathematical functions.
@(x)
defines that x
is the variable of which is the same as f(x) in mathematics. fzero()
is MATLAB's existing function to calculate the x
value for f(x) = 0 which means calculating roots of defined funtion. Giving your x a real value at the beginning does mean the starting point to find the root. It will find the roots greater than 1 in your case. It will be very clear for you when you read existing documentation of MATLAB.
Edit:
If you give an interval such as x = [0 1]
instead of x = 1
, fzero(f,x)
would try to calculate roots of f function in given interval, if there is no roots exist in that interval it woud return a NaN
value.
Upvotes: 1