ENGR 240 UNH Programming Evaluating Integrals Problems

ENGR 240 – Computational Methods for Engineering
ENGR 240
Computational Methods for Engineering
Lecture 15: Analyzing Data Statistically
Readings: : Excel p. 141-168, Matlab p. 153-156
Basic Concepts in Statistics
Mean
n
x1 + x2 + L + xn ) 1
(
x=
=
x
n

n i =1
i
=AVERAGE(A1:A20)
Median – Value such that half of data values lie above and
half lie below.
=MEDIAN(A1:A20)
Mode – Value that occurs with the greatest frequency within
a data set.
=MODE(A1:A20)
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
1
ENGR 240 – Computational Methods for Engineering
Basic Concepts in Statistics
Min & Max – Values that indicate the extremities of the
data
=MIN(A1:A20)
=MAX(A1:A20)
Variance – Provides an indication of the degree of spread in the data.
The greater the variance, the greater the spread
n
1
s =
(xi − x )2
n − 1 i =1
2

=VAR(A1:A20)
Standard Deviation – Provides measure of spread in the data set.
n
s = s2 =
1
(xi − x )2
n − 1 i =1

=STDEV(A1:A20)
Lecture 15: Analyzing Data Statistically
Basic Concepts in Statistics
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
2
ENGR 240 – Computational Methods for Engineering
Histograms

Plot that shows the data in a manner that illustrates
how the values are distributed within their range.

A histogram is a graph of frequency of events.

Histograms are very useful for displaying the
number of items that occur in each of several
possible ranges.

For example, you might want to display the number
of parts that are within specified ranges as they are
produced by some manufacturing process.

From this plot, we can obtain a plot of the
cumulative distribution, which allows us to estimate
the likelihood that a data value is associated with an
item drawn at a random is less that or greater than
some specified value.
Lecture 15: Analyzing Data Statistically
Histograms – Procedure
Enter the data to be into one column (the data might be a large
number of measurements, for example).
2. Create a column of “bin values” which correspond to the ranges
into which you want to divide your data. In particular, Excel
wants a column of bin values, where the bin values are the
Bin Values
upper limits for each range.
1.
18
19
Example: Suppose you wanted to display the number
of runners that crossed the finish line in a 5k race in
ranges of 1 minute.
• Number of runners under 18 minutes
• Number of runners in 18-19 minute range
• ….
• Number of runners in 27-28 minute range
20
21
22
23
24
25
26
27
28
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
3
ENGR 240 – Computational Methods for Engineering
Histograms – Procedure
3. Select Data – Data Analysis – Histogram
4. Select
Input Range (your data)
• Bin Range
• Output Range (specify the upper left cell where Excel
should writes its report)
• Check Chart Output to generate a graph (histogram)

Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
4
ENGR 240 – Computational Methods for Engineering
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
5
ENGR 240 – Computational Methods for Engineering
Cumulative Distribution
Cumulative distribution allows us to determine the likelihood
that a particular value drawn at random is less than or greater
that some specified value.
 For example, determine the likelihood that a cylinder diameter
within a randomly selected engine block is greater than some
specified value
 To construct a cumulative distribution (F) , it is necessary to
construct a histogram and determine the relative frequency (f)
associated with each of the intervals.
F4 = f1 + f 2 + f 3 + f 4

F1 = f1
L
F2 = f1 + f 2
k
F3 = f1 + f 2 + f 3
Fk = f1 + f 2 + L + f k = ∑ f i
i =1
Lecture 15: Analyzing Data Statistically
Cumulative Distribution
Frequency Cumulative %
2
8.00%
1
12.00%
3
24.00%
0
24.00%
2
32.00%
3
44.00%
5
64.00%
3
76.00%
2
84.00%
2
92.00%
2
100.00%
0
100.00%
Histogram
Frequency
Bin
18
19
20
21
22
23
24
25
26
27
28
More
6
120.00%
5
100.00%
4
80.00%
3
60.00%
2
40.00%
1
20.00%
0
0.00%
Frequency
Cumulative %
Bin
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
6
ENGR 240 – Computational Methods for Engineering
Histograms on Matlab
A histogram is a plot of the distribution of data. Entire range of data broken into
consecutive subranges or bins.
In histogram plot

Each bin represented by vertical bar

Left and right of vertical bar show range of data in bin
Height of vertical bar is number of data points in bin
MATLAB command hist makes histogram. Simples
form is hist(y)


y is vector of data points

hist divides range
into ten equal bins,
then plots result
Lecture 15: Analyzing Data Statistically
Histograms on Matlab
Additional forms of hist
hist(y,nbins)

MATLAB divides range into nbins (scalar) bins of equal size
hist(y,x)

x (vector) specifies midpoint of each bin

Spacing between consecutive elements of x can be different

Bin edges are midpoints of consecutive bins
Can get histogram heights if desired
n=hist(y) n=hist(y,nbins) n=hist(y,x)

Output n is a vector

Size of n is number of bins

Value of element of n is number of data points in corresponding bin
For two forms, can also get bin centers
[n xout]=hist(y)
[n xout]=hist(y,nbins)
n same as before
xout(i) is center of ith bin
Lecture 15: Analyzing Data Statistically
Lecture 15: Analyzing Data Statistically
7
ENGR 240 – Computational Methods for Engineering
ENGR 240
Computational Methods for Engineering
Lecture 14: Evaluating Integrals
Readings: : Excel 373-401, Matlab p. 300-302, 363-366
What is an Integral?
An integral evaluates the area under the curve of the function
from point a to point b
f(x)
b
Area =
∫ f ( x)dx
a
a
x
b
Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
1
ENGR 240 – Computational Methods for Engineering
Trapezoidal Rule
n
I=
f(x)
∑A
i
i =1
Ai = yi ∆xi
n
I=
∑ y ∆x
i
i
i =1
a
x
Height using first value
b
Height using last value
Height using average value
Lecture 14: Evaluating Integrals
Trapezoidal Rule – Unequally Spaced Data
y2
y1
y n −1
y3
x1
x2
∆xi = xi +1 − xi
x3
x4
yi =
xn-1
 y + yi 
Ai =  i +1
( xi +1 − xi )
2


yi +1 + yi
2
n −1
n −1
 yi +1 + yi 
I=

(xi +1 − xi )
2

i =1 

xn
I=

1
( yi +1 + yi )(xi +1 − xi )
2 i =1
Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
2
ENGR 240 – Computational Methods for Engineering
Trapezoidal Rule – Unequally Spaced Data
yi
6
15.5
24.5
34
41.5
47
49.5
49.5
48
46
43.5
39
34.5
31.5
28.5
24
18.5
12.5
6.5
Δxi
0.005
0.005
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.01
0.02
0.02
0.02
0.02
0.05
0.05
0.1
0.12
SUM
yi =
yi +1 + yi
2
Ai
0.03
0.0775
0.245
0.34
0.415
0.47
0.495
0.495
0.48
0.46
0.435
0.78
0.69
0.63
0.57
1.2
0.925
1.25
0.78
Voltage (V)
60
50
Voltage (V)
Time (s) Voltage (V) Rectangle
0
0
1
0.005
12
2
0.01
19
3
0.02
30
4
0.03
38
5
0.04
45
6
0.05
49
7
0.06
50
8
0.07
49
9
0.08
47
10
0.09
45
11
0.1
42
12
0.12
36
13
0.14
33
14
0.16
30
15
0.18
27
16
0.23
21
17
0.28
16
18
0.38
9
19
0.5
4
20
40
30
20
10
0
0
0.1
0.2
0.3
Time (ms)
0.4
0.5
0.6
10.7675
∆xi = xi +1 − xi
 y + yi 
Ai =  i +1
( xi +1 − xi )
2


Lecture 14: Evaluating Integrals
Trapezoidal Rule – Equally Spaced Data
y2
y1
y n −1
y3
x1
x2
∆xi = xi +1 − xi = ∆x
n

i =1
yi =
x3
x4
yi =
yi +1 + yi
2
xn-1
y1 =
y1 + y 2
2
y1 + 2 y 2 + 2 y3 + L + 2 y n −1 + y n
2
y2 =
y 2 + y3
2
xn
y3 =
y3 + y 4
2
 y + yn n−1 
I = 1
+ ∑ yi ∆x
i=2
 2

Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
3
ENGR 240 – Computational Methods for Engineering
Trapezoidal Rule –Equally Spaced Data
Time (s)
Temp. (K)
0
5
300
360
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
420
480
540
600
660
720
780
840
900
960
1020
1080
1140
1200
1260
1320
1380
1440
1500
Average 1st & Last
900
Sum 2nd – n-1
17100
Total Sum
18000
Δx
5
Integral
90000
Lecture 14: Evaluating Integrals
Simpson’s Rule
Is =
1
( y1 + 4 y 2 + 2 y3 + 4 y4 + 2 y5 + L + 2 yn −2 + 4 yn −1 + yn )∆x
3
Time (s) Temp. (K) Multiplier Multiplication
0
300
1
300
5
360
4
1440
10
420
2
840
15
480
4
1920
20
540
2
1080
25
600
4
2400
30
660
2
1320
35
720
4
2880
40
780
2
1560
45
840
4
3360
50
900
2
1800
55
960
4
3840
60
1020
2
2040
65
1080
4
4320
70
1140
2
2280
75
1200
4
4800
80
1260
2
2520
85
1320
4
5280
90
1380
2
2760
95
1440
4
5760
100
1500
1
1500
Sum
52500
Δx
3
Integral
157500
Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
4
ENGR 240 – Computational Methods for Engineering
Numerical Integration Matlab – With Function
q = quad(function,a,b)
The value of the
integral
The function to
be integrated
The integration
limits

Use when can write integrand as a MATLAB function

Uses numerical technique called “adaptive Simpson method”

Function should be previously defined as an anonymous function with vector
notation.

The user has to make sure that the function does not have a vertical asymptote
between a and b.

f=@ (x) x.*exp(-x.^0.8)+0.2
I=quad(f,0,8)
I = 3.160432265706000
න ‫ି ݁ݔ‬௫
బ.ఴ

+ 0.2 ݀‫ݔ‬
Lecture 14: Evaluating Integrals
Numerical Integration Matlab – With Function
q = quadl(function,a,b)
The value of the
integral
The function to
be integrated
The integration
limits

Use when can write integrand as a MATLAB function

Uses numerical technique called “adaptive Lobatto method”

Function should be previously defined as an anonymous function with vector
notation.

The user has to make sure that the function does not have a vertical asymptote
between a and b.
f=@ (x) x.*exp(-x.^0.8)+0.2
I=quadl(f,0,8)
I = 3.160432765650086

න ‫ି ݁ݔ‬௫

బ.ఴ
+ 0.2 ݀‫ݔ‬
Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
5
ENGR 240 – Computational Methods for Engineering
Numerical Integration Matlab – With Data
q = trapz(x,y)
 The two vectors x and y are vectors with the x and y coordinates
of the points must be of the same length.
Time (s) Temp. (K)
0
300
5
360
10
420
15
480
20
540
25
600
30
660
35
720
40
780
45
840
50
900
55
960
60
1020
65
1080
70
1140
75
1200
80
1260
85
1320
90
1380
95
1440
100
1500
Definition of t
t=[0:5:100];
T=[300 360 420 480 540 600 660 720 780 840 900 960
Definition of T
1020 1080 1140 1200 1260 1320 1380 1440 1500];
Integral of both
vectors t and T
using trapezoidal
rule
I=trapz(t,T)
I = 90000
Lecture 14: Evaluating Integrals
Symbolic Integration Matlab
Indefinite integrals:
Definite integration:
int(S)
int(S,a,b)
or
or
int(S,var)
int(S,var,a,b)
Either S can be the name of a previously created symbolic expression,
or an expression can be typed in for S.
 In the int(S) command, if the expression contains one symbolic
variable, the integration is carried out with respect to that variable. If
the expression contains more than one variable, the integration is
carried out with respect to the default symbolic variable
 In the int(S,var) command, which is used for integration of expressions
with several symbolic variables, the integration is carried out with
respect to the variable var.
 a and b are the limits of integration. The limits can be numbers or
symbolic variables.

Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
6
ENGR 240 – Computational Methods for Engineering
Symbolic Integration Matlab
Indefinite integrals
syms x y t
S=2*cos(x)-6*x;
int(S)
ans = 2*sin(x)-3*x^2
int(x*sin(x))
ans = sin(x)-x*cos(x)
R=5*y^2*cos(4*t);
int(R)
ans = (5*y^3*cos(4*t))/3
int(R,t)
ans = (5*y^2*sin(4*t))/4
Define x, y, and t as symbolic variables.
Assign to S the expression 2cos(x) – 6x.
Use the int(S)command to integrate S.
The answer 2sin(x) – 3×2 is displayed.
Use the int(S)command to integrate x sin(x) .
The answer sin(x) – x cos(x) is displayed.
Assign to R the expression 5y2 cos(4t) .
Use the int(R)command to integrate R.
MATLAB integrates R with respect to y (default symbolic variable)
Use the int(R,t)command to integrate R with respect to t.
The answer 5y2 sin(4t) I 4 is displayed.
Definite integrals
syms y
int(sin(y)-5*y^2,0,pi)
ans = 2 – (5*pi^3)/3
Use the int(S)command to integrate sin(y) – 5y2 from 0 to 
Lecture 14: Evaluating Integrals
Symbolic Differentiation
diff(S)
or
diff(S,var)
 S is name of existing symbolic expression, or can type in an expression
 For diff(S)
 If only one variable in S, command differentiates with respect to
that variable
 If multiple variables in S, command differentiates with respect to
first variable in default order
 diff(S,var) differentiates with respect to var
To get the nth derivative, use
diff(S,n) or diff(S,var,n)
Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
7
ENGR 240 – Computational Methods for Engineering
Differentiation
syms x y t
Define x, y, and t as symbolic variables.
S=exp(x^4);
Assign to S the expression ex .
diff(S)
Use the diff(S)command to differentiate S.
4
ans = 4*x^3*exp(x^4)
The answer 4x3ex4 is displayed.
diff((1-4*x)^3)
Use the diff(S)command to differentiate (1 – 4x)3 .
ans = -12*(1-4*x)^2
The answer –12(1 – 4x)2 is displayed.
R=5*y^2*cos(3*t);
Assign to R the expression 5y2 cos(3t) .
diff(R)
Use the diff(R)command to differentiate R.
ans = 10*y*cos(3*t)
MATLAB differentiates R with respect to y (default symbolic variable).
diff(R,t)
Use the diff(R,t)command to differentiate R with respect to y . t.
ans = -15*y^2*sin(3*t)
The answer –15y2 sin(3t) is displayed.
diff(S,2)
Use diff(S,2)command to obtain the second derivative of S.
ans = 12*x^2*exp(x^4)+16*x^6*exp(x^4)
The answer is displayed.
Lecture 14: Evaluating Integrals
Lecture 14: Evaluating Integrals
8

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper
Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER