solving sin(x) by solve commad?

When I write this command solve(sin(x)), why is the result 0? What is the meaning of this operation? Can I solve it for 0:10?
Thank you.

 Accepted Answer

Um, is it not true that sin(0) == 0? You ask why? It happens to be the correct answer. At least, A correct answer. And since there are infinitely many solutions to the question posed, it is difficult to list them all. However, if you wish to push solve to give you the complete set of solutions, you can do so in parametric form.
syms x
xsol = solve(sin(x) == 0,x,'returnconditions',true)
xsol = struct with fields:
x: [1×1 sym] parameters: [1×1 sym] conditions: [1×1 sym]
xsol.x
ans = 
πk
xsol.conditions
ans = 
k
So solve tells us that pi*k is a solution for integer k.
I have no idea what your question at the end means: "Can I solve it for 0:10?"
Yes, you can solve whatever you want, but until I know what you are asking, I have no clue how to solve what is in your mind. The crystal ball is too foggy.

6 Comments

sorry for being too vague. I will try to be more clear.
[QUOTE] Um, is it not true that sin(0) == 0? You ask why? It happens to be the correct answer. At least, A correct answer [\QUOTE]
But I didn't assign a value to sin(x), I simply used sin(x), and I also didn't assign a value to x, considering both a general case. (When I write x it turns out as x)
[QUOTE]I have no idea what your question at the end means: "Can I solve it for 0:10?" [\QUOTE]
Since: 0:10 means 0 1 2 3 4 5 6 7 8 9 10, I meant to say that can I solve the equation for these values in one time, that is only with one operation.
Meanwhile is there any quote tab here?
Thank you.
(NO, there is no direct way to create a quote. However, it seems simple enough to copy text, and paste it in, then making it bold if you like using the buttons on top of the edit panel.
"But I didn't assign a value to sin(x), I simply used sin(x), and I also didn't assign a value to x, considering both a general case. (When I write x it turns out as x)"
You used solve. When no equality is assigned for an expression, solve sets it equal to zero.
In the beginning of the help for solve, we see the statement:
If the expressions are not equations or inequalities,
solve seeks zeros of the expressions.
Otherwise solve seeks solutions.
Therefore, when you type the line
solve(sin(x))
with x a symbolic variable, it solves for x, such that sin(x) == 0. The first such solution, typically known as the primary solution is at x ==0. I showed how to get all of the other solutions. If you use the tool, is there a reason why you would not read the help?
How do I know that x is a symbolic variable? You said so yourself. (When I write x it turns out as x)
Therefore at some point in your MATLAB session, you typed the command
syms x
or something equivalent to it.
But now, it appears that what you really want is to not use solve at all. Solve is a tool that "solves" equations and systems of equations.
It appears you merely want to substitute x = 0:10, in turn, into the expression sin(x).
format short g
x = 0:10;
sin(x)
ans =
Columns 1 through 9
0 0.84147 0.9093 0.14112 -0.7568 -0.95892 -0.27942 0.65699 0.98936
Columns 10 through 11
0.41212 -0.54402
Or, in a symbolic context, you could uses subs. Or you could create a function handle.
syms x
F = sin(x);
f = matlabFunction(F);
f(0:10)
ans =
Columns 1 through 9
0 0.84147 0.9093 0.14112 -0.7568 -0.95892 -0.27942 0.65699 0.98936
Columns 10 through 11
0.41212 -0.54402
Or, you could have done this:
subs(F,x,0:10)
ans =
[0, sin(1), sin(2), sin(3), sin(4), sin(5), sin(6), sin(7), sin(8), sin(9), sin(10)]
Of course, those values were left in a symbolic form. But you could force MATLAB to convert them to doubles, using the function double.
F = sin(x);
f = matlabFunction(F);
f(0:10)
Would you please explain me the above?
First you determine a variable F whose value is sin(x). I am very poor in Matlab so step 2 and 3 do make no sense to me.
Honestly, I think you need to learn MATLAB. The Onramp tutorials and various sources of help you can find would greatly improve your ability to use the language. Or, just look on YouTube. For example:
To explain the above, you need to understand what a variable is in MATLAB. And that is why you need to start learning MATLAB, because until you understand what a variable is, you cannot use the most basic parts of this language.
There are two kinds of variable classes you will use in your work, at least immediately. For example, if I do this
x = [2 3 5]
x = 1×3
2 3 5
whos x
Name Size Bytes Class Attributes x 1x3 24 double
This creates a vector valued variable. It contains the numbers [1 2 3]. You can operate on that variable. You can compute the sin of those values, if you so please. The result will be another variable, containing those numbers.
y = sin(x)
y = 1×3
0.9093 0.1411 -0.9589
Alternatively, you can create symbolic variables, if you have the necessary toolbox. syms is a command that can
syms z
z
z = 
z
whos z
Name Size Bytes Class Attributes z 1x1 8 sym
whos tells us that z is a symbolic variable, with an essentially unknown value.
w = sin(z)
w = 
sin(z)
Here, w is also a symbolic variable. different from y, where y contains known FIXED values.
What does MATLAB function do? TRY IT! You will learn nothing until you begin making an effort.
F = matlabFunction(w)
F = function_handle with value:
@(z)sin(z)
So F is now a function (actually known as a function handle), of some unknown argument. We can evaluate that function for any value of the argument.
F(pi/6)
ans = 0.5000
F(x)
ans = 1×3
0.9093 0.1411 -0.9589
But really, you need to start learning MATLAB. I can't be your tutor.
metin yilmaz
metin yilmaz on 9 Nov 2020
Edited: metin yilmaz on 9 Nov 2020
But really, you need to start learning MATLAB. I can't be your tutor.
I have three different introductory matlab books but I can't some functions such as "deal" and "horzcat" in them. Would you please suggest me some books? The two with 300-400 pages do not cover even matlabfunction. Only the one with about 700pages cover it.
Those are valid questions, but they are wildly off-topic in this question. A question should not become a running tutoring session though. So if you have a question that you cannot understand about a function, then ask it as another question.
The very best book is in the form of the help docs for MATLAB. This is because any text is limited. It covers the functions the author thought to put in it, what the author thinks is useful. And it also reflects MATLAB as of a few years ago, since a book takes time to write and publish. So texts will be out of date compared to the help docs.
So the very dirst thing when you have no idea what a function does is to use doc.
doc deal
and
doc matlabFunction
These docs will explain the use of those tools. And they will include examples.
Then, TRY THEM OUT. Get your hands dirty, so to speak. Play with the functions. If and when you become confused, then ask a (separate) question on Answers.
syms x
F = matlabFunction(x.^2)
LOOK AT F. TRY IT. I will not do that for you here, since you won't learn unless you are the one making an effort. If you have questions about what you see there, then try
doc function_handle
Look at the examples you see there. Try using it in MATLAB. Test your hypothesis as to what it does and how it works.
Anyway, if I did give you detailed answers in this comment to your initial question, that would just encourage you to ask more questions about different things as followups to this question.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!