• Ei tuloksia

Conclusion

In document 1 Introduction to Maple (sivua 50-61)

42 Chapter 2: Mathematics with Maple: the Basics

3 Finding Solutions

This chapter introduces the key concepts needed for quick, concise problem solving in Maple. By learning how to use such tools assolve,map, subs, andunapply, you can save yourself a substantial amount of work.

In addition, this chapter examines how these commands inter-operate.

3.1 Simple solve

Maple’s solvecommand is a general-purpose equation solver. It takes a set of one or more equations and attempts to solve them exactly for the specified set of unknowns. (Recall from section 2.5 that you use braces to denote a set.) In the following examples, you are solving one equation for one unknown, so each set contains only one element.

> solve({x^2=4}, {x});

{x= 2},{x=−2}

> solve({a*x^2+b*x+c=0}, {x});

{x= 1 2

−b+√

b2−4a c

a },{x= 1

2

−b−√

b2−4a c

a }

Maple returns each possible solution as a set. Since both of these equations have two solutions, Maple returns a sequence of solution sets.

If you do not specify any unknowns in the equation, Maple solves for all of them.

43

44 Chapter 3: Finding Solutions

> solve({x+y=0});

{x=−y, y=y}

Here you get only one solution set containing two equations. This result means thaty can take any value, whilex is the negative ofy. This solution is parameterized with respect toy.

If you give an expression rather than an equation, Maple automatically assumes that the expression is equal to zero.

> solve({x^3-13*x+12}, {x});

{x= 1},{x= 3},{x=−4}

Thesolve command can also handle systems of equations.

> solve({x+2*y=3, y+1/x=1}, {x,y});

{x=−1, y= 2},{x= 2, y= 1 2}

Although you do not always need the braces (denoting a set) around either the equation or variable, using them forces Maple to return the so-lution as a set, which is usually the most convenient form. For example, it is a common practice to check your solutions by substituting them into the original equations. The following example demonstrates this procedure.

As a set of equations, the solution is in an ideal form for the subs command. You might first give the set of equations a name, likeeqns, for instance.

> eqns := {x+2*y=3, y+1/x=1};

eqns :={x+ 2y= 3, y+ 1 x = 1}

Then solve.

> soln := solve( eqns, {x,y} );

soln:={x=−1, y= 2},{x= 2, y= 1 2} This produces two solutions:

3.1 Simplesolve 45

> soln[1];

{x=−1, y = 2}

and

> soln[2];

{x= 2, y= 1 2}

Verifying Solutions

To check the solutions, substitute them into the original set of equations using the two-parametereval command.

> eval( eqns, soln[1] );

{1 = 1,3 = 3}

> eval( eqns, soln[2] );

{1 = 1,3 = 3}

For verifying solutions, you will find that this method is generally the most convenient.

Observe that this application of the eval command has other uses.

Suppose you wish to extract the value ofx from the first solution. Again, the best tool is theeval command.

> x1 := eval( x, soln[1] );

x1 :=−1

Alternatively, you could extract the first solution fory.

> y1 := eval(y, soln[1]);

y1 := 2

You can use this evaluation trick to convert solutions sets to other forms. For example, you can construct a list from the first solution

46 Chapter 3: Finding Solutions

where x is the first element and y is the second. First construct a list with the variables in the same order as you want the corresponding solutions.

> [x,y];

[x, y]

Then simply evaluate this list at the first solution.

> eval([x,y], soln[1]);

[−1,2]

The first solution is now a list.

Instead, if you prefer that the solution forycomes first, evaluate[y,x]

at the solution.

> eval([y,x], soln[1]);

[2,−1]

Since Maple typically returns solutions in the form of sets (in which the order of objects is uncertain), remembering this method for extracting solutions is useful.

The map command is another useful command that allows you to apply one operation to all solutions. For example, try substitutingboth solutions.

Themapcommand applies the operation specified as its first argument to its second argument.

> map(f, [a,b,c], y, z);

[f(a, y, z),f(b, y, z),f(c, y, z)]

Due to the syntactical design ofmap, it cannot perform multiple func-tion applicafunc-tions to sequences. Consider the previous solufunc-tion sequence, for example.

> soln;

3.1 Simplesolve 47

{x=−1, y = 2},{x= 2, y= 1 2}

Enclosesoln in square brackets to convert it to alist.

> [soln];

[{x=−1, y= 2},{x= 2, y= 1 2}]

Now use the following command to substitute each of the solutions simultaneously into the original equations,eqns.

> map(subs, [soln], eqns);

[{1 = 1,3 = 3},{1 = 1,3 = 3}]

This method can be valuable if your equation has many solutions, or if you are unsure of the number of solutions that a certain command will produce.

Restricting Solutions

You can limit solutions by specifying inequalities with the solve com-mand.

> solve({x^2=y^2},{x,y});

{x=−y, y=y},{x=y, y=y}

> solve({x^2=y^2, x<>y},{x,y});

{x=−y, y=y}

Consider this system of five equations in five unknowns.

> eqn1 := x+2*y+3*z+4*t+5*u=41:

> eqn2 := 5*x+5*y+4*z+3*t+2*u=20:

> eqn3 := 3*y+4*z-8*t+2*u=125:

> eqn4 := x+y+z+t+u=9:

> eqn5 := 8*x+4*z+3*t+2*u=11:

Now solve the system for all variables.

> s1 := solve({eqn1,eqn2,eqn3,eqn4,eqn5}, {x,y,z,t,u});

48 Chapter 3: Finding Solutions

s1 :={x= 2, u= 16, z=−1, y= 3, t=−11}

You can also choose to solve for a subset of the unknowns. Then Maple returns the solutions in terms of the other unknowns.

> s2 := solve({eqn1,eqn2,eqn3}, { x, y, z});

s2 :={x=−527

13 −7t− 28

13u, z =−70

13 −7t−59 13u, y = 635

13 + 12t+70 13u}

Exploring Solutions

You can explore the parametric solutions found at the end of the previous section. For example, evaluate the solution atu= 1 and t= 1.

> eval( s2, {u=1,t=1} );

{x= −646

13 , z= −220

13 , y= 861 13 }

As in section 3.1, suppose that you require the solutions fromsolve in a particular order. Since you cannot fix the order of elements in a set, solve will not necessarily return your solutions in the order x, y, z.

However, lists do preserve order. Try the following.

> eval( [x,y,z], s2 );

[−527

13 −7t−28

13u, 635

13 + 12t+70

13u,−70

13 −7t−59 13u]

This command not only fixed the order, but it also extracted the right-hand side of the equations. Because the order is fixed, you know the solution for each variable. This capability is particularly useful if you want to plot the solution surface.

> plot3d(%, u=0..2, t=0..2, axes=BOXED);

3.1 Simplesolve 49

–56–58 –52–54 –48–50 –44–46 –42 5055

6065 7075

80 –25

–20 –15 –10 –5

The unapply Command

For convenience, define x = x(u, t), y = y(u, t), and z = z(u, t), that is, convert the solutions to functions. Recall that you can easily select a solution expression for a particular variable using eval.

> eval( x, s2 );

−527

13 −7t−28 13u

However, this is anexpression forx and not a function.

> x(1,1);

x(1,1)

To convert the expression to a function you need another important command,unapply. To use it, provideunapply with the expression and the independent variables. For example,

> f := unapply(x^2 + y^2 + 4, x, y);

f := (x, y)→x2+y2+ 4

produces the function, f, of x and y that maps (x, y) to x2+y2+ 4.

This new function is easy to use.

> f(a,b);

a2+b2+ 4

50 Chapter 3: Finding Solutions

Thus, to make your solution forxa function of bothuandt, the first step is to obtain theexpression forx, as above.

> eval(x, s2);

−527

13 −7t−28 13u

Then useunapplyto turn it into a function of uand t.

> x := unapply(%, u, t);

x:= (u, t)→ −527

13 −7t−28 13u

> x(1,1);

−646 13

You can create the functionsy and z in the same manner.

> eval(y,s2);

635

13 + 12t+70 13u

> y := unapply(%,u,t);

y:= (u, t)→ 635

13 + 12t+70 13u

> eval(z,s2);

−70

13 −7t−59 13u

> z := unapply(%, u, t);

z:= (u, t)→ −70

13−7t−59 13u

> y(1,1), z(1,1);

3.1 Simplesolve 51 861

13 , −220 13

The assign Command

The assign command also allocates values to unknowns. For example, instead of definingx,y, andz as functions, assign each to the expression on the right-hand side of the corresponding equation.

> assign( s2 );

> x, y, z;

−527

13 −7t−28

13u, 635

13 + 12t+70

13u,−70

13 −7t−59 13u

Think of theassigncommand as turning the “=” signs in the solution set into “:=” signs.

Theassigncommand is convenient if you want to assign expressions to names. Remember, though, that while this command is useful for quickly assigning solutions, it cannot create functions.

This next example incorporates solving differential equations, which section 3.6 discusses in further detail. To begin, assign the solution.

> s3 := dsolve( {diff(f(x),x)=6*x^2+1, f(0)=0}, {f(x)} );

s3 := f(x) = 2x3+x

> assign( s3 );

However, you have yet to create a function.

> f(x);

2x3+x

produces the expected answer, but despite appearances, f(x) is simply a name for theexpression 2x3+xand not afunction. Call the function f using an argument other than x.

> f(1);

52 Chapter 3: Finding Solutions

f(1)

The reason for this apparently odd behavior is thatassignasks Maple to do the assignment

> f(x) := 2*x^3 + x;

f(x) := 2x3+x which is not at all the same as the assignment

> f := x -> 2*x^3 + x;

f :=x→2x3+x

The former defines the value of the function f for only the special argument x. The latter defines the function f:x 7→ 2x3 +x so that it works whether you sayf(x),f(y), orf(1).

To define the solutionf as a function of x useunapply.

> eval(f(x),s3);

2x3+x

> f := unapply(%, x);

f :=x→2x3+x

> f(1);

3

The RootOf Command

Maple occasionally returns solutions in terms of the RootOf command.

The following example demonstrates this point.

> solve({x^5 - 2*x + 3 = 0},{x});

In document 1 Introduction to Maple (sivua 50-61)