0% found this document useful (0 votes)
19 views20 pages

Engr 1204 Lecture 9 Sp24

Uploaded by

Fahad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

Engr 1204 Lecture 9 Sp24

Uploaded by

Fahad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ENGR 1204 Programming

Languages in Engineering
Advanced Mathematics

Calculus-Integration, Differentiation
Polynomials-Roots Finding, Curve Fittings

Attaway MATLAB 6E – Chapters 14 & 12


Calculus: Integration/Differentiation

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.

y is the function of (x), a=0 , b= 1

To calculate the area of one rectangle,


We get area:
f(1) = 12 = 1

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

four rectangles, we get area:


two rectangles, we get area:
with bases [0, 1/ 2 ] and [ 1/ 2 , 1]
Area = (1 /4)* (1/4)2 +(1/4)*(2/4)2
heights f( 1/ 2 ) = 1/ 4 and f(1) = 1,
+(1/4)*(3/4)2+(1/4)*12

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

The syntax for trapz function


is trapz(x,y)
Where x is a vector and y is
the function of x at each point.
See examples on next slide

5
Numerical Integration examples

>> f = @(x) 3*x.^2-1; % first create the anonymous function


>> x = [2 4]; % create x vector, only one panel
>> y = f(x); % y is the function of x
>> area = trapz(x,y) % trapz function
area =
58 % approximation is higher

>> x = 2:4; % x with two panels


>> y = f(x);
>> area=trapz(x,y)
area =
>> integral(f,2,4)
55
ans =
54.0000 % approximation
correct
>> x=2:.01:4; % more panel
>> y=f(x);
>> área=trapz(x,y) integral: The first input argument is
area =
a function handle.
54.0001

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

y = sqrt(abs(4 - x.^2)); %y function of x


Quarter circle: x + y = r
2

1.8

area = trapz(x,y); % calculate area 1.6

disp('Approximate area:')
1.4

1.2

disp(area) 1

y
0.8

plot(x, y), grid % plot area 0.6

xlabel('x'), ylabel('y’), title('Quarter circle: x^2 + y^2 = r^2') 0.4

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

It is the derivative of a function y=f(x), written as ,and is defined as the


rate of change of the dependent variable y with respect to x. The
derivative is the slope of the line tangent to the function at a given point.

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

Example next page

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)

>>p= ([4 ,-2 , -8 , 3])


>> roots (p)
ans =
-1.3660
1.5000 Note: there are three roots
0.3660

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.

The least squares fit minimizes the sum of the squares


of the differences between the actual data and the data
predicted by the line.

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

x=linspace(min(time),max(time)); % creating a vector x with more time values

y1=polyval(coefs1, x); % y values for each x points


y2=polyval(coefs2, x);
y3=polyval(coefs3,x);

plot(time,temp,'ro', x,y1,'g-', x,y2,'b-', x,y3,'k-'), grid on


xlabel ('Time')
ylabel('Temperature')
title ('Temperature one afternoon’)
legend('Data Set','Degree 1','Degree 2','Degree 3')
axis([1 7 60 75]) % change axis

17
x = 2:6; % data set 1-time vector
y = [65 67 72 71 63]; % data set 2-temperature vector

morex = linspace(min(x),max(x)); % take more x values to have a smooth curve


for pd = 1:3 % loop variable pd is for three polynomials
coefs = polyfit(x,y, pd); % get the coefficients for each degree
curve = polyval(coefs, morex); % get y values for each x values

subplot(1,3,pd); % use subplot to plot three curves


plot(x,y,'ko’, morex,curve,'k',LineWidth=2);
xlabel('Time’); ylabel('Temperatures’);
title(sprintf('Degree %d’, pd));
axis([1 7 60 75]); % change the axis
end

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

You might also like