![]() This module acts as a library for symbolic calculationĪnd is quite easy to use yet surprisingly powerful for it’s complexity. # Use explicit namespaces to make clear from which package a function comes. import sympy as sp import numpy as np import matplotlib.pyplot as pl # Define a symbol x = sp. Symbol ( "x" ) # A symbolic expression expr = sp. sin ( x ) + x ** 2 # We can evaluate it for any value of x: print ( expr. Plotting a 3D plot is similar to plotting a 2D plot, but we need to specify ranges for two variables instead of one. subs () # Again we can evaluate the expression for any value of x easily: print ( fs ( 2 )) print ( fs ( sp. There is a lot you can do with the 3D plots. Try rotating the plot above by clicking and dragging the mouse inside of the plot. pi )) # But we want the function to work with the arrays from numpy too. # Thus we have to lambdify the expression. ![]() In this series of tutorials Id like to implement the same functionality from scratch, so youll be able to clearly see each step and learn the language in the process. Lambdify ( x, expr, "numpy" ) xvals = np. At the very high level, the calculator is a loop that gets a line of text from the user and then calculates and displays the result. linspace ( - 5, 5, 100 ) yvals = fn ( xvals ) # Compute the derivative of our expression dexpr = sp. diff ( expr, x ) # And again lambdify it fnd = sp. lambdify ( x, dexpr, "numpy" ) # Evaluate the derivative on the given grid dyvals = fnd ( xvals ) # Compute the second derivative of our expression ddexpr = sp. diff ( expr, x, 2 ) # And again lambdify it fndd = sp. lambdify ( x, ddexpr, "numpy" ) # Evaluate the derivative on the given grid ddyvals = fndd ( xvals ) # And plot the function sampled at the grid points xvals # Note how we reuse the symbolic expressions for generating the plot labels too! pl. plot ( xvals, yvals, label = r"$ %s $" % sp. plot ( xvals, dyvals, label = r"$ %s $" % sp. plot ( xvals, ddyvals, label = r"$ %s $" % sp. savefig ( "symbolics_demo.png" )įor many other simple examples, please look at the introductory level sympy tutorial. ![]() There are two major modes of typesetting math in LaTeX one is embedding the math directly into your text by encapsulating your formula in dollar signs and the other is using a predefined math environment. You can follow along and try the code in your computer or online using overleaf. Using inline math – embed formulas in your text I also prepared a quick reference of math symbols.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |