Snoopysclassroom.weebly.com



Notes: Simple Plotting with PylabPylab is a simple plotting library included with Python that can fulfill a lot of the basic needs to plot various mathematical functions. It is simpler to use than matplotlib.The simplest way to use Pylab to plot is to declare a matrix of points, with a minimum and maximum value. Then call the linspace function, passing this data. The following code shows plotting several trig functions with specific x and y limits on the same graph.# Pylab plotting demonstrationsimport pylabimport mathn=1000xmin, xmax = -2*math.pi,2*math.pix=pylab.linspace(xmin, xmax, n)y1=pylab.cos(x)y2=pylab.sin(x)y3=pylab.tan(x)pylab.ylim(-5,5)pylab.plot(x,y1)pylab.plot(x,y2)pylab.plot(x,y3)pylab.show()Labels can be added to the plot by calling the label= command within the pylab.plot function call. Let us enhance the above code.pylab.plot(x,y1, label=’cos(x)’)pylab.plot(x,y2, label=’sin(x)’)pylab.plot(x,y3, label=’tan(x)’)pylab.legend() #this call must precede the call to show() in order for the legends to displayTitles can be added using the pylab.title command.The marker styles and colors of each plot can be modified using the marker= and color= references within the plot command.The following web links give all the possibilities for each: also is the linestyle command to change the way the lines look on the plot. HYPERLINK "; \l "matplotlib.lines.Line2D.set_linestyle" following plot pretty much puts all this together.The following code illustrates how to plot a pair of quadratic functions on the same graph using pylab.# Pylab quadratic plotting demonstrationimport pylabn=1000xmin, xmax = -10, 10x=pylab.linspace(xmin, xmax, n)y=x**2-4*x+7y2=x**2+2*x-3pylab.plot(x,y, label="x^2-4*x+7")pylab.plot(x,y2, label="x^2+2*x-3")pylab.grid()pylab.xlabel("x")pylab.ylabel("y")pylab.legend()pylab.show()The code for quadratic plotter is uploaded to my weebly site under the Python sublink, click to Code. They are listed as Pylabdemo.pyNext, I want to discuss some of the Sympy library tools/commands. Sympy (SymbolicPython) is a Python programming library that allows you to work with variables, or what we know also as symbolic mathematics.Here I am going to introduce an example of each Sympy command and how it is used.To be sure your output appears in the console window when typed, be sure your tools/preferences/Ipython console/Display/Background Color radio button is set to Light BackgroundSome essential Sympy commandsA complete list may be found at:docs.You may search there for specifics on these commands and many otherssimplify—simplifies an expressionexpand—expands a mathematical expressionfactor – factors a polynomialcollect—collects terms of a function from highest power to leastcancel—simplifies fractional polynomialsapart—performs partial fraction decompositiontrigsimp—simplifies trigonometric functionsexpand_trig—expands trigonometric functionspowsimp—combines powersexpand_power_exp—expands powers of an exponentevalf substitutes a value into a function to evaluate it at that value. import mathimport sympy as spx=sp.Symbol('x') #must define as a variablesp.simplify((sp.sin(x))**2+(sp.cos(x))**2)Out[4]: 1 sp.expand(x*(x+1)**2)Out[5]: x^3+2*x^2+xsp.factor(x*x-3*x-18)Out[6]: (x+6)(x+3)func=(x*x+2*x+1)/(x+1)sp.cancel(func,x)Out[8]: x+1sp.powsimp(x**2*x**3)Out[9]: x^5a=Symbol('a') #must define as a variableb=Symbol('b') #must define as a variablesp.powsimp(x**a*x**b) ##combines powersOut[20]: x^(a+b)sp.expand_power_exp(x**(a+b)) #expand powersOut[59]: x**a*x**bFF=sp.sin(x)/(x*x)FF.evalf(subs={x:sp.pi/6}) #can use subsOut[71]: 1.82378130556208res=sp.solve(3*x**2+8*x-5)pprint(res) #pprint command prints in a common mathematical form? 4 √31 √31 4??- ─ + ───, - ─── - ─?? 3 3 3 3?Note that using the Solve command, the function must always be put as = 0 before it is sent to the solver. Note the sp.factor and sp.solve commands above. You will be asked to use these in the following homework problems. Your take home portion of exam 2 will ask you to use Python to do things.Practice: 1. Using Python’s Pylab plotting capabilities, plot the following functions, and try to determine their roots using the commands/plotting method shown in the pylabdemo.py program above, that is also uploaded to my Weebly site. You may download the program and modify it accordingly for the purpose.2*x^2-12*x+10=0x^2+3*x-6=02. Find the roots of these equations using the Sympy solve command.3. Factor these equations using the Sympy factor command.4. Do problem 70, parts a-d on page 298. Plot the given function using Pylab. Show the code and the graph produced by the function. You can save the graph image with the save file button located on the graph when it displays.As part of solving this problem, use Sympy’s solver command and find the roots of the function. You will need these values as they are part of solving the problem. 5. Do problem 77 p. 307. Remember that you must put the equation in the formAx^2 + Bx + C = 0 before you can send it to the solver. To prepare for the in class portion of your test, the chapter review beginning on p. 318 will suffice. You may skip problems 8,9,10 and 45-48. These will not be on the test.Take Home Test Using Python’sPython Take Home Exam1. Plot all 5 of the functions, using pylab, that you worked with in problem 39, p. 270 on the same graph. Create different colors for each graph, label the x and y axes, give the plot a title, and generate the legend box for the graph.2. Solve 6r2-7r-5=0 using the Sympy Solve command3. Factor 21x2-77x+28 using the Sympy Factor command4. Solve problem 77, p. 307 using the Sympy Solve command. Then using pylab plot the function and answer part b.5. Solve problem 69 on p. 298 using the Sympy Solve command. Again, recall that in order to solve for t the entire function must be set = 0. 50 million in thousands is 50000.Assign this solution to the variable S.You will need to get an approximate decimal value for the solutions returned by the solver.Discard the negative solution and issue the commandfloat(S[0]), or float(S[1]), depending on which solution printed from Python is the positive one. These two solutions are stored in what is called a Python list, and the [0] and [1] chooses which solution to convert to a floating point number. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download