Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy

Similar presentations


Presentation on theme: "Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy"— Presentation transcript:

1 Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy Email:rmadaz1@lsu.edurmadaz1@lsu.edu Dept of Mechanical Engineering LSU Department of Mechanical Engineering, LSUSession IV

2 Last Session…. Plotting in MATLAB  2-D Graphics  3-D Graphics  Mesh, surface and contour plots in 3-D plots  Exercises in Graphics

3 Department of Mechanical Engineering, LSU Session IV Session IV Outline….  Using fplot function  Minimization  Zero finding  Curve fitting  Interpolation  Integration

4 Department of Mechanical Engineering, LSU Session IV Using fplot() function…. fplot(fun,[xmin, xmax]) plots the function specified by the string fun between the x-axis limits specified by [xmin xmax]. For example fplot(‘sin(x)’,[0 2*pi]) will plot sine of x with x ranging from 0 to 2 . Try it.

5 Department of Mechanical Engineering, LSU Session IV You can also use fplot for the function defined by yourself. >>fplot(‘function1’,[0 8]) where function1 is a M-file function: fun=‘function1’; fplot(fun,[0 8]) function y=function1(x) y=2*exp(-x).*sin(x); Using fplot() function….

6 Department of Mechanical Engineering, LSU Session IV Output….

7 Department of Mechanical Engineering, LSU Session IV Minimization….  In many situations, we want to find the function extremes, the local maximum (peaks) and minimum (valleys).  fmin(fun,x1,x2) can be used to find a local minimum of function f(x) defined by string fun in the interval x1 < x < x2.  In order to find the local maximum, function f(x) should be replaced by -f(x).

8 Department of Mechanical Engineering, LSU Session IV Example….

9 Department of Mechanical Engineering, LSU Session IV Zero Finding….  We have learned to use roots to find the zeros of a polynomial.  fzero(fun,x0) can be used to find zero of a general function f(x) near x0.  If you want to solve the equation f(x)=c, just define a function g(x)=f(x)-c. Then find the zero of g(x).

10 Department of Mechanical Engineering, LSU Session IV Example….

11 Department of Mechanical Engineering, LSU Session IV Curve Fitting by Polynomial Perform curve fit of N point of data (x i, y i ) (i=1,…N) by a n-th order polynomial For example, n=4, f(x)=a 0 +a 1 *x+a 2 *x^2+a 3 *x^3+a 4 *x^4 Find a 0, …, a 4 which minimize the sum of the squared error at the data points Solve linear equations to find a 0, …, a

12 Department of Mechanical Engineering, LSU Session IV Using polyfit function….  In Matlab, the function polyfit(x, y, n) finds the coefficients of a polynomial p(x) of degree n that fits the data provided by array x and y in a least-squares sense.  The program on next slide illustrates the use of this function

13 Department of Mechanical Engineering, LSU Session IV Poly_fit M-File….

14 Department of Mechanical Engineering, LSU Session IV Output….

15 Department of Mechanical Engineering, LSU Session IV More on Curve Fitting….  If x and y can be modeled by a power function or exponential function in the form y=a*x^b  y=a*exp(b*x) Taking natural logarithms of both side will yield ln(y)=ln(a)+b*ln(x)  ln(y)=ln(a)+b*x Then we can use the first order polynomial to fit the data and therefore find a and b

16 Department of Mechanical Engineering, LSU Session IV Interpolation….  Interpolation is defined as a way of estimating values of a function between those given by some set of data points.  The simplest way of interpolation is called linear interpolation in which the intermediate values are calculated by the straight line between the entered points.  Matlab provides function interp1 for one dimensional interpolation.

17 Department of Mechanical Engineering, LSU Session IV Using interp1 function…. yi = interp1(x,y,xi,'method'), where array x and y provide the data points that are known, yi is the result of interpolation at xi. Available methods are:  'nearest' - nearest neighbor interpolation  'linear' - linear interpolation  'spline' - cubic spline interpolation  'cubic' - cubic interpolation The default is linear interpolation.

18 Department of Mechanical Engineering, LSU Session IV Example…. Known temperature every hour for twelve hours. Find the temperature at 9.3 and 11.7 using interp1 HoursTemp(C°) 15 28 39 415 525 629 731 830 922 1025 1127 1224

19 Department of Mechanical Engineering, LSU Session IV M-File….

20 Department of Mechanical Engineering, LSU Session IV Output in command window….

21 Department of Mechanical Engineering, LSU Session IV Example 2….

22 Department of Mechanical Engineering, LSU Session IV Output….

23 Department of Mechanical Engineering, LSU Session IV Integration…. Many engineering problems require the calculation of the definite integral of a function bounded in the finite interval [a, b]. Numerically, we can approximate the definite integral by the weighted sum of a number of function values

24 Department of Mechanical Engineering, LSU Session IV Using trapz, quad, quad8 functions….  Matlab provides three function, trapz, quad, quad8, for calculation of integrals.  The function trapz(x, y) calculates the integral by summing the area of trapezoids formed from the data points defined by array x and y  The function quad(fun, a, b) or quadl(fun, a, b) calculate the integral based on the concept of quadrature.

25 Department of Mechanical Engineering, LSU Session IV Matlab M-File….

26 Department of Mechanical Engineering, LSU Session IV Output….

27 Department of Mechanical Engineering, LSU Session IV Recap….  Using fplot function  Minimization  Zero finding  Curve fitting  Interpolation  Integration

28 Department of Mechanical Engineering, LSU Session IV Next Session…. Ordinary Differential Equations…. Examples using MATLAB to solve ODE’s…. Mention of DDE’s….

29 Department of Mechanical Engineering, LSU Session IV Thank You


Download ppt "Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy"

Similar presentations


Ads by Google