Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming for Mechanical Engineers (ME 319)

Similar presentations


Presentation on theme: "Introduction to Programming for Mechanical Engineers (ME 319)"— Presentation transcript:

1 Introduction to Programming for Mechanical Engineers (ME 319)
Lecture 8.2: Symbolic Math Toolbox

2 Symbolic object Symbolic object or sym is a MATLAB data type.
The ‘sym’ command lets you construct symbolic variables and expressions. x = sym('x') a = sym('alpha') Creates a symbolic variable x that prints as x and a symbolic variable a that prints as alpha. rho = sym('(1 + sqrt(5))/2') f = rho^2 - rho – 1 Note that both ‘rho’ and ‘f’ are not evaluated. They are just printed as a string representation. (simplify(f) evaluates it!)

3 Symbolic object f = sym('a*x^2 + b*x + c')
Assigns the symbolic expression 𝑎 𝑥 2 +𝑏𝑥+𝑐 to the variable 𝑓. Does not create variables corresponding to the terms of the expression, 𝑎, 𝑏, 𝑐, and 𝑥. To perform symbolic math operations (e.g., integration, differentiation, substitution, etc.) on 𝑓, you need to create the variables explicitly. i.e. a = sym('a') b = sym('b') c = sym('c') x = sym('x') or simply syms a b c x Recommended to use ‘syms’ because it requires less typing.

4 MATLAB Command Example
sym(t,'f') f-floating point >>t=0.1; sym(t,'f') ans = / sym(t,'r') or sym(t) r-rational >> sym(t,'r') 1/10 sym(t,'e') e-estimated error >> sym(t,'e') eps/40 + 1/10 sym(t,'d') d-decimal >> sym(t,'d')

5 Finding the Limits of a function- f(x)
Mathematical Operation MATLAB Command lim 𝑥→0 𝑓(𝑥) limit(f) lim 𝑥→𝑎 𝑓(𝑥) limit(f,x,a) or limit(f,a) lim 𝑥→𝑎+ 𝑓(𝑥) limit(f,x,a,'right') lim 𝑥→𝑎− 𝑓(𝑥) limit(f,x,a,'left') Evaluate: (a) lim 𝑥→ 𝑥 (b) lim 𝑥→ 𝑥 (c) lim 𝑥→0− 1 𝑥

6 Finding the derivative of a function
Mathematical Operation MATLAB Command 𝑦= sin 𝑎𝑥; 𝑑𝑦 𝑑𝑥 =? 𝑑𝑦 𝑑𝑎 = ? syms a x f = sin(a*x) diff(f) diff(f,a) 𝑑 2 𝑦 𝑑 𝑥 2 =? diff(f,2) or diff(f,x,2) Differentiation of a symbolic matrix – ‘A’ Performs element-wise differentiation A = [cos(a*x),sin(a*x);-sin(a*x),cos(a*x)] diff(A)

7 Finding the integral of a function
Mathematical Operation MATLAB Command 𝑥 𝑛 𝑑𝑥= 𝑥 𝑛+1 𝑛+1 int(x^n) or int(x^n,x) 0 𝜋/2 sin 2𝑥 𝑑𝑥=1 int(sin(2*x),0,pi/2) or int(sin(2*x),x,0,pi/2) 𝑔= cos 𝑎𝑡+𝑏 𝑔 𝑡 𝑑𝑡= sin 𝑎𝑡+𝑏 𝑎 g = cos(a*t + b) int(g) or int(g,t) 𝐽 1 𝑧 𝑑𝑧=− 𝐽 0 (𝑧) int(besselj(1,z)) or int(besselj(1,z),z) All the variables that are used in indefinite integral should be defined as ‘syms’ (symbolic) variables

8 The integral of f(x) interpreted as the area A under the curve of f (x) from x = a to x = b.

9 Illustration of (a) rectangular and (b) trapezoidal numerical integration. Figure 9.1–1, page 370.

10 Numerical integration functions. Table 9.1–1, page 371.
Command Description Example quad(fun,a,b) Uses an adaptive Simpson’s rule to compute the integral of the function whose handle is fun, with a as the lower integration limit and b as the upper limit. The function fun must accept a vector argument. Compute the integral of : 0 𝜋 𝑠𝑖𝑛 𝑥 𝑑𝑥 quadl(fun,a,b) Uses Lobatto quadrature to compute the integral of the function fun. The rest of the syntax is identical to quad.

11 Numerical integration functions. Table 9.1–1, page 371 (Contd.)
Command Description Example trapz(x,y) Uses trapezoidal integration to compute the integral of y with respect to x, where the array y contains the function values at the points contained in the array x. Although the ‘quad’ and ‘quadl’ functions are more accurate than ‘trapz’, they are restricted to computing the integrals of functions and cannot be used when the integrand is specified by a set of points. For such cases, use the trapz function.

12 Polynomial Integration
q = polyint(p,C)returns a polynomial q representing the integral of polynomial p with a user-specified scalar constant of integration C. See page 375. Double Integrals A = dblquad(fun, a, b, c, d) computes the integral of f(x,y) from x = a to b, and y = c to d. Here is an example using an anonymous function to represent f(x,y) = xy 𝑥=𝑎 𝑏 𝑦=𝑐 𝑑 𝑥 𝑦 2 ⅆ𝑥ⅆ𝑦 >>fun >>A = dblquad(fun,1,3,0,1) The answer is A = For more, see pages 376 to 377.

13 Triple Integrals A = triplequad(fun, a, b, c, d, e, f) computes the triple integral of f(x, y, z) from x = a to b, y = c to d, and z = e to f. Here is an example using an anonymous function to represent f(x, y,z) = (xy - y2)/z. i.e. 𝑥=𝑎 𝑏 𝑦=𝑐 𝑑 𝑧=𝑒 𝑓 𝑥𝑦− 𝑦 2 𝑧 >>fun –y^2)/z; >>A = triplequad(fun,1,3,0,2,1,2) The answer is A = Note that the function must accept a vector x, but scalar y and z. See page 377.

14 Bar Charts and Histograms
Command Syntax Description Example bar(x,y) Creates a bar chart of y versus x. tests = 100; y=[91*ones(1,13),92*ones(1,15),93*ones(1,22),94*ones(1,19),95*ones(1,17),96*ones(1,14)]; x = [91:96]; [z,x] = hist(y,x); hist(y,x); figure; bar(x,z/tests); hist(y) Aggregates the data in the vector y into 10 bins evenly spaced between the minimum and maximum values in y. hist(y,n) Aggregates the data in the vector y into n bins evenly spaced between the minimum and maximum values in y. hist(y,x) Aggregates the data in the vector y into bins whose center locations are specified by the vector x. The bin widths are the distances between the centers.

15 Command Syntax Description [z,x] = hist(y) Same as hist(y) but returns two vectors z and x that contain the frequency count and the bin locations. [z,x] = hist(y,n) Same as hist(y,n) but returns two vectors z and x that contain the frequency count and the bin locations. [z,x] = hist(y,x) Same as hist(y,x) but returns two vectors z and x that contain the frequency count and the bin locations. The returned vector x is the same as the user-supplied vector x.

16 Thank you for the attention
Any Questions and Suggestions please…


Download ppt "Introduction to Programming for Mechanical Engineers (ME 319)"

Similar presentations


Ads by Google