259 Lecture 18 The Symbolic Toolbox
The Symbolic Toolbox MATLAB has a set of built-in commands that allow us to work with functions in a fashion similar to Mathematica. For more on the commands available, type “help Symbolic Toolbox”. Octave can also perform much of the same functionality via the “symbolic” package – we will look at how to install this package after looking at the MATLAB features! There is also an online version of Octave! See Octave-Online.net (http://octave-online.net/). The commands we will look at are: sym, syms, diff, int, simplify, pretty, subs, double, and ezplot. In MATLAB, for help on these commands, use the help file. If a symbolic command has the same name as a numerical command, such as “diff”, typing “help sym/commandname” will give help on the symbolic command. Try “help diff” and “help sym/diff”.
sym To define symbolic variables, use the “sym” command. Example 1a: Here’s how to define variables x, y, and a, and functions f(x) = x3 + 2x2 +x-1 g(x) = sin(x) h(y) = (a2-y2)/(y+1) x = sym(‘x’) y = sym(‘y’) a = sym(‘a’) f = x^3+2*x^2+x-1 g = sin(x) h = sqrt(a^2-y^2)/(y+1) Typing “pretty(h)” will make h(y) look nicer! In Octave, this is done “automatically”! Note that “syms x y a” also works to define symbolic variables!
diff To find the derivative of a function defined symbolically, we use the “diff” command. Example 2a: Find the following derivatives of the functions from Example 1a: f’(x), f’’(x), g’(x), h’’’(y). Also find dy/dx if y = x + x2 – x4 or y = tan(x)sin(x) -ln(x)/ex diff(f) fprime = diff(f) f2prime = diff(f,2) gprime = diff(g) h3prime = diff(h,3) pretty(h3prime) simplify(h3prime) pretty(simplify(h3prime)) diff(x + x^2 - x^4) diff(tan(x)^sin(x) -log(x)/exp(x)) pretty(ans)
subs To evaluate a symbolic function we use the command “subs”. Example 3a: For the functions defined in Example 1a, find each of the following: f(-3) f(v) where v = [1 2 4] g’(pi/4) h(1) h(1) with a = 2 h(y) with a = 2 subs(f,x,-3) subs(f,-3) v = [1 2 4] subs(f,v) subs(gprime,pi/4) subs(h,1) b = subs(h,1) subs(b,2) subs(h,a,2)
ezplot We can plot symbolic functions with the command “ezplot”! Example 4a: Use ezplot to graph the functions f(x), g(x), g’(x), f’(x), and f’’(x) defined in Example 1a. Note that the default settings for ezplot can be changed with title, xlabel, and ylabel. The default x-interval of [-2, 2] can also be changed. ezplot(f) ezplot(f,[-1,1]) ezplot(g) ezplot(gprime,[0,2*pi]) One way to plot multiple graphs via ezplot: hold on ezplot(fprime) ezplot(f2prime) title('Plot of f and it''s derivatives.') hold off
int int(f) int(g,0,pi) int(h,y,0.5,1) pretty(ans) int(h,a,0.5,1) To find indefinite or definite integrals in MATLAB, we use “int”. Example 5: Find each integral: int(f) int(g,0,pi) int(h,y,0.5,1) pretty(ans) int(h,a,0.5,1) int(x + x^2 - x^4)
funtool Finally, here’s a way to work with functions that is more “user friendly”. Typing “funtool” brings up a function calculator in MATLAB. funtool is a visual function calculator that manipulates and displays functions of one variable. At startup, funtool displays graphs of a pair of functions, f(x) = x and g(x) = 1. The graphs plot the functions over the domain [-2*pi, 2*pi]. funtool also displays a control panel that lets you save, retrieve, redefine, combine, and transform f and g. This command won’t work in Octave.
Symbolic Package in Octave To perform symbolic manipulation in Octave, first we need to load the “symbolic” package. The symbolic package, as well as other add-ons can be found here: Octave downloads from Source Forge (both Windows and Mac OS X installers can be found here: http://octave.sourceforge.net/ Octave Homepage: http://www.gnu.org/software/octave/ A reference for the “symbolic” package can be found here (choose “Function Reference”): http://octave.sourceforge.net/symbolic/index.html
Symbolic Functions in Octave Note that to run the symbolic package in Octave, Python (https://www.python.org/) and SymPy (http://www.sympy.org/en/index.html) must also be installed. For Windows, I used version 2.4.0 of the symbolic package, version 3.6 of Python and version 0.7.6.1 of SymPy.
Symbolic Functions in Octave To install the symbolic package from Octave, make sure that the file symbolic-2.4.0.tar.gz is located in the current directory. Type “pkg install symbolic-2.4.0.tar.gz”. Once the “symbolic” package is installed, turn on Octave and type ‘pkg load symbolic” to load the symbolic package or “pkg load all” to load all installed packages.
References Using MATLAB in Calculus by Gary Jenson MATLAB Help File 12