Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
1. A Practical Introduction to Mathematica 106 1.5.7 Solving Equations An expression like x^2 + 2 x - 7 == 0 represents an equation in Mathematica. You will often need to solve equations like this, to find out for what values of x they are true. This gives the two solutions to the quadratic equation x2 + 2x ; 7 = 0. The solutions are given as replacements for x. In 1]:= Solve x^2 + 2x - 7 == 0, x] Here are the numerical values of the solutions. In 2]:= N % ] You can get a list of the actual solutions for x by applying the rules generated by Solve to x using the replacement operator. In 3]:= x /. % You can equally well apply the rules to any other expression involving x. In 4]:= x^2 + 3 x /. %% Solvelhs == rhs, x] x /. solution expr /. solution -2 + 4 Sqrt2] -2 - 4 Sqrt2] Out 1]= {{x -> ---------------------------}, {x -> ---------------------------}} 2 2 Out 2]= {{x -> 1.82843}, {x -> -3.82843}} Out 3]= {1.82843, -3.82843} Out 4]= {8.82843, 3.17157} solve an equation, giving a list of rules for x use the list of rules to get values for x use the list of rules to get values for an expression Finding and using solutions to equations. Solve always tries to give you explicit formulas for the solutions to equations. However, it is a basic mathematical result that, for sufficiently complicated equations, explicit algebraic formulas cannot be given. If you have an algebraic equation in one variable, and the highest power of the variable is at most four, then Mathematica can always give you formulas for the solutions. However, if the highest power is five or more, it may be mathematically impossible to give explicit algebraic formulas for all the solutions. Mathematica can always solve algebraic equations in one variable when the highest power is less than five. In 5]:= Solve x^4 - 5 x^2 - 3 == 0, x] 5 + Sqrt37] 5 + Sqrt37] Out 5]= {{x -> Sqrt-----------------------]}, {x -> -Sqrt-----------------------]}, 2 2 5 - Sqrt37] 5 - Sqrt37] {x -> Sqrt-----------------------]}, {x -> -Sqrt-----------------------]}} 2 2 It can solve some equations that involve higher powers. In 6]:= Solve x^6 == 1, x] I/3 Pi (2 I)/3 Pi }, {x -> E }, Out 6]= {{x -> 1}, {x -> E (4 I)/3 Pi (5 I)/3 Pi {x -> -1}, {x -> E }, {x -> E }} Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected]; http://www.wolfram.com/; 1-800-441-6284. 1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. 1.5 Symbolic Mathematics 107 There are some equations, however, for which it is mathematically impossible to find explicit formulas for the solutions. Mathematica uses the function Roots to represent the solutions in this case. In 7]:= Solve 2 - 4 x + x^5 == 0, x] Even though you cannot get explicit formulas, you can still find the solutions numerically. In 8]:= N % ] 5 Out 7]= {ToRulesRoots-4 x + x == -2, x]]} Out 8]= {{x -> -1.51851}, {x -> -0.116792 - 1.43845 I}, {x -> -0.116792 + 1.43845 I}, {x -> 0.508499}, {x -> 1.2436}} In addition to being able to solve purely algebraic equations, Mathematica can also solve some equations involving other functions. After printing a warning, Mathematica returns one solution to this equation. In 9]:= Solve Sin x] == a, x ] Solve::ifun: Warning: inverse functions are being used by Solve, so some solutions may not be found. Out 9]= {{x -> ArcSina]}} It is important to realize that an equation such as sin(x) = a actually has an infinite number of possible solutions, in this case differing by multiples of 2 . However, Solve by default returns just one solution, but prints a message telling you that other solutions may exist. Section 3.4.4 discusses this in more detail. There is no explicit “closed form” solution for a transcendental equation like this. In 10]:= Solve Cos x] == x, x ] Solve::ifun: Warning: inverse functions are being used by Solve, so some solutions may not be found. Out 10]= SolveCosx] == x, x] You can find an approximate numerical solution using FindRoot, and giving a starting value for x. In 11]:= FindRoot Cos x] == x, {x, 1} ] Out 11]= {x -> 0.739085} Solve can also handle equations involving symbolic functions. In such cases, it again prints a warn- ing, then gives results in terms of formal inverse functions. Mathematica returns a result in terms of the formal inverse function of f. In 12]:= Solve f x^2] == a, x ] Solve::ifun: Warning: inverse functions are being used by Solve, so some solutions may not be found. (-1) (-1) a]]}, {x -> -Sqrtf a]]}} Out 12]= {{x -> Sqrtf Solve{lhs1 ==rhs1 , lhs2 ==rhs2 , ... }, {x, y, ... }] solve a set of simultaneous equations for x, y, ... Solving sets of simultaneous equations. Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected]; http://www.wolfram.com/; 1-800-441-6284. 1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. 1. A Practical Introduction to Mathematica 108 You can also use Mathematica to solve sets of simultaneous equations. You simply give the list of equations, and specify the list of variables to solve for. Here is a list of two simultaneous equations, to be solved for the variables x and y. Here are some more complicated simultaneous equations. The two solutions are given as two lists of replacements for x and y. In 13]:= Solve {a x + y == 0, 2 x + (1-a) y == 1}, {x, y}] 1 a 2 -2 + a - a -2 + a - a Out 13]= {{x -> -(---------------------), y -> ---------------------}} 2 In 14]:= Solve {x^2 + y^2 == 1, x + 3 y == 0}, {x, y}] -3 1 Out 14]= {{x -> ---------------, y -> ---------------}, Sqrt10] Sqrt10] 3 1 {x -> ---------------, y -> -(---------------)}} Sqrt10] Sqrt10] This uses the solutions to evaluate the expression x + y. In 15]:= x + y /. % -2 2 Out 15]= {---------------, ---------------} Sqrt10] Sqrt10] Mathematica can solve any set of simultaneous linear equations. It can also solve a large class of simultaneous polynomial equations. Even when it does not manage to solve the equations explicitly, Mathematica will still usually reduce them to a much simpler form. When you are working with sets of equations in several variables, it is often convenient to reorganize the equations by eliminating some variables between them. This eliminates y between the two equations, giving a single equation for x. In 16]:= Eliminate {a x + y == 0, 2 x + (1-a) y == 1}, y] 2 Out 16]= -(a x) + a x == 1 - 2 x If you have several equations, there is no guarantee that there exists any consistent solution for a particular variable. There is no consistent solution to these equations, so Mathematica returns {}, indicating that the set of solutions is empty. In 17]:= Solve {x==1, x==2}, x] There is also no consistent solution to these equations for almost all values of a. In 18]:= Solve {x==1, x==a}, x] Out 17]= {} Out 18]= {} The general question of whether a set of equations has any consistent solution is quite a subtle one. For example, for most values of a, the equations {x==1, x==a} are inconsistent, so there is no possible solution for x. However, if a is equal to 1, then the equations do have a solution. Solve is set up to give you generic solutions to equations. It discards any solutions that exist only when special constraints between parameters are satisfied. Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected]; http://www.wolfram.com/; 1-800-441-6284. 1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited. 1.5 Symbolic Mathematics 109 If you use Reduce instead of Solve, Mathematica will however keep all the possible solutions to a set of equations, including those that require special conditions on parameters. This shows that the equations have a solution only when a == 1. The notation x==1 && x==a represents the requirement that both x==1 and x==a should be True. In 19]:= Reduce {x==1, x==a}, x] This gives the complete set of possible solutions to the equation. The answer is stated in terms of a combination of simpler equations. && indicates equations that must simultaneously be true; || indicates alternatives. In 20]:= Reduce a x + b == 0, x] This gives a more complicated combination of equations. In 21]:= Reduce a x^2 + b == 0, x] Out 19]= a == 1 && x == 1 b Out 20]= a != 0 && x == -(-) || a == 0 && b == 0 a b b a a Out 21]= a != 0 && (x == Sqrt-(-)] || x == -Sqrt-(-)]) || a == 0 && b == 0 You can use LogicalExpand to manipulate the set of equations. In 22]:= LogicalExpand %] b Out 22]= a != 0 && x == Sqrt-(-)] || a b a != 0 && x == -Sqrt-(-)] || a == 0 && b == 0 a Solvelhs==rhs, x] solve an equation for x Solve{lhs1 ==rhs1 , lhs2 ==rhs2 , ... }, {x, y, ... }] solve a set of simultaneous equations for x, y, ... Eliminate{lhs1 ==rhs1 , lhs2 ==rhs2 , ... }, {x, ... }] eliminate x, ... in a set of simultaneous equations Reduce{lhs1 ==rhs1 , lhs2 ==rhs2 , ... }, {x, y, ... }] give a set of simplified equations, including all possible solutions Functions for solving and manipulating equations. Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected]; http://www.wolfram.com/; 1-800-441-6284. 1991 Wolfram Research, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.