Kate Allen & Scott Clarke Symbolic Math toolbox – Cheat sheet Create symbolic variables syms x: creates a single symbolic variable x syms x y z: creates multiiple symbolic variables x, y, and z. List variable names required separated by a space after syms Create symbolic expressions and numbers Variables can be used to create symbolic expressions se = sym(‘expression’): assigns expression to the variable se se = a*x^2 + b*x + c: alternative way to create an expression Symbolic numbers give a precise representation of a number sn = sym (c) is used to create a symbolic number 1. >> syms a b c x 5. >> % return numeric value of 2. >> f = sym(‘a*x^2 + b*x + c’) 6. >> % symbolic number 3. >> a = sqrt(sym(2)) 7. >> double(a) 8. ans = 1.4142 4. ans = 2^(1/2) Create symbolic functions First define the variables, then the function 5. % substitute x=2 into f 1. syms x y 6. subs(f, x, 2) 2. f(x,y) = x^2 + y^2 7. ans = 4 + y^2 3. % evaluate f(y+1,y) 4. ans = (y+1)^2 + y^2 Differentiation diff(f, ‘var’, #): • f is the expression of function to differentiate • # is the order of differentiation(optional) • ‘var’ is the variable to differentiate with respect to (optional) 7. >> % using a string expression 1. >> syms x 8. >> syms z 2. >> f = x^3 + x^2 + x + 5 9. >> g = ‘z^3 + 4*a*z^2’ 3. >> diff(f) 10. % note a is a constant 4. ans = 3x^2 + 2x + 1 11. >> diff (g, z, 2) 5. >> diff(f, 3, x) 12. ans = 6z + 8a 6. ans = 6 Integration int(expression, ‘var’) computes the indefinite integral of expression with respect to the symbolic scalar variable var. • Specifying the variable var is optional. • If you do not specify var, int uses the default variable determined by symvar . A constant has the default var x 1. syms x 2. f = x^2 3. int(f) 4. % ans = x^3/3 5. int(f,0,10) 6. % ans = 1000/3 Simplify Symbolic computations – the toolbox provides different methods to simplify expressions as the required form will depend on the problem being addressed collect(f): assumes f is a polynomial in its symbolic variable and gathers all coefficients with the same power of x. 2nd parameter sets variable to collect where f is a function of more than one variable itself e.g. declare x (syms x) and function (f ='2*x + 5*x + 6*x^2’) then use collect(f,x)to obtain 6*x^2 + 7*x expand(f): multiplies out brackets, and applies identities involving functions of sums. horner(f): assumes f is a symbolic polynomial and rewrites f into its nested (or Horner) representation factor(f): assumes f is a polynomial and factorises it over the rational numbers. If this is not possible f is returned in the same form. simplify(f): powerful, general purpose tool. Applies many algebraic identities including functional identities such as trig, log and exp. 1. >> syms x y 2. >> f=x^3 - 6*x^2 + 11*x - 6; 3. >> factor(f) 4. ans = (x-1)(x-2)(x-3) 5. >> horner(f) 6. ans = x(x(x-6)+11)-6 7. >> expand(exp(x+y)) 8. ans = exp(x)*exp(y) 9. >> simplify(exp(x)*exp(y) 10. ans = exp(x+y) Reference & Recommended Reading: http://www.mathworks.co.uk/help/pdf_doc/symbolic/symbolic_tb.pdf Kate Allen & Scott Clarke Symbolic Math toolbox – Cheat sheet Improving on Double Precision Floating Point Accuracy Use exact symbolic numbers to obtain exact computations. No need to concern yourself with round off errors. Possibly the most expensive in terms of time and computer memory. Use variable precision arithmetic using vpa allows balancing the accuracy against the expense of computations by enabling user to set the number of significant figures to a value between 1 and 229 using digits. This can be set to affect all calculations or just a single calculation. 1. >> digits = 5; 2. >> s5=vpa(sym(1/3)+1/2); 3. >> % stored s5=0.83333 4. >> % to only change for 1 calc 5. >> s11=vpa(sym(1/3)+1/2, 11); 6. >> % stored s11=0.83333333333 Estimate precision of numeric to Symbolic conversions The sym command can be used to convert a numeric value to a symbolic form. Optionally the type of form can be chosen. By default the ‘r’ option is used, otherwise chose using sym(num, opt) e.g. sym(t,’e’) ‘f’ - To Floating-Point Symbolic Form: Converts to an exact numeric values of the form N*2e, where N and e are integers, N>0 ‘r’ - Rational Symbolic Form (default): converts to rational form ‘e’ – Rational Symbolic with Machine Precision: same as ‘r’ option plus difference between rational expression and machine floating point value in terms of eps (2-52) ‘d’ – Decimal expansion: decimal expansion up to number of significant digits set by the digits function. Ordinary differential equations Dsolve(ODE, condition 1, condition 2, condition …) • ODE (ordinary differential equation) • Condition(s) are the initial conditions for an ODE, i.e. y(0), f’(0)… Initial conditions are optional but required in order to remove constants of integration. Solve the following first order ordinary differential equation: y’(x)= t2 + 5 with the initial condition: y(0) = 2 1. >> syms y(t) 2. >> dsolve(diff(y, 1) = y*(t2 +5)), y(0) ==2) Solve the following second order ordinary differential equation: f’’(y)=t2+5 with initial conditions: y(0) = 0 and dy(0) = 1 1. >> syms y(t) 2. >> Dy = diff(y) 3. >> dsolve(diff(y,2) == cos(2*t) - y, y(0) == 1, Dy(0) == 0) 4. % Using a string expression: 5. >> syms y(t) 6. >> dsolve ('D2y == cos(2*t) - y, y(0) == 1, Dy(0) ==0') Plotting symbolic functions - The Symbolic Math Toolbox extends existing plotting capabilities in MATLAB ezplot ( f(x) ,[xmin , xmax] ) syms x ezplot (sin(6*x)) Creates a 2D plot of the function f(x) over the domain xmin < x < xmax, or over the default domain -2π<x<2π if [xmin , xmax] omitted. ezplot3 ( x(t), y(t), z(t), [tmin, tmax], ‘animate’ ) ezplot3('sin(t)','cos(t)','t ',[0,6*pi], 'animate') Creates a 3D plot of functions x,y and z over the domain tmin < t < tmax. Including ‘animate’ will create a red ball that traces the plot. ezsurf ( f(x,y) ,[xmin , xmax, ymin, ymax] ) syms x y; z=x^2 + 3*y^2; ezsurf(sin(z/100)); Creates a surface plot of f(x,y) over domain xmin < x < xmax and ymin < y < ymax, or over the default domain -2π<x<2π, -2π<x<2π if omitted. Reference & Recommended Reading: http://www.mathworks.co.uk/help/pdf_doc/symbolic/symbolic_tb.pdf