You are on page 1of 26

ECE 1010 ECE Problem Solving I

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

ECE 1010 ECE Problem Solving I

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

Chapter 9: Symbolic Algebra

92

ECE 1010 ECE Problem Solving I

findsym(S,1) ans = x

% Find the one variable closest to 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

To see what this is doing type whos


S = sym('sin(x + z)'); syms a b x y whos Name Size Bytes S a b x y 1x1 1x1 1x1 1x1 1x1 144 126 126 126 126

Class sym sym sym sym sym object object object object object

Grand total is 19 elements using 648 bytes

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

ECE 1010 ECE Problem Solving I

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)

Chapter 9: Symbolic Algebra

94

ECE 1010 ECE Problem Solving I

Simplifications To simplify symbolic mathematical expressions we have the functions listed in Table 9.1
Table 9.1: Simplification functions

Function collect(S) expand(S) factor(S) simple(S) simplify(S)

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

collect(S,v) Collect coefficients with respect to v

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)

Chapter 9: Symbolic Algebra

95

ECE 1010 ECE Problem Solving I

% 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)

Description Transforms S into the Horner nested polynomial format

Chapter 9: Symbolic Algebra

96

ECE 1010 ECE Problem Solving I

Table 9.2: Arithmetic operators (Continued)

Function [N,D] = numden(S) sym(S,?); where ? may be f,r,e, or d

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

poly2sym(c) pretty(S) sym2poly(S) A+B A/B A*B S^p A-B

Symbolic subtraction A B

Chapter 9: Symbolic Algebra

97

ECE 1010 ECE Problem Solving I

Examples: 1p1 = ---------y3 3y p2 = ----------y+2 p3 = ( y + 4 ) ( y 3 ) y


syms y p1 = 1/(y-3); p2 = 3*y/(y+2); p3 = (y+4)*(y-3)*y; % Now operate: p1*p2 ans = 3/(y-3)*y/(y+2)

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)

Chapter 9: Symbolic Algebra

98

ECE 1010 ECE Problem Solving I

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

Chapter 9: Equation Solving

99

ECE 1010 ECE Problem Solving I

To solve a system of equations use


g = solve(eq1,eq2,...,eqn,var1,var2,...,varn);

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

Chapter 9: Equation Solving

910

ECE 1010 ECE Problem Solving I

Solutions to Differential Equations


The symbolic toolbox can solve single differential equations and systems of differential equations A detailed study of differential equations is beyond the scope of this course, but as discussed in Chapter 8, linear constant coeffieint differential equations are of particular interest to electrical engineers, and hence worth investigating again briefly Revisit the RLC Circuit of Chapter 8 R L vi ( t ) + i(t) C vo ( t )

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

ECE 1010 ECE Problem Solving I

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

ECE 1010 ECE Problem Solving I

ezplot(y1,[0 100]) axis([0 80 0 1.5])

The exact, closed-form, solution to this second-order differential equation is 1v o ( t ) = 1 ----51e 17 e


t ----10 t ----10

1 ----sin 51 t 30

(9.3)

1cos ----51t , t 0 30

1-1/17*51^(1/2)*exp(-1/10*t)*sin( ~~~ exp(-1/10*t)*cos(1/30*51^(1/2)*t 1.5

vo ( t )
0.5

time in seconds
0 0 10 20 30 40 t 50 60 70 80

Chapter 9: Solutions to Differential Equations

913

ECE 1010 ECE Problem Solving I

Differentiation and Integration


In Chapter 7 we investigated numerical differentiation and integration. In this section true symbolic solutions will be obtained. Differentiation The MATLAB function for performing symbolic differentiation is diff(f,t,n) where f is the symbolic expression to differentiate t is an optional attribute used to specify the variable of differentiation (there may be more than one symbol present); if not specifed the letter closes to x is used n specifies the order of the derivative, with one beging the default if n is omitted Examples: f 1 ( x ) = 6 x 4 x + bx 5 f 2 ( a ) = sin ( a ) 1t f 3 ( t ) = -----------4 1+t
syms x a b t f1 = 6*x^3-4*x^2+b*x-5; f2 = sin(a); f3 = (1 - t^3)/(1 + t^4); diff(f1) ans = 18*x^2-8*x+b

Chapter 9: Differentiation and Integration

914

ECE 1010 ECE Problem Solving I

diff(f1,2) ans = 36*x-8 diff(f1,b) ans = x diff(f2) ans = cos(a) diff(f2,x) ans = 0

% Expected since not a function of x

diff(f3) ans = -3*t^2/(1+t^4)-4*(1-t^3)/(1+t^4)^2*t^3 simplify(diff(f3)) ans = t^2*(-3+t^4-4*t)/(1+t^4)^2

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

ECE 1010 ECE Problem Solving I

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

Chapter 9: Differentiation and Integration

916

ECE 1010 ECE Problem Solving I

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

Chapter 9: Differentiation and Integration

917

ECE 1010 ECE Problem Solving I

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

The MATLAB symbolic solution is


syms fn Fs a Hfs = (1 - a)^2/(1 - 2*a*cos(2*pi*fn) + a^2); pretty(Hfs) 2 (1 - a) ------------------------2 1 - 2 a cos(2 pi fn) + a Bnn = int(Hfs,'fn',0,1); pretty(Bnn) 2 (1 + 2 a + a ) tan(pi (1/2 - fn)) atan(---------------------------------) 1/2 %1 --------------------------------------1/2 %1 pi

lim fn -> 0+

Chapter 9: Differentiation and Integration

918

ECE 1010 ECE Problem Solving I

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

ECE 1010 ECE Problem Solving I

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

In the above we used the symbolic function


subs(S,old,new)

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)

Chapter 9: Differentiation and Integration

920

ECE 1010 ECE Problem Solving I

ylabel('Bn in KHz','fontsize',14) xlabel('a','fontsize',14) grid

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

Problem Solving Applied: Weather Balloons


Weather balloons are used to gather atmospheric temperature and pressure data as a function of balloon altitude The balloon is filled with helium and thus rises until a point of equilibrium is reached as a result of reduced air density During the day the balloon rises higher due to sunlight
Chapter 9: Problem Solving Applied: Weather Balloons 921

ECE 1010 ECE Problem Solving I

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

Chapter 9: Problem Solving Applied: Weather Balloons

922

ECE 1010 ECE Problem Solving I

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

ECE 1010 ECE Problem Solving I

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

ECE 1010 ECE Problem Solving I

% 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

The results of running weather_balloon.m


weather_balloon Altitude in meters, t hours 4 - 3/25 t + 12 t 3 2 - 380 t + 4100 t + 220

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

Acceleration in meters/sec^2, t hours

Chapter 9: Problem Solving Applied: Weather Balloons

925

ECE 1010 ECE Problem Solving I

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

Balloon Alt., Vel., & Acc. VS Time

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

Chapter 9: Problem Solving Applied: Weather Balloons

926

You might also like