1 / 30

CSE 123

CSE 123. Symbolic Processing. Declaring Symbolic Variables and Constants. To enable symbolic processing, the variables and constants involved must first be declared as symbolic objects . For example, to create the symbolic variables with names x and y:. >> syms x y.

lemuel
Download Presentation

CSE 123

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 123 Symbolic Processing

  2. Declaring Symbolic Variables and Constants To enable symbolic processing, the variables and constants involved must first be declared as symbolic objects. For example, to create the symbolic variables with names x and y: >> syms x y To declare symbolic constants, the sym function is used. >>a = 3 *sqrt(2) a = 4 . 2426 >>b = 3 *sqroot2 b= 3*2^(1/2) >> pi = sym(’pi’); >> delta = sym(’1/10’); >> sqroot2 = sym(’sqrt(2)’); The advantage of using symbolic constants is that they maintain full accuracy until a numeric evaluation is required.

  3. Symbolic Expressions Symbolic variables can be used in expressions and as arguments of functions in much the same way as numeric variables have been used. >> syms s t A >> f = s^2 + 4*s + 5 f = s^2+4*s+5 >> g = s + 2 g = s+2 >> h = f*g h = (s^2+4*s+5)*(s+2) >> z= exp(-s*t) z = exp(-s*t) >> y = A*exp(-s*t) y = A*exp(-s*t) >>n = 3 ; >>syms x ; >>A = x.^((0:n)'*( 0:n))

  4. Manipulating Polynomial Expressions The Matlab commands for this purpose include:

  5. Manipulating Polynomial Expressions >> syms s >> A = s+2; >> B = s+3; >> C = A*B C = (s+2)*(s+3) >> C = expand(C) C = s^2+5*s+6 >> syms s >> A = s^4 -3*s^3 -s +2; >> B = 4*s^3 -2*s^2 +5*s -16; >> C = A + B C = s^4+s^3+4*s-14-2*s^2 >> syms s >> D = s^2 + 6*s + 9; >> D = factor(D) D = (s+3)^2 >> P = s^3 - 2*s^2 -3*s + 10; >> P = factor(P) P = (s+2)*(s^2-4*s+5) >> syms s >> A = s^4 -3*s^3 -s +2; >> C = 3*A C = 3*s^4-9*s^3-3*s+6

  6. Manipulating Polynomial Expressions Consider the expressions >> syms s >> H = -(1/6)/(s+3) -(1/2)/(s+1)+(2/3)/s; >> [N,D] = numden(H) N = s+2 D = (s+3)*(s+1)*s >> D = expand(D) D = s^3+4*s^2+3*s >> syms s >> G = s+4 + 2/(s+4) + 3/(s+2); >> [N,D] = numden(G) N = s^3+10*s^2+37*s+48 D = (s+4)*(s+2) >> D = expand(D) D = s^2+6*s+8

  7. Manipulating Polynomial Expressions Cancellation of terms: >> syms s >> H = (s^3 +2*s^2 +5*s +10)/(s^2 + 5); >> H = simplify(H) H = s+2 >> factor(s^3 +2*s^2 +5*s +10) ans = (s+2)*(s^2+5)

  8. Manipulating Polynomial Expressions Variable substitution: >>syms x >>f = 2*x^2 - 3*x + 1 >>subs(f,2) >>ans = 3 >>syms x y >>f = x^2*y + 5*x*sqrt(y) >>subs(f, x, 3) >>ans = 9*y+15*y^(1/2) >>subs(f, y, 3) >>ans = 3*x^2+5*x*3^(1/2) >> syms s >> H = (s+3)/(s^2 +6*s + 8); >> G = subs(H,s,s+2) G = (s+5)/((s+2)^2+6*s+20) >> G = collect(G) G = (s+5)/(s^2+10*s+24)

  9. Manipulating Polynomial Expressions The function poly2 sym (p) converts a coefficient vector p to a symbolic polynomial. The form poly2sym (p , ' v ' ) generates the polynomial in terms of the variable v. For example, >>poly2sym ( [2 , 6 , 4] ) ans = 2 *x^2+6 *x+4 >> poly2sym( [5 , -3, 7) , ' y ' ) ans= 5*y^2 - 3 *y+7 The function sym2poly (E) converts the expression E to a polynomial coefficient vector. >>syms x >>sym2poly(9 *x^2+4 *x+ 6) 946

  10. Manipulating Trigonometric Expressions Trigonometric expressions can also be manipulated symbolically in Matlab, primarily with the use of the expand function. >> syms theta phi >> A = sin(theta + phi) A = sin(theta+phi) >> A = expand(A) A = sin(theta)*cos(phi)+cos(theta)*sin(phi) >> B = cos(2*theta) B = cos(2*theta) >> B = expand(B) B = 2*cos(theta)^2-1

  11. Solving Algebraic and Transcendental Equations The symbolic math toolbox can be used to solve algebraic and transcendental equations, as well as systems of such equations. The function used in solving these equations is solve. There are several forms of solve, solve(E1, E2,...,EN) solve(E1, E2,...,EN, var1, var2,...,varN) where E1, E2,...,EN are the names of symbolic expressions and var1, var2,..., varN are variables in the expressions that have been declared to be symbolic. The solutions obtained are the roots of the expressions; that is, symbolic expressions for the variables under the conditions E1=0, E2 = 0, . . . EN = 0.

  12. Solving Algebraic and Transcendental Equations >> syms theta x z >> E = z*cos(theta) - x; >> theta = solve(E,theta) theta = acos(x/z) >> syms s >> E = s+2; >> s = solve(E) s = -2 >> syms s >> D = s^2 +6*s +9; >> s = solve(D) s = [ -3] [ -3]

  13. Calculus Limits The Symbolic Math Toolbox enables you to calculate the limits of functions directly. >>syms h n x >>limit( (cos(x+h) - cos(x))/h,h,0 ) >>ans = -sin(x)

  14. Calculus Limits >>limit(x/abs(x),x,0,'left') >>ans = -1 >>limit(x/abs(x),x,0,'right') >>ans = 1 >>limit(x/abs(x),x,0) >>ans = NaN

  15. Calculus Differentiation The diff function, when applied to a symbolic expression, provides a symbolic derivative. diff(E) Differentiates a symbolic expression E with respect to its free variable as determined by findsym. diff(E,v) Differentiates E with respect to symbolic variable v. diff(E,n) Differentiates E n times for positive integer n.

  16. Calculus Differentiation >> syms s n >> p = s^3 + 4*s^2 -7*s -10; >> d = diff(p) d = 3*s^2+8*s-7 >> e = diff(p,2) e = 6*s+8 >> f = diff(p,3) f = 6 >> g = s^n; >> h = diff(g) h = s^n*n/s >> h = simplify(h) h = s^(n-1)*n

  17. Calculus Integration The int function, when applied to a symbolic expression, provides a symbolic integration. int(E) Indefinite integral of symbolic expression E with respect to its symbolic variable as defined by findsym. If E is a constant, the integral is with respect to x. int(E,v) Indefinite integral of E with respect to scalar symbolic variable v. int(E,a,b) Definite integral of E with respect to its symbolic variable from a to b, where a and b are each double or symbolic scalars.

  18. Calculus Integration >> syms x >> int(1/x) ans = log(x) >> int(cos(x)) ans = sin(x) >> int(1/(1+x^2)) ans = atan(x) >> int(exp(-x^2)) ans = 1/2*pi^(1/2)*erf(x) >> syms x n a b t >> int(x^n) ans = x^(n+1)/(n+1) >> int(x^3 +4*x^2 + 7*x + 10) ans = 1/4*x^4+4/3*x^3+7/2*x^2+10*x >> int(x,1,t) ans = 1/2*t^2-1/2 >> int(x^3,a,b) ans = 1/4*b^4-1/4*a^4

  19. Calculus Symbolic summation: symsum() >>syms x k >> s1 = symsum(1/k^2,1,inf) >> s2 = symsum(x^k,k,0,inf) s1 = 1/6*pi^2 s2 = -1/(x-1)

  20. Example 1 Two polynomials in the variable x are represented by the coefficient vectors p1= [6 , 2 , 7 , -3] and p2 = [10 , -5 , 8) . a. Use MATLAB to calculate product of these two polynomials; express the product In Its simplest form. b. Use MATLAB to find the numeric value of the product if x = 2. >>p1 = poly2sym([6, 2, 7, -3]); >> p2 = poly2sym([10, -5, 8]); >> p3 = p1*p2; >> expand(p3) ans = 60*x^5-10*x^4+108*x^3-49*x^2+71*x-24 >>syms x >>subs(ans,x,2) ans = 2546

  21. Example 2 Use MATLAB to solve the polynomial equation x 3+ 8x2+ ax + 10 = 0, for x in terms of the parameter a, and evaluate your solution for the case a = 17. Use MATLAB to check the answer. >>syms a x >>E = x^3+8*x^2+a*x+10 >>solve(E,x) >>subs(ans, a, 17); ans = -1.0000 - 0.0000i -5.0000 + 0.0000i -2.0000 - 0.0000i

  22. Example 3 In terms of the parameter b, use MATLAB to find the points of intersection of the two ellipses described by Evaluate the solution obtained in part a for the case b = 2. >>subs(S.x,b,2) ans = 0.9685 -0.9685 0.9685 -0.9685 >>subs(S.y,b,2) ans = 0.4976 -0.4976 0.4976 -0.4976 syms b x y E = x^2+y^2/b^2-1; F = x^2/100+4*y^2-1; S = solve(E,F);

  23. Example 4 Use MATLAB to determine all the local minima and local maxima and all the inflection points of the following function: >>d2ydx2 = diff(y,2); >>solve(d2ydx2) ans = [2/3] [2] >>p1 = subs(d2ydx2,0) p1 = 16 >>p2 = subs(d2ydx2,2) p2 = 0 >>p3 = subs(d2ydx2,2/3) p3 = 0 >>syms x >>y = x^4-(16/3)*x^3+8*x^2-4; >>dydx = diff(y); >>solve(dydx) ans = 0 2 2

  24. Example 5 A certain object has a mass m = 100 kg and is acted on by a force f(t) = 500[2 - e-tsin(5πt)] N. The mass is at rest at t=0. Use MATLAB to compute the object's velocity at t= 5 s. The equation of motion is f=ma. syms t f = exp(-t)*sin(5*pi*t); v = double(50-5*int(f,0,5)) v = 49.6808

  25. Example 6 Use MATLAB to compute the following limits:

  26. Example 6 Find the expression for the sum of the geometric series syms r k n symsum(r^k,k,0,n-1); simplify(ans) ans = (r^n-1)/(r-1)

  27. Example 7 Given the expressions: E1= x3- 15x2+ 75x - 125 and E2= (x + 5)2 - 20x, use MATLAB to a. Find the product E1 E2 and express it in its simplest form. b. Find the quotient E 1/ E2 and express it in its simplest form . c. Evaluate the sum E 1 + E2 at x = 7.1 in symbolic form and in numeric form. >>syms x >>E1 = x^3-15*x^2+75*x-125; >>E2 = (x+5)^2-20*x; >>S1 = E1*E2; >>factor(S1) ans = (x-5)^5 >>S2 = E1/E2; >>simplify(S2) ans = x-5 >>S3 = E1+E2; >>G = sym(subs(S3,x,7.1)) G = 7696088813222736*2^(-49) >>G = simplify(G) G = 481005550826421/35184372088832 >>H = double(G) H = 13.6710

  28. Example 8 Use MATLAB to solve the equation syms x solve(sqrt(1-x^2)-x) ans = 1/2*2^(1/2) Use MATLAB to solve the equation set x + 6y = a, 2x - 3y = 9 for x and y in terms of the parameter a. >>S = solve(‘x+6*y=a’,’2*x-3*y=9’); >>S.x ans = 18/5+1/5*a >>S.y ans = -3/5+2/15*a

  29. Example 9 Given that y = x sin(3x), use MATLAB to find . syms x int(x*sin(3*x)) ans = 1/9*sin(3*x)-1/3*x*cos(3*x) Given that z = 6y2tan(8x), use MATLAB to find syms x y int(6*y^2*tan(8*x),y) ans = 2*y^3*tan(8*x)

  30. Example 10 The shape of a cable hanging with no load other than its own weight is a catenary curve. A particular bridge cable is described by the catenary y(x) =10 cosh((x – 20)/ 10) for 0 ≤ x ≤ 50, where x and y are the horizontal and vertical coordinates measured in feet. The length L of a curve described by y(x) for a ≤ x ≤ b can be found from the following integral: syms x y = 10*cosh((x-20)/10); dydx = diff(y); L = double(int(sqrt(1+dydx^2),0,50)) L = 136.4474

More Related