Lesson 3: Solving Equations

Report 12 Downloads 200 Views
Lesson 3: Solving Equations restart;

Equations and how to solve them Last time we solved a quadratic equation. solve(x^2 - 3*x - 4); (1.1) There are two solutions, and , and Maple gives us both. Of course, the solution of a quadratic equation could involve square roots. solve(x^2 - 3*x - 5); (1.2) ... or complex numbers. solve(x^2 - 3*x + 4); (1.3) Maple uses for instead of . How about a cubic? solve(x^3 - 3*x + 4); (1.4)

Three solutions, two of which involve (the commas separating the solutions are sometimes hard to spot). What are their numerical values? It would have been useful to have saved the result in a variable. I could have said solutions := solve(x^3 - 3*x + 4); (1.5)

But actually I didn't need to redo the command, because there is a special variable: % . Maple always assigns this the result of the last command. So I could have just said

solutions := %; (1.6)

There's also % % for the second-last and % % % for the third-last (but that's as far back as it goes). Now that I have the three solutions, I can refer to any particular one as s o l u t i o n s [ 1 ], s o l u t i o n s [ 2 ] or s o l u t i o n s [ 3 ], putting the number in square brackets. solutions[2]; (1.7) evalf(%); (1.8) Next a polynomial of degree 5: solutions := solve(x^5 - x^3 + 2*x + 1); (1.9)

What does this somewhat bizarre notation mean? RootOf just means "a solution of": it writes the polynomial, using its own variable instead of the variable you used, and then = (something from 1 to 5). That's Maple's way of keeping track of which solution is which. So RootOf( just means the fourth solution of this equation, in whatever numbering Maple is using. evalf(solutions[4]); (1.10) There's a good reason Maple doesn't give you a formula for the solutions of this polynomial: there is no general formula (not in terms of radicals, at least) for roots of a polynomial of degree 5 or more. Now let's try an equation not involving a polynomial. solve(sin(x)=1/2, x); (1.11) By the way, remember, it's sin(x), not sin x or sin*x or sinx. That's one solution, but not the only one. But there's a way to get Maple to give us the others. To find out how, try the help system. ?solve _EnvAllSolutions:= true; (1.12)

(1.12) solutions:= solve(sin(x)=1/2, x); (1.13) (or etc) is a variable that can take values 0 or 1 (B for binary). could be any integer (Z for the German Zahl). The ~ tells you that Maple has made an assumption on this variable. Maple uses names that start with the underscore _, because it wants to avoid using names that you're already using. about(_B1); Originally _B1, renamed _B1~: is assumed to be: OrProp(0,1)

about(_Z1); Originally _Z1, renamed _Z1~: is assumed to be: integer

Maple needed to use something like this to express the solutions of the equation, because there are infinitely many of them. We say it's a parametric solution. To get a particular solution, we want to give values to the parameters and . We can do this using eval. eval(solutions, {_B1 = 1, _Z1 = 0}); (1.14) "eval" is short for "evaluate". What this does is evaluate the first item (solutions) after making the substitutions given by the second item (an equation or a set of equations). We can also use eval to check that this really is a solution, by plugging it back into the equation. eval(sin(x)=1/2, x=%); (1.15) Here's a harder equation. eq := x * sin(x) = Pi/2; (1.16) solve(eq); (1.17) Maple doesn't know the solutions, even though there are two that are easy to guess. evalf(%); (1.18) Here's something rather cool. What number is this? identify(%); (1.19)

(1.19)

Plotting Are there other solutions? One thing that's almost always a good idea is to draw a graph. plot(x*sin(x), x=-20..20);

15

10

5

0

It would be handy to see the line

10 x

20

too. Maple can plot several things in the same graph. You

put the different items to plot within square brackets. plot([x*sin(x), Pi/2], x = -10 .. 10);

6

4

2

0

5 x

10

It looks like there are infinitely many solutions. Note that if you click inside the plot, you can see the coordinates of the "target" cursor in the context bar. This lets you locate any point on the plot with better precision than you would have guessing "by eye", especially for points not on the axes. You can also resize the plot by grabbing and dragging the little boxes on the border of the plot window. By the way, notice that you don't plot an equation; what I plotted were the left and right sides of the equation. plot(eq, x=-10..10); Error, invalid input: plot expects its 1st argument, p, to be of type {set, array, list, rtable, algebraic, procedure, And (`module`, appliable)}, but received x*sin(x) = (1/2)*Pi I could have extracted the left or right side of the equation with the commands lhs and rhs. lhs(eq); (2.1) rhs(eq); (2.2)

So I could have said something like this. While I'm at it, I'll change the colours. plot([lhs(eq), rhs(eq)], x = -10 .. 10, colour=[blue,black]);

6

4

2

0

5 x

10

fsolve In addition to solve which finds exact solutions (when possible), Maple has fsolve which finds a numerical approximation to a solution of an equation, even those that "solve" can't handle. Let's see how it handles our equation: fsolve(eq); (3.1) If we want a different solution, we can specify an interval we want fsolve to look in. For example: fsolve(eq, x = 5 .. 10); 6.526260523 (3.2) Is there a formula for that one? identify(%); 6.526260523

(3.3)

Probably not. If fsolve can't find a solution in the interval (usually because there isn't any), it returns unevaluated.

For example, it looks like there won't be any solution between fsolve(eq, x = 5 .. 6);

and

. (3.4)

Usually fsolve returns one solution (if it can find one). The exception is for polynomials (or equations where both sides are polynomials in the variable), where fsolve returns all the real solutions (or all solutions in the specified interval). p := x^5 + x^3 - 3*x + 1; (3.5) fsolve(p = 0, x); (3.6) fsolve(p, x = 0 .. 1); (3.7) If you do want complex solutions, you can add the option complex. fsolve(p, x, complex); (3.8) Here's another difficult equation to solve. eq := sin(x)^2 = exp(-x)*cos(x); (3.9) By the way, note that exp is the exponential function. In Maple input, it's exp(-x), not e^(-x); to Maple, e is nothing special, just another name. Maple shows exp(-x) as in output, though if you look closely, you'll see that the e is in a different font than what it would use for the name e. e^(-x); e (3.10) Let's say I'm interested in positive solutions. plot([lhs(eq),rhs(eq)], x = 0 .. 25, colour=[red,blue]);

1

0 5

10

15

20

25

x It's a bit hard to see the blue curve because part of it is hidden by the x axis. We can get that out of the way. plot([lhs(eq),rhs(eq)], x = 0 .. 25, colour=[red,blue], axes = box);

1

0 0

5

10

15

20

25

x There's certainly one solution near x = .63, maybe some near x = 6.3, x = 9.4, etc, but it's hard to tell. We're interested in what happens near y = 0: most of the graph is wasted from that point of view. Notice that Maple automatically chooses the y interval to accommodate the curves it's plotting. But we can tell plot what y interval we want, if we don't like the one Maple chose. plot([lhs(eq),rhs(eq)], x = 0 .. 10, y = -0.01 .. 0.01, colour=[red,blue], axes = box);

y

0

0

2

4

6

8

10

x It looks like there are solutions near 6.3, but maybe not near 9.4. fsolve(eq, x = 6.1 .. 6.5); 6.325488468

(3.11)

fsolve(eq, x = 9.2 .. 9.6); (3.12) Actually a bit of thought and analysis explains why this is so. When is more than 5 or so, the makes the right side very close to 0. The left side, being the square of something, is always , and = 0 only when is a multiple of If is near an even multiple of , ; the right side is greater than the left side when is exactly an even multiple of , so they should be equal at some points on either side of those multiples of . On the other hand, if is near an odd multiple of , so there's no chance of a solution.

Maple objects introduced in this lesson RootOf I %

%% %%% _EnvAllSolutions about eval identify plot lhs rhs colour fsolve .. complex