> j := 2*x^3-5*x^2-2*x+5; > factor(j); > plot(j,x=-100..100); > plot(j,x=-3..3);The plot command is used to verify that there are exactly three roots for this expression. As the two plot commands show, it is sometimes difficult to see exactly how many roots there are based on the
> factor(sin(x)+3); > plot(sin(x)+3,x=-4*Pi..4*Pi); > factor(x*sin(x)-1); > plot(x*sin(x)-1,x=-50..50);When an expression is already in factored form or cannot be factored, the Maple output is the same expression that was entered. Remember, not every expression has roots. That is, some expressions, when plotted, don't intersect with the
> solve(equation,variable);The following example illustrates how we can find the roots of the function
> f := x-> 2*x^3-5*x^2-2*x+5; > solve(f(x)=0,x); > solve(2*x^3-5*x^2-2*x+5=0,x);Here the ``='' sign is used in the equation, not ``:='' which is used for assignment. If you forget to type in an equation and only type in an expression without setting it equal to zero, Maple automatically sets the expression equal to zero. The solve command is not only used for solving for zeros, it can be used to solve other equations as well. In the examples below, you can see some of the solving capabilities of Maple.
> solve(sin(x)=tan(x),x); > solve(x^2+2*x-1=x^2+1,x);
Unfortunately, many equations cannot be solved analytically. For example, even the relatively simple equation sin(x) = x/2 has no analytical solution.
In this case, the only possibility is to solve it numerically. In Maple, the command to use is fsolve. The syntax for fsolve is very similar to that of solve. A simple example will show how we can find solutions to the equation
. When the solve command is used, the output looks like:
> solve(sin(x)=x/2,x); RootOf(_Z-2sin(_Z))This is not incorrect, as some of the zeros of a function may be imaginary and others may be real. However, it is much better to solve numerically as shown below:
> fsolve(sin(x) = x/2, x);Note that the result is a decimal approximation and is not exact. Also, a plot of both equations on the same graph will show that this solution is not complete. There are two other intersection points that the fsolve command did not output. The fsolve command allows us to solve the equation in a range of
> plot({sin(x),x/2},x=-2*Pi..2*Pi); > fsolve(sin(x) = x/2, x=-3..-1); > fsolve(sin(x) = x/2, x=-1..1); > fsolve(sin(x) = x/2, x=1..3);Once you have solved an equation, you may want to use the output or the solution later. In order to label the output to a solution, you need to assign a label in the same line as the solve or fsolve command. For example,
> expr2 := x^2 + 2*x - 5; > answer := solve(expr2=0,x); > evalf(subs(x=answer[1], expr2));Here, an expression was defined first and then the solution was assigned to the label ``answer''. Note that there was more than one solution. In order to substitute the answer that was listed first back into the expression, the subs command was used and [1] was added onto the variable name answer to distinguish the first solution from the second.