Upload
albert-stone
View
219
Download
0
Embed Size (px)
Citation preview
Department of Mechanical Engineering, LSU Session IV
MATLAB Tutorials
Session IVMathematical Applications using MATLAB
Rajeev MadazhyEmail: [email protected]
Dept of Mechanical Engineering
LSU
Department of Mechanical Engineering, LSU Session IV
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
Department of Mechanical Engineering, LSU Session IV
Session IV Outline….
Using fplot function
Minimization
Zero finding
Curve fitting
Interpolation
Integration
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.
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….
Department of Mechanical Engineering, LSU Session IV
Output….
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).
Department of Mechanical Engineering, LSU Session IV
Example….
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).
Department of Mechanical Engineering, LSU Session IV
Example….
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
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
Department of Mechanical Engineering, LSU Session IV
Poly_fit M-File….
Department of Mechanical Engineering, LSU Session IV
Output….
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
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.
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.
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 2210 2511 2712 24
Department of Mechanical Engineering, LSU Session IV
M-File….
Department of Mechanical Engineering, LSU Session IV
Output in command window….
Department of Mechanical Engineering, LSU Session IV
Example 2….
Department of Mechanical Engineering, LSU Session IV
Output….
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
)()(
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.
Department of Mechanical Engineering, LSU Session IV
Matlab M-File….
Department of Mechanical Engineering, LSU Session IV
Output….
Department of Mechanical Engineering, LSU Session IV
Recap….
Using fplot function
Minimization
Zero finding
Curve fitting
Interpolation
Integration
Department of Mechanical Engineering, LSU Session IV
Next Session….
Ordinary Differential Equations….
Examples using MATLAB to solve ODE’s….
Mention of DDE’s….
Department of Mechanical Engineering, LSU Session IV
Thank You