This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 14:440:127– Introduction to Computers for Engineers Notes for Lecture 12 Rutgers University, Fall 2008 Instructor- Blase E. Ur 1 Symbolic Calculus Recall that last week, we saw numerical methods to estimate the answers to calculus problems. This was useful whenever we had a data set we collected and needed to perform calc operations on that data, or when the function describing the data is fairly complicated. However, if you are dealing with a relatively simple equations, you can use symbolic math, which deals with ”symbols” rather than estimates. For instance, taking the derivative of x 2 would return 2 x rather than an estimate at some particular point. 1.1 Intro to Symbolic Math In general, to tell Matlab that you’re dealing with symbolic numbers, you pass a string to the function sym( ) , which makes that string into a symbolic data type. Once you have symbolic data types, you can perform math operations on them, but the answers will be the exact mathematical symbols rather than estimates, i.e.: >> sym(’2’) * sym(’x^2’) ans = 2*x^2 Notice that rather than looking up what the variable x contains, Matlab here keeps x as a symbol. Alternately, you can specify which ”variables” should be considered symbolic by typing something like syms x y , which will make x and y symbolic variables. From then on, whenever you refer to x or y, Matlab will treat them as a symbol rather than as a variable. >> syms x y >> 2*x^3 ans = 2*x^3 1.2 Making Symbolic Math All Neat and Pretty There are functions like collect , expand , factor , and simplify , among others. You may be able to guess what these do by the names (collect like terms, expand powers, factor an expression, or put the expression into its simplest possible form). For instance, let’s see how collect and factor work: >> syms x y >> collect(x^2 - 3*x^2 + 5 - 2*x + 32*x) ans =-2*x^2+5+30*x >> factor(x^3-1) ans = (x-1)*(x^2+x+1) You can also solve equations using the solve function: >> syms x y >> solve(’x^2 + 5*x -3 = 11’) ans = 2-7 If you’ll notice, here I had to put the equation in quotes (because of the equals sign). How do you know when you can just type it normally, have to put it in quotes, or even have to use the sym function? Rather than try to memorize the complex set of rules, try typing it the first way, and then try it the other ways if it gives you an error. One function you might find useful when you’re presenting the output from your curve fitting is poly2sym , which converts a polynomial (stored as a vector) into a symbolic expression. This allows you to display the best fit equation you find as symbols: >> a = 1:3; >> b = a.^2+2*a; >> c = polyfit(a,b,2) c = 1.0000 2.0000-0.0000 >> poly2sym(c) ans = x^2+2*x-2453841957234431/2535301200456458802993406410752 >> poly2sym(round(c)) ans = x^2+2*x Notice the complication from the fact that Matlab gives you an estimated solution... I decided to round the coefficients to the nearest integer before displaying them so that we don’t show 0 as some...
View Full Document
This note was uploaded on 11/09/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Fall '08 term at Rutgers.
- Fall '08