Engr 1204 Lecture 9 Sp24
Engr 1204 Lecture 9 Sp24
Languages in Engineering
Advanced Mathematics
Calculus-Integration, Differentiation
Polynomials-Roots Finding, Curve Fittings
2
Numerical Integration
The integral of a function f(x) for a ≤ x ≤b can be interpreted as the area between
f(x) curve and the x axis, bounded by the limits x=a and x=b. If we denote this area
by A, then we can write A as definite integral –it has specified limits (a, b) of
integration
Suppose we are given a function f(x) defined on a region (a, b). Let S be the blue
region under the graph of f, we have to find the area of this region.
Area = 1.
3
The simplest way to find the area under a curve is to split the area into
rectangles with smaller width covering the S more closely. Then adding
the area of the rectangles. This approximation will be close to the
actual area of S.
Y=(1) 2
Y=(1/2) 2
=15/32
Area = (1/ 2) *(1/4)+ (1/ 2) *1
= 0.46875.
= 0.625.
4
A most sophisticated method is to use trapezoidal elements. Each trapezoid is
called a panel. MATLAB implements trapezoidal integration with the trapz
function.
5
Numerical Integration examples
6
A program finding area of a quarter circle curve
% Quarter Circle with Radius = 2, Exact area = pi = 3.1415
% the equation of a circle is x^2+y^2=r^2 , here r= 2
% x^2 +y^2 = 4; y=sqrt(4-x^2)
a = 0; b = 2; % two limits
N = input('Enter number of intervals: ‘); % prompt for intervals
w = (b - a) / N; % width of each panel
x = a: w: b; % range of x
2 2 2
1.8
disp('Approximate area:')
1.4
1.2
disp(area) 1
y
0.8
0.2
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x
7
Numerical Differentiation
Matlab has a built-in function diff, which returns the differences between
consecutives elements in a vector.
For a function y=f(x) where x is a vector-the differentiation can be
approximated by dividing diff(y) by diff(x).
8
For example, the equation x 3 +2x2 -4x +3 can be written as an
Anonymous function and then calculate the differentiation.
9
Polynomial Functions
Root finding and Curve fitting
10
Polynomial Curves
Simple curves are polynomials of different degrees, or orders.
The degree is the integer of the highest exponent in the
expression. For example,
A straight line is a first order (or degree 1) polynomial of the form
ax + b or more explicitly ax1 + b
A quadratic is a second order (or degree 2) polynomial of the form
ax2 + bx + c
A cubic (degree 3) is of the form ax3 + bx2 + cx + d
In MATLAB, a polynomial is represented by a row vector of
coefficients – e.g. a second order polynomial would be
represented by a row vector with a length of 3 (the coefficients a,
b, and c)
11
Roots finding
Numerical methods are used when there is no formula for root, or the
formula is too complex. The roots function in MATLAB can be used to find
the roots of an equation represented by a polynomial. For example, for the
mathematical function:
f(x)=4x3-2x2-8x+3 to solve the equation f(x)=0
roots(p) finds the roots of an equation (pass a polynomial row vector p,
which are the coefficients)
12
Curve Fitting
In many cases, data in the form of (x, y) points is sampled
in time (e.g., every second or every hour) or in space (e.g.,
every inch or every kilometer) which results in discrete
data points
Fitting a curve to the data is often desired. Curve Fitting
is finding the best polynomials to fit the data from the
sampling.
the function polyfit(x,y,d) fits a polynomial of the degree
d specified to the data points represented by x and y and
returns a polynomial row vector of coefficients, e.g., for a
straight line, polyfit(x, y, 1)
13
Fitted a curve for first order polynomial
Data Values
10
(x4,y4)
9
(x2,y2)
8 Y = mX + b
(x5,y5)
7
(x3,y3)
6
Y
4
(x1, y1)
3
0
0 1 2 3 4 5 6 7 8 9 10
X
14
Least Squares Regression
The polyfit function uses the least squares regression
method.
Our job is to find the equation that best fits a data set
15
Temperature one afternoon
75
Data Set
X4 Degree 1
Y 72 Degree 2
Degree 3
70 X5
Y 71
Temperature
X3
Y 67
65
X2
Y 65
X6
Y 62.8857
60
1 2 3 4 5 6 7
Time
16
time=2:6; % Time vector
temp=[65 67 72 71 63]; % temperature readings
coefs1=polyfit(time,temp,1); % finding coefficients for straight line
coefs2=polyfit (time,temp,2); % finding coefficients for quadratic
coefs3=polyfit (time,temp,3); % finding coefficients for cubic
17
x = 2:6; % data set 1-time vector
y = [65 67 72 71 63]; % data set 2-temperature vector
18
Degree 1 Degree 2 Degree 3
75 75 75
70 70 70
Temperatures
Temperatures
Temperatures
65 65 65
60 60 60
2 4 6 2 4 6 2 4 6
Time Time Time
19
Interpolation and Extrapolation
Using the curve that has been fit through the data
points, we can estimate values other than at the
sampled data points
Interpolation is estimating the values in between
recorded data points
Extrapolation is estimating outside of the bounds of
the recorded data
both can be accomplished using polyval
Underfitting is when the fit does not describe the
data very well, and with overfitting it fits too well
(and therefore will not be generalizable)
20