# Writing a math equation.

1 view (last 30 days)
Sardor Butunboev on 28 Sep 2021
Edited: John D'Errico on 28 Sep 2021
I want to write a math equation sin(1+x) and plot it.
1. I did it in this way:
funktion = @(x) sin(1+x);
fplot(funktion,[0 pi])
Yet, it shows that it will not cross the x-axis before "pi".
When I google how I sin(1+x) is graphed it shows me that it should before 'pi', around 2.14.
In this regard I want to ask where is mistake.
2. I don't understand why I need "@(x)" in the "funktion". I found an example of a function in the internet.
Can you tell in simple terms what it does?
Thanks.

John D'Errico on 28 Sep 2021
Edited: John D'Errico on 28 Sep 2021
Good. You made an effort, did some reading. Not only that, but you are trying things, getting your hands dirty. Keep that up and you will learn quickly.
First, what does the @(x) syntax mean? This allows you to create a function on the fly, so to speak. A one line thing that is a function of the variable x. You can now evaluate this function directly, but you can also pass it around. You can even create a function handle inside the call to another function, in the same line of code. (That is often called an anonymous function handle.) Function handles are efficient to evaluate. You can pass a function handle into an optimzer like fzero, or into a tool like fplot, or into your own functions.
funktion = @(x) sin(1+x);
It is called a function handle. We can use it like this:
format long g
funktion(3)
ans =
-0.756802495307928
Indeed, MATLAB evaluates the function specified. We can pass it around. For example, pass it to fplot. Here, I will plot the function between limoits on x of 0 and 5. I'll plot the curve in blue, then add a reference line in red at y == 0. Finally, I add grid lines on the pot to help see where it is crossing zero. Finally, since we all know it must cross zero at pi-1, I'll put a verticle green line there.
fplot(funktion,[0 5],'b')
yline(0,'r')
xline(pi-1,'g')
grid on So we see the curve does appear to cross zero around 2. pi-1 would seem the correct value. We can solve for that point using fzero.
[xval,fval] = fzero(funktion,[0,5])
xval =
2.14159265358979
fval =
1.22464679914735e-16
Since fzero is a numerical solver, and we cannot exactly represent pi-1 as a floating point number anyway, fzero did as well as it can do, giving us roughly 16 digits of precision in the result.
Or, we can convert it into a symbolic form to solve it.
syms X
funktion(X)
ans = So when I passed a symbolic variable X into funktion, it returns sin(X+1) in a now symbolic form.
Xsol = solve(funktion(X),X,'returnconditions',true)
Xsol = struct with fields:
X: pi*k - 1 parameters: k conditions: in(k, 'integer')
So the global set of solutions is pi*k-1, where k is any integer.
As far as what you did, and why it failed, this I cannot guess. because what you say you did was correct. If I was forced to guess, you actually had some other function handle stuffed into the function handle funktion. This probably happened when you were playing around. So when you see something strange, check what is in the variables you have created. You may sometimes be surprised.

KSSV on 28 Sep 2021
x = linspace(0,pi) ;
f = sin(1+x) ;
plot(x,f) 