29
Department of Mechanical Engineering, LSU Session IV MATLAB Tutorials Session IV Mathematical Applications using MATLAB Rajeev Madazhy Email: [email protected] Dept of Mechanical Engineering LSU Department of Mechanical Engineering, LSU Session IV

MATLAB Tutorials

  • Upload
    mura

  • View
    79

  • Download
    0

Embed Size (px)

DESCRIPTION

MATLAB Tutorials. Session IV Mathematical Applications using MATLAB. Rajeev Madazhy Email: [email protected] Dept of Mechanical Engineering LSU. Department of Mechanical Engineering, LSUSession IV. Last Session…. Plotting in MATLAB 2-D Graphics 3-D Graphics - PowerPoint PPT Presentation

Citation preview

Page 1: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

MATLAB Tutorials

Session IVMathematical Applications using MATLAB

Rajeev MadazhyEmail: [email protected]

Dept of Mechanical Engineering

LSUDepartment of Mechanical Engineering, LSU Session IV

Page 2: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Last Session….

Plotting in MATLAB 2-D Graphics 3-D Graphics Mesh, surface and contour plots in 3-D plots Exercises in Graphics

Page 3: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Session IV Outline….

Using fplot function Minimization Zero finding Curve fitting Interpolation Integration

Page 4: MATLAB Tutorials

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.

Page 5: MATLAB Tutorials

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….

Page 6: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Output….

Page 7: MATLAB Tutorials

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

Page 8: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Example….

Page 9: MATLAB Tutorials

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

Page 10: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Example….

Page 11: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Curve Fitting by Polynomial

Perform curve fit of N point of data (xi, yi) (i=1,…N) by a n-th order polynomial For example, n=4, f(x)=a0+a1*x+a2*x^2+a3*x^3+a4*x^4 Find a0, …, a4 which minimize the sum of the squared error at the data points Solve linear equations to find a0, …, a

21 ))f(x(yΣS ii

ni

Page 12: MATLAB Tutorials

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

Page 13: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Poly_fit M-File….

Page 14: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Output….

Page 15: MATLAB Tutorials

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

Page 16: MATLAB Tutorials

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.

Page 17: MATLAB Tutorials

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.

Page 18: MATLAB Tutorials

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 interp1Hours Temp(C°)

1 52 83 94 155 256 297 318 309 22

10 2511 2712 24

Page 19: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

M-File….

Page 20: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Output in command window….

Page 21: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Example 2….

Page 22: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Output….

Page 23: MATLAB Tutorials

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

b

a

dxxf )(

n

iii

b

a

xfadxxf1

)()(

Page 24: MATLAB Tutorials

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.

Page 25: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Matlab M-File….

Page 26: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Output….

Page 27: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Recap….

Using fplot function Minimization Zero finding Curve fitting Interpolation Integration

Page 28: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Next Session….

Ordinary Differential Equations….Examples using MATLAB to solve ODE’s….Mention of DDE’s….

Page 29: MATLAB Tutorials

Department of Mechanical Engineering, LSU Session IV

Thank You