Professional Documents
Culture Documents
Symbolic Mathematics
Introduction
MATLAB is probably best known for its capability to perform numerical computations. This is particularly true when you consider the native capability of MATLAB itself. With the addition of the Symbolic Math Toolbox (included in the student edition) and the Extended Symbolic Math Toolbox, however, MATLAB can perform true symbol computations. MATLAB achieves its symbolic capability via the symbolic processor engine from Waterloo Maple Software, Inc. This symbolic engine is a derivative of the stand-alone Maple V software package. In the PC lab we have the full/professional Symbolic Math Toolbox installed for MATLAB version 5 The course text was written for MATLAB 4 There are some differences in the way symbolic math is handled between versions 4 and 5 The notes that follow deviate from the text to account for these differences; Some old methods will not work in MATLAB version 5
Chapter 9: Introduction
91
Overview of the Capabilities The Maple engine provides many capabilities, such as: factor, simplify, etc. equation solving solve differential equations differentiation integration linear (matrix) algebra Fourier, Laplace, and z-transforms Numerical evaluation of expressions to a specified accuracy
Symbolic Algebra
Symbolic Expressions A symbolic expression in MATLAB is created as a character string using the function sym(S) where S is a string, e.g.,
S = sym(sin(x + z)); S = sym(2*x^3 + x); S = sym(1/(cos(x) + angle)); S = sym(exp(x^2));
When symbolic expressions are created as in the above examples, there may be more than one variable present To find the variables in a symbolic expression or matrix we can use the function findsym(S)
S = sym('sin(x + z)'); findsym(S) ans = x, z % Finds all variables
92
findsym(S,1) ans = x
The variation findsym(S,n) finds the n variable names closest to the letter x If we wish to create some working variables, as opposed to expressions, we can use the shorthand function syms
a = sym(a); b = sym(b); x = sym(x); y = sym(y); % This is a lot of typing, instead use syms a b x y
Class sym sym sym sym sym object object object object object
We see that we have created sym(bolic) objects The ability to create objects as opposed to just plain variables is a new feature in MATLAB 5, hence the differences between symbolic operations in versions 4 and 5
Chapter 9: Symbolic Algebra 93
Note: We will need to create variables like this to perform symbolic math operations Once a symbolic expression is defined using sym(S) we can take advantage of some powerful symbolic toolbox features such as ezplot ezplot(S) plots the expression in S on the interval 2 to 2 assuming it contains only one variable ezplot(S,[xmin,xmax]) plots the expression in S on the specified interval
S = sym('sin(x) + 1/3*sin(3*x)'); ezplot(S)
94
Simplifications To simplify symbolic mathematical expressions we have the functions listed in Table 9.1
Table 9.1: Simplification functions
Description Collect coefficients of S Expand S into products of factors Factorization of S Attempts to put S into its simplest form Simplifies S using Maples simplification rules
Example: S1 = x 1 S2 = ( x 3 ) + ( y 4 )
2 2 3
S1 = sym('x^3 - 1'); S2 = sym('(x-3)^2 + (y-4)^2'); %%% Alternate construction %%%%%%%%%%%%% syms x y S1 = x^3 - 1; S2 = (x-3)^2 + (y-4)^2; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Now perform some simplifications factor(S1) ans = (x-1)*(x^2+x+1)
95
% Try the pretty form help pretty %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PRETTY Pretty print Maple string. PRETTY(S) prints the Maple string in a format that resembles type-set mathematics. PRETTY(S,n) uses screen width n instead of the default 79. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pretty(factor(S1)) 2 (x - 1) (x + x + 1) pretty(expand(S2)) 2 2 x - 6 x + 25 + y - 8 y pretty(collect(S2)) 2 x - 6 x + 9 + (y - 4) 2
Operations on Symbolic Expressions Once an expression or variable is declared as a symbolic object we can perform standard mathematical operations such a given in Table 9.2
Table 9.2: Arithmetic operators
Function horner(S)
96
Description returns two symbolic numerator and denominator expressions Symbolic to numeric conversion: f is floating point, r = rational form, e = rational plus an error term, d = decimal expansion when proceeded by digits(N) Converts a polynomial vector c into a symbolic polynomial Produces a typeset type display Converts a symbolic polynomial S into a polynomial vector Symbolic addition of A + B Symbolic division of A B Symbolic multiplication of AB Symbolic power S
p
Symbolic subtraction A B
97
p1*p3 ans = (y+4)*y p2^3 ans = 27*y^3/(y+2)^3 p1+p2 ans = 1/(y-3)+3*y/(y+2) [num,den] = numden(p1+p2) num = -8*y+2+3*y^2 den = (y-3)*(y+2)
98
Comments on Practice! p. 229: The instructions for S1, S2, and S3 will not work in MATLAB 5, instead use
syms x S1 = 1/(x+4); S2 = x^2+8*x+16; S3=(x+4)*(x-2);
Equation Solving
With symbolic math we can solve a single equation or a system of equations. Solutions to Equations To solve a single equation we use
g = solve(eq,var);
that is find what value of the independent variable makes eq = 0; var is optional If more than one variable is present, the variable to solve for set with var
S = sym('x^2 + 3*x + 18'); solve(S) x = [ -3/2+3/2*i*7^(1/2), -3/2-3/2*i*7^(1/2)] syms x a b c S = sym('a*x^2 + b*x + c'); % Contains four variables solve(S,x) % Solve for x x = [ 1/2/a*(-b+(b^2-4*a*c)^(1/2)), 1/2/a*(-b-(b^2-4*a*c)^(1/2))] %quadratic equation
99
The var1, ...,varn are the variables to solve for They must be declared as symbolics using syms
syms x y z eq4 = sym('3*x+2*y-z=10'); eq5 = sym('-x+3*y+2*z=5'); eq6 = sym('x-y-z=-1'); solve(eq4,eq5,eq6,x,y,z) x = -2 y = 5 z = -6
Comments for Practice! p. 230: Follow the example given above or try the alternative format given below, where all the equations are set equal to zero by moving all non zero terms to the lefthand side:
syms x y z solve(3*x+2*y-z-10,-x+3*y+2*z-5,x-y-z+1) x = -2 y = 5 z = -6
910
In Chapter 8 it was shown that d vo ( t ) dv o ( t ) -------------- + vo ( t ) v i ( t ) = RC + LC ----------------2 dt dt or LCv o'' ( t ) + RCv o' ( t ) + v o ( t ) = v i ( t ) (9.2)
2
(9.1)
Using the dsolve() function we can symbolically solve (9.2) Here will assume the input is v i ( t ) = 1, t 0
Chapter 9: Solutions to Differential Equations 911
We also must supply boundary or initial conditions Assuming the circuit is initially at rest dv o ( 0 ) v o ( 0 ) = 0, ---------------- = 0 dt
syms R L C y t % Create symbolic objects y = dsolve('L*C* D2y + R*C* Dy + y = 1','y(0) = 0',... 'Dy(0) = 0','t') pretty(y) % Display general solution in a nice format
2 1/2 2 2 1/2 (-R C + (-C (-R C + 4 L)) ) t ((R C - 4 L C) + R C) exp(1/2 --------------------------------) L C 1 - 1/2 -------------------------------------------------------------------2 2 1/2 (R C - 4 L C) + 1/2 2 1/2 1/2 (R C + (-C (-R C + 4 L)) ) t (R C - (R C - 4 L C) ) exp(- 1/2 -------------------------------) L C --------------------------------------------------------------------2 2 1/2 (R C - 4 L C) 2 2
% Solve the special case: R=1, L=5, C=3 y1 = dsolve('15* D2y + 3* Dy + y = 1','y(0) = 0',... 'Dy(0) = 0','t'); pretty(y1) % The output for special values of R,L, &C 1/2 1 - 1/17 51 exp(- 1/10 t) sin(1/30 51 1/2 - exp(- 1/10 t) cos(1/30 51
Chapter 9: Solutions to Differential Equations
1/2 t)
t)
912
1 ----sin 51 t 30
(9.3)
1cos ----51t , t 0 30
vo ( t )
0.5
time in seconds
0 0 10 20 30 40 t 50 60 70 80
913
914
diff(f1,2) ans = 36*x-8 diff(f1,b) ans = x diff(f2) ans = cos(a) diff(f2,x) ans = 0
Comments for Practice! p. 233 Make sure to use the MATLAB 5 symbolic notation, e.g.,
syms x g1 = x^3 - 5*x^2 + 2*x + 8; % An so on .........
Note: Check the first and second derivatives by hand to see if you agree Integration The MATLAB function for performing symbolic integration is int(f,t,a,b) or int(f,t,m,n) where f is the symbolic expression to integrate
Chapter 9: Differentiation and Integration 915
t optionally specifies the variable of integration for the case of several different symbols; if not specifed the letter closes to x is used a and b optionally specify numerical limits of integration m and n optionally specify symbolic limits of integration Examples:
( 6 x 4 x + bx 5 ) dx
sin a da x dx
syms x a b g1 = 6*x^3 - 4*x^2 + b*x - 5; g2 = sin(a); g3 = sqrt(x); int(g1) ans = 3/2*x^4-4/3*x^3+1/2*b*x^2-5*x int(g1,'b') ans = 6*x^3*b-4*b*x^2+1/2*b^2*x-5*b int(g2) ans = -cos(a) int(g2,0,pi) ans = 2
916
int(g2,0,2*pi) ans = 0 % Expected for the area under a full cycle int(g3) ans = 2/3*x^(3/2) int(g3,'a','b') ans = 2/3*b^(3/2)-2/3*a^(3/2) int(g3,1,2) ans = 4/3*2^(1/2)-2/3 vpa(int(g3,1,2),50) % Variable precision arithmetic ans = 1.2189514164974600650689182989462641047595625005025
Example: Noise Equivalent Bandwidth Calculation assigned at the end of Chapter 7 (special problem not in the text) Bn =
Fs
H ( f ) df
(4)
where H ( f ) is the digital filter frequency response with f in Hz For a first-order digital lowpass filter the frequency response magnitude is given by 1a H ( f ) = -----------------------------------------------------2 2 f - +a 1 2 a cos ------F s (5)
where F s is the sampling rate in Hz and a is a filter parameter which controls bandwidth
917
Solve this integral symbolically we first simplify the integral via a change of variables, that is let ff n = ---Fs then (1 a) - df n B n = F s --------------------------------------------------2 0 1 2 a cos ( 2 f ) + a n
2
lim fn -> 0+
918
2 (1 + 2 a + a ) tan(pi (1/2 - fn)) atan(---------------------------------) a 1/2 %1 - 2 ----------------------------------------1/2 %1 pi 2 (1 + 2 a + a ) tan(pi (1/2 - fn)) 2 atan(---------------------------------) a 1/2 %1 + -----------------------------------------1/2 %1 pi 2 (1 + 2 a + a ) tan(pi (1/2 + fn)) atan(---------------------------------) 1/2 %1 - --------------------------------------1/2 %1 pi 2 (1 + 2 a + a ) tan(pi (1/2 + fn)) atan(---------------------------------) a 1/2 %1 + 2 ----------------------------------------1/2 %1 pi
Chapter 9: Differentiation and Integration 919
2 (1 + 2 a + a ) tan(pi (1/2 + fn)) 2 atan(---------------------------------) a 1/2 %1 - -----------------------------------------1/2 %1 pi 2 2 %1 := (1 - 2 a + a ) (1 + 2 a + a ), substitution for %1 vpa(44100*subs(Bnn,a,0.9),10) ans = 2321.052631 % Here Fs=44.1Khz, a=0.9 vpa(44100*subs(Bnn,a,0.4),10) ans = 18900.00000 % Here Fs=44.1Khz, a=0.4
to replace a with a numeric value, here 0.9 and 0.4 We also used variable precision arithmetic to evaluate the limit in the ugly expression for B n To plot the results requires a bit more work
a_vals = 0.4:0.05:0.9; Bn_vals = vpa(44100*subs(Bnn,a,a_vals)); % Convert symbolics to numbers and scale to kHz plot(a_vals,sym2poly(Bn_vals)/1000) title('Lowpass Bn vs Coefficient a','fontsize',16)
920
Lowpass Bn vs Coefficient a
20
18
16
14
Bn in KHz
12
10
2 0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
heating the helium and making it less dense At night the balloon descends to a lower equilibrium altitude A polynomial description of balloon altitude in meters versus time in hours, valid for 48 hours following launch, is the following h ( t ) = 0.12 t + 12 t 380 t + 4100 t + 220 Problem Statement Using the polynomial description determine the velocity and acceleration corresponding to balloon height. Plot balloon altitude, velocity, and acceleration using units of meters, meters/sec, and meters/sec2 all versus hours. Finally, find the maximum altitude and the corresponding time. Input/Output Description Polynomial Altitude Description MATLAB Solution Plot of Altitude Plot of Velocity Plot of Acceleration Peak Altitude/time
4 3 2
922
Hand Calculation Since this problem will use symbolic calculations, hand calculations will not be needed, except to make sure that the required units conversion of hours to seconds is properly carried out in the velocity and acceleration plots. 60 60 seconds t seconds = t hours -----------------------------------1 hours MATLAB Solution A script file will be written to hold all of the required MATLAB statements
% Start of script weather_balloon.m %%%%%% Weather Balloon Calculations Script %%%%%%%% % A script file for performing the required % calculations of % % Altitude (meters) vs Time (hours) % Velocity (meters/sec) vs Time (hours) % Acceleration (meters/sec^2) vs Time (hours) % % The location of the peak altitude and the % corresponding time. % syms t altitude = -0.12*t^4 + 12*t^3 - 380*t^2 + 4100*t + 220; velocity = diff(altitude,'t'); % Units of meters/hour acceleration = diff(altitude,'t',2); % Units of meters/ hour^2 % disp('Altitude in meters, t hours') pretty(altitude,65)
Chapter 9: Problem Solving Applied: Weather Balloons 923
disp('Velocity in meters/hour, t hours') pretty(velocity,65) disp('Acceleration in meters/hour^2, t hours') pretty(acceleration,65) % velocity = velocity/(60*60); acceleration = acceleration/(60*60)^2; disp('**** Change Units ******') disp('Velocity in meters/sec, t hours') pretty(velocity,65) disp('Acceleration in meters/sec^2, t hours') pretty(acceleration,65) % % Plot results using subplot % disp('*****Plot Started*****') tt = 0:0.1:48; alt_coef = sym2poly(altitude); vel_coef = sym2poly(velocity); acc_coef = sym2poly(acceleration); subplot(311) plot(tt,polyval(alt_coef,tt)); grid; title('Balloon Alt., Vel., & Acc. VS Time',... 'fontsize',16); ylabel('Alt.- meters','fontsize',14); subplot(312) plot(tt,polyval(vel_coef,tt)); grid; ylabel('Vel.- m/s','fontsize',14); subplot(313) plot(tt,polyval(acc_coef,tt)); grid; ylabel('Acc.- m/sec^2','fontsize',14); xlabel('Time in Hours','fontsize',14); disp('*****Plot Finished*****') %
Chapter 9: Problem Solving Applied: Weather Balloons 924
% Compute the maximum value of altitude and the % corresponding time using numerical methods. % [max_alt,k] = max(polyval(alt_coef,tt)); max_time = tt(k); fprintf('The maximum altitude is h = %8.2f meters at t = %6.2f hours\n', max_alt(1),max_time(1)); % End of script weather_balloon.m
Velocity in meters/hour, t hours 12 3 2 - -- t + 36 t - 760 t + 4100 25 Acceleration in meters/hour^2, t hours 36 2 - -- t + 72 t - 760 25 **** Change Units ****** Velocity in meters/sec, t hours 3 - 1/7500 t + 1/100 t 2 19 41 - -- t + -90 36
925
2 - 1/9000000 t
19 + 1/180000 t - -----324000
*****Plot Started***** *****Plot Finished***** The maximum altitude is h = 17778.57 meters at t = 42.40 hours
4
x 10
Alt.- meters
1.5 1 0.5 0 2 0 5 10 15 20 25 30 35 40 45 50
Vel.- m/s
1 0 -1 2
0 -5 x 10
10
15
20
25
30
35
40
45
50
Acc.- m/sec2
0 -2 -4 -6 0 5 10 15 20 25 30 35 40 45 50
Time in Hours
926