Doing Math with Python by Amit Saha
Author:Amit Saha
Language: eng
Format: mobi, epub, azw3, pdf
Publisher: No Starch Press, Inc.
Published: 2015-08-25T04:00:00+00:00
Plotting Expressions Input by the User
The expression that you pass to the plot() function must be expressed in terms of x only. For example, earlier we plotted y = 2x + 3, which we entered to the plot function as simply 2x + 3. If the expression were not originally in this form, we’d have to rewrite it. Of course, we could do this manually, outside the program. But what if you want to write a program that allows its users to graph any expression? If the user enters an expression in the form of 2x + 3y – 6, say, we have to first convert it. The solve() function will help us here. Let’s see an example:
>>> expr = input('Enter an expression: ')
Enter an expression: 2*x + 3*y - 6
➊ >>> expr = sympify(expr)
➋ >>> y = Symbol('y')
>>> solve(expr, y)
➌ [-2*x/3 + 2]
At ➊, we use the sympify() function to convert the input expression to a SymPy object. At ➋, we create a Symbol object to represent 'y' so that we can tell SymPy which variable we want to solve the equation for. Then we solve the expression to find y in terms of x by specifying y as the second argument to the solve() function. At ➌, this returns the equation in terms of x, which is what we need for plotting.
Notice that this final expression is stored in a list, so before we can use it, we’ll have to extract it from the list:
>>> solutions = solve(expr, 'y')
➍ >>> expr_y = solutions[0]
>>> expr_y
-2*x/3 + 2
We create a label, solutions, to refer to the result returned by the solve() function, which is a list with only one item. Then, we extract that item at ➍. Now, we can call the plot() function to graph the expression. The next listing shows a full graph-drawing program:
'''
Plot the graph of an input expression
'''
Download
Doing Math with Python by Amit Saha.epub
Doing Math with Python by Amit Saha.azw3
Doing Math with Python by Amit Saha.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Modelling of Convective Heat and Mass Transfer in Rotating Flows by Igor V. Shevchuk(6200)
Weapons of Math Destruction by Cathy O'Neil(5778)
Factfulness: Ten Reasons We're Wrong About the World – and Why Things Are Better Than You Think by Hans Rosling(4454)
Descartes' Error by Antonio Damasio(3137)
A Mind For Numbers: How to Excel at Math and Science (Even If You Flunked Algebra) by Barbara Oakley(3075)
Factfulness_Ten Reasons We're Wrong About the World_and Why Things Are Better Than You Think by Hans Rosling(3025)
TCP IP by Todd Lammle(2982)
Applied Predictive Modeling by Max Kuhn & Kjell Johnson(2857)
Fooled by Randomness: The Hidden Role of Chance in Life and in the Markets by Nassim Nicholas Taleb(2833)
The Tyranny of Metrics by Jerry Z. Muller(2818)
The Book of Numbers by Peter Bentley(2744)
The Great Unknown by Marcus du Sautoy(2516)
Once Upon an Algorithm by Martin Erwig(2454)
Easy Algebra Step-by-Step by Sandra Luna McCune(2433)
Lady Luck by Kristen Ashley(2386)
Practical Guide To Principal Component Methods in R (Multivariate Analysis Book 2) by Alboukadel Kassambara(2358)
Police Exams Prep 2018-2019 by Kaplan Test Prep(2332)
All Things Reconsidered by Bill Thompson III(2241)
Linear Time-Invariant Systems, Behaviors and Modules by Ulrich Oberst & Martin Scheicher & Ingrid Scheicher(2210)
