259 Lecture 18 The Symbolic Toolbox The Symbolic

259 Lecture 18 The Symbolic Toolbox

The Symbolic Toolbox o o o o 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. n n 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”. 2

sym o To define symbolic variables, use the “sym” command. o Example 1 a: Here’s how to define variables x, y, and a, and functions o f(x) = x 3 + 2 x 2 +x-1 o g(x) = sin(x) o h(y) = (a 2 -y 2)/(y+1) o o o o o 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! 3

diff o To find the derivative of a function defined symbolically, we use the “diff” command. o Example 2 a: Find the following derivatives of the functions from Example 1 a: f’(x), f’’(x), g’(x), h’’’(y). o Also find dy/dx if y = x + x 2 – x 4 or o y = tan(x)sin(x) -ln(x)/ex diff(f) fprime = diff(f) f 2 prime = diff(f, 2) gprime = diff(g) h 3 prime = diff(h, 3) pretty(h 3 prime) simplify(h 3 prime) pretty(simplify(h 3 prime)) diff(x + x^2 - x^4) diff(tan(x)^sin(x) log(x)/exp(x)) o pretty(ans) o o o o o 4

subs o To evaluate a symbolic function we use the command “subs”. o Example 3 a: For the functions defined in Example 1 a, find each of the following: o f(-3) o f(v) where v = [1 2 4] o g’(pi/4) o h(1) with a = 2 o h(y) with a = 2 o o o o o 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) 5

ezplot o We can plot symbolic functions with the command “ezplot”! o Example 4 a: Use ezplot to graph the functions f(x), g’(x), f’(x), and f’’(x) defined in Example 1 a. o Note that the default settings for ezplot can be changed with title, xlabel, and ylabel. o The default x-interval of [-2 , 2 ] can also be changed. o o o ezplot(f) ezplot(f, [-1, 1]) ezplot(gprime, [0, 2*pi]) One way to plot multiple graphs via ezplot: n n n ezplot(f) hold on ezplot(fprime) ezplot(f 2 prime) title('Plot of f and it''s derivatives. ') hold off 6

int o To find indefinite or definite integrals in MATLAB, we use “int”. o Example 5: Find each integral: o o o o int(f) int(g, 0, pi) int(h, y, 0. 5, 1) pretty(ans) int(h, a, 0. 5, 1) pretty(ans) int(x + x^2 - x^4) 7

funtool o o o o 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. 8

Symbolic Package in Octave o To perform symbolic manipulation in Octave, first we need to load the “symbolic” package. o The symbolic package, as well as other add-ons can be found here: n Octave downloads from Source Forge (both Windows and Mac OS X installers can be found here: http: //octave. sourceforge. net/ n Octave Homepage: http: //www. gnu. org/software/octave/ o A reference for the “symbolic” package can be found here (choose “Function Reference”): n http: //octave. sourceforge. net/symbolic/index. html 9

Symbolic Functions in Octave o Note that to run the symbolic package in Octave, Python (https: //www. python. org/) and Sym. Py (http: //www. sympy. org/en/index. html) must also be installed. o For Windows, I used version 2. 4. 0 of the symbolic package, version 3. 6 of Python and version 0. 7. 6. 1 of Sym. Py. 10

Symbolic Functions in Octave o To install the symbolic package from Octave, make sure that the file symbolic -2. 4. 0. tar. gz is located in the current directory. o Type “pkg install symbolic-2. 4. 0. tar. gz”. o 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. 11

References o Using MATLAB in Calculus by Gary Jenson o MATLAB Help File 12
- Slides: 12