Vous êtes sur la page 1sur 86

Probability, Statistics

and Interpolation
Cheng-Liang Chen
PSE
LABORATORY
Department of Chemical Engineering
National TAIWAN University
Chen CL 1
Statistics, Histograms, and Probability
A Histogram is a plot of the frequency of occurrence of data values
versus the values themselves
Test 1 61 61 65 67 69 72 74 74 76 77
83 83 85 88 89 92 93 93 95 98
Test 2 66 69 72 74 75 76 77 78 78 79
79 80 81 83 84 85 87 88 90 94
Test 1 ve scores in 60 69 range, ve in 70 79,
ve in 80 89, ve in 90 99;
Test 2 two scores in 60 69 range, nine in 70 79,
seven in 80 89, two in 90 99
Chen CL 2
% Collect the scores in each bin.
test1 = [5, 5, 5, 5];
test2 = [2, 7, 9, 2];
% Specify the bin centers.
x = [64.5, 74.5, 84.5, 94.5];
subplot(2,1,1)
bar(x, test1), axis([60 100 0 10]),...
title(Histogram of scores for test 1),...
xlabel(Score range),...
ylabel(Number of scores)
%
subplot(2,1,2)
bar(x, test2), axis([60 100 0 10]),...
title(Histogram of scores for test 2),...
xlabel(Score range),...
ylabel(Number of scores)
Chen CL 3
60 65 70 75 80 85 90 95 100
0
2
4
6
8
10
Histogram of scores for test 1
Score range
N
u
m
b
e
r

o
f

s
c
o
r
e
s
60 65 70 75 80 85 90 95 100
0
2
4
6
8
10
Histogram of scores for test 2
Score range
N
u
m
b
e
r

o
f

s
c
o
r
e
s
Chen CL 4
Ex: Breaking Strength of Thread
To ensure proper quality control, a thread manufacturer selects samples and tests
them for breaking strength. Suppose that 20 thread samples are pulled until they
break, and the breaking force is measured in newtons rounded o to integer
values. The breaking force values recorded were 92, 94, 93, 96, 93, 94, 95, 96, 91,
93, 95, 95, 95, 92, 93, 94, 91, 94, 92, and 93. Plot the histogram of the data.
Solution:
Six outcomes (91, 92, 93, 94, 95, 96) six bins
% Thread breaking strength data for 20 tests.
y = [92, 94, 93, 96, 93, 94, 95, 96, 91, 93,...
95, 95, 95, 92, 93, 94, 91, 94, 92, 93];
% The six possible outcomes are: 91, 92, 93, 94, 95, 96
x = [91:96];
hist(y, x), axis([90 97 0 6]),...
xlabel(Thread strength (newtons)),...
ylabel(Absolute frequency),...
title(Absolute frequency histogram for 20 tests)
Chen CL 5
91 92 93 94 95 96
0
1
2
3
4
5
6
Thread strength (newtons)
A
b
s
o
l
u
t
e

f
r
e
q
u
e
n
c
y
Absolute frequency histogram for 20 tests
Chen CL 6
Note:
100 samples, 6 outcomes (91, 92, 93, 94, 95, 96)
with number of times: 13, 15, 22, 19, 17, 14
% Thread breaking strength data for 100 tests.
y = [91*ones(1,13), 92*ones(1,15), 93*ones(1,22),...
94*ones(1,19), 95*ones(1,17), 96*ones(1,14)];
% The six possible outcomes are: 91, 92, 93, 94, 95, 96
x = [91:96];
hist(y, x),...
xlabel(Thread strength (newtons)),...
ylabel(Absolute frequency),...
title(Absolute frequency histogram for 100 tests)
Chen CL 7
91 92 93 94 95 96
0
5
10
15
20
25
Thread strength (newtons)
A
b
s
o
l
u
t
e

f
r
e
q
u
e
n
c
y
Absolute frequency histogram for 100 tests
Chen CL 8
Relative Frequency:
100 samples, 6 outcomes (91, 92, 93, 94, 95, 96)
with number of times: 13, 15, 22, 19, 17, 14
tests = 100;
y = [13, 15, 22, 19, 17, 14]/tests;
x = [91:96]; bar(x, y),...
xlabel(Thread strength (newtons)),...
ylabel(Relative frequency),...
title(Absolute frequency histogram for 100 tests)
OR
tests = 100;
y = [91*ones(1,13), 92*ones(1,15), 93*ones(1,22),...
94*ones(1,19), 95*ones(1,17), 96*ones(1,14)];
x = [91:96]; [z, x] = hist(y, x);
bar(x, z/tests),...
xlabel(Thread strength (newtons)),...
ylabel(Relative frequency),...
title(Absolute frequency histogram for 100 tests)
Chen CL 9
91 92 93 94 95 96
0
0.05
0.1
0.15
0.2
0.25
Thread strength (newtons)
R
e
l
a
t
i
v
e

f
r
e
q
u
e
n
c
y
Absolute frequency histogram for 100 tests
Chen CL 10
Test Your Understanding
T7.1-1
In 50 tests of thread, the number of times 91, 92, 93, 94, 95, or 96
newtons was measured was 7, 8, 10, 6, 12, and 7, respectively.
Obtain the absolute and relative frequency histograms.
Chen CL 11
Histogram Functions
Command Description
bar(x,y) Creates a bar chart of y versus x
hist(y) Aggregates the data in the vector coloryellowy into 10 bins evenly
spaced between the minimum and maximum values in y
hist(y,n) Aggregates the data in the vector y into n bins evenly spaced
between the minimum and maximum values in y
hist(y,x) Aggregates the data in the vector y into bins whose center locations
are specied by vector x. The bin widths are the distances between
the centers
[z,x]=hist(y) Same as hist(y) but returns two vectors z and x that contain the
frequency count and the bin locations
[z,x]=hist(y,n) Same as hist(y,n) but returns two vectors z and x that contain
the frequency count and the bin locations
[z,x]=hist(y,x) Same as hist(y,x) but returns two vectors z and x that contain the
frequency count and the bin locations (return the same x)
Chen CL 12
Probability
Comparison of theory and experiment for 100 rolls of a single die
Theory:
1
6
= 16.67% for 1, 2, 3, 4, 5, 6
Experi. 21, 14, 18, 16, 19, 12 occurrences for 1, 2, 3, 4, 5, 6
Chen CL 13
Probability
Comparison of theory and experiment for 100 rolls of two dice
Probabilities for the sum of two dice
Sum 2 3 4 5 6 7 8 9 10 11 12
Probability (36) 1 2 3 4 5 6 5 4 3 2 1
Data for two dice
Sum 2 3 4 5 6 7 8 9 10 11 12
Frequency 5 5 8 11 20 10 8 12 7 10 4
Chen CL 14
Test Your Understanding
T7.1-2
If you roll a pair of balanced dice 200 times,
how many times would you expect to obtain a sum of 7 ?
How many times would you expect to obtain a sum of either 9, 10 or
11 ?
How many times would you expect to obtain a sum less than 7 ?
(ANS: 33, 50, 106)
Chen CL 15
Data: Heights of 100 Men 20 Years of Age
The heights were recorded to the nearest 1/2 inch. Plot the scaled frequency
histogram.
Height (inches) Frequency Height (inches) Frequency
64.0 1 70.0 9
64.5 0 70.5 8
65.0 0 71.0 7
65.5 0 71.5 5
66.0 2 72.0 4
66.5 4 72.5 4
67.0 5 73.0 3
67.5 4 73.5 1
68.0 8 74.0 1
68.5 11 74.5 0
69.0 12 75.0 1
69.5 10
Chen CL 16
Scaled Frequency Histogram
% Absolute frequency data
y_abs = [1,0,0,0,2,4,5,4,8,11,12,10,9,8,7,5,4,4,3,1,1,0,1];
binwidth = .5;
% Compute scaled frequency data
area = binwidth*sum(y_abs);
y_scaled = y_abs/area;
% Define the bins
bins = [64:binwidth:75];
% Plot the scaled histogram
bar(bins, y_scaled),...
xlabel(Height (inches)), ylabel(Scaled frequency)
% Compute prob. of a height lying between 67, 69 inches
% 6th to 11th values, (.1+.08+.16+.22+.24)*(.5)=.4, 40%
prob = cumsum(y_scaled)*binwidth;
prob67_69 = prob(11)-prob(6)
Chen CL 17
62 64 66 68 70 72 74 76
0
0.05
0.1
0.15
0.2
0.25
Height (inches)
S
c
a
l
e
d

f
r
e
q
u
e
n
c
y
prob67_69 =
0.4000
Chen CL 18
Continuous Approximation to the Scaled Histogram
Normal Probability Function
p(x) =
1

2
e
(x)
2
/2
2
Chen CL 19
Eect of Values on Normal Distribution Curve ( = 10)
Chen CL 20
Probability Interpretation of (a) the and (b) the 2
Pr(x| < x < + ) = 68.3%
Pr(x| 2 < x < + 2) = 95.5%
Pr(x| 3 < x < + 3) = 99.7%
Chen CL 21
Estimating the Mean and Standard Deviation
x =
1
n
(x
1
+ + x
n
) =
1
n
n

i=1
x
i
=
_

n
i=1
(x
i
x)
2
n 1
Statistical Functions
Command Description
mean(x) Calculate the mean of the data stored in vector x
median(x) Calculate the median of the data stored in vector x
std(x) Calculate the standard deviation of the data stored in vector x
cumsum(x) Creates a vector the same size as x, containing the cumulative
sum of the elements of x
Chen CL 22
Ex: Mean and Standard Deviation of
Heights
Statistical analysis of data on human proportions is required in many engineering
applications. For example, designers of submarine crew quarters need to know how
small they can make bunk lengths without eliminating a large percentage of
prospective crew members. Use MATLAB to estimate the mean and standard
deviation for the preceding height data.
% Absolute frequency data
y_abs = [1,0,0,0,2,4,5,4,8,11,12,10,9,8,7,5,4,4,3,1,1,0,1];
binwidth = .5;
% Define the bins
bins = [64:binwidth:75];
% Reconstruct the original (raw) height data
% from the absolute frequency data
% Fill the vector y_raw with the raw data
% Start with a null vector
Chen CL 23
y_raw = [];
for i = 1:length(y_abs)
if y_abs(i) > 0
new = bins(i)*ones(1, y_abs(i));
else
new = [ ];
end
y_raw = [y_raw, new];
end
% Compute the mean (69.3) and standard deviation (1.96)
mu = mean(y_raw), sigma = std(y_raw)
Chen CL 24
Use Error Function to Compute (Normal)
Probability
Pr(x b) =
1
2
_
1 + erf
_
b

2
__
Pr(a x b) =
1
2
_
erf
_
b

2
_
erf
_
a

2
__
Chen CL 25
Ex: Mean and Standard Deviation of
Heights
Use the results of last example to estimate how many twenty-year old men are no
taller than 68 inches. How many are within 3 inches of the mean ?
Pr(x 68) =
1
2
_
1 + erf
_
68 69.3
1.96

2
__
Pr(69.3 3 x 69.3 + 3) =
1
2
_
erf
_
3
1.96

2
_
erf
_
3
1.96

2
__
mu = 69.3;
sigma = 1.96;
% How many are no taller than 68 inches ?
b1 = 68;
p1 = (1+erf((b1-mu)/(sigma*sqrt(2))))/2
Chen CL 26
% How many are within 3 inches of the mean ?
a2 = 66.3;
b2 = 72.3;
p2 = (erf((b2-mu)/(sigma*sqrt(2)))...
- erf((a2-mu)/(sigma*sqrt(2))))/2
% ANS: p1 = 0.2536, p2 = 0.8741
p1 =
0.2536
p2 =
0.8741
Chen CL 27
Test Your Understanding
T7.2-1
Suppose that 10 more height measurements are obtained so that the
following numbers must be added to the preceding height table.
Height Add. data
64.5 1
65.0 2
66.0 1
67.5 2
70.0 2
73.0 1
74.0 1
(a) Plot the scaled frequency
histogram. (b) Find the mean and
standard deviation. (c) Use the mean
and standard deviation to estimate
how many 20-year-old men are no
taller than 69 inches. (d) Estimate
how many are between 68 and 72
inches tall.
(ANS: = 69.4, = 2.14; 43%; 63%)
Chen CL 28
Sums and Dierences of Random Variables
If x and y are normally distributed with means
x
and
y
, and
variances
2
x
and
2
y
, and if u = x + y and v = x y, then

u
=
x
+
y

v
=
x

2
u
=
2
v
=
2
x
+
2
y
Chen CL 29
Random Number Generation
Uniformly Distributed Random Numbers
rand
ans =
0.6161
rand
ans =
0.5184
Application Example:
if rand < 0.5
disp(heads)
else
disp(tails)
end
Chen CL 30
To generate same sequence:
rand(state,0)
rand
ans =
0.9501
rand
ans =
0.2311
rand(state,0)
rand
ans =
0.9501
rand
ans =
0.2311
OR
s = rand(state)
rand(state,s)
rand
ans =
0.6068
rand(state,s)
rand
ans =
0.6068
Chen CL 31
Test Your Understanding
T7.3-1
Use MATLAB to generate a vector y containing 1500 uniformly
distributed random numbers in the interval [5, 15]. Check your
results with the mean, min, and max functions.
Chen CL 32
Ex: Optimal Production Quantity
You have recently taken a position as the engineer in charge of your companys
seasonal product, which is manufactured during the o-season. You want to
determine the optimum production level. If you produce more units than you can
sell, your prot will not be as large as it could be; if you produce too many units,
the unsold units at the end of the season will hurt prots. A review of the
companys records shows that the xed cost of production is $30, 000 per season,
no matter how many units are made, and that it costs $2, 000 above the xed cost
to make one unit. In addition, past sales have uctuated between 25 and 50 units
per season, with no increasing or decreasing trend in the sales data. Your sales
force estimates that you cannot raise the price above $4, 000 during the season.
Assume also that at the end of the season all unsold units can be sold at $1, 000
each.
Chen CL 33
Solution:
The most reasonable model for the sales is a uniform distribution
over the interval [25, 50].
loop over k: compute prot for each of the 26 production levels
(25 50)
loop over m: random number simulation to compute prot
n = 5000; % number of random simulations
level = [25:50]; % initialize production level vector
cost = 30000+2000*level;
for k = 1:26
cum_profit = 0;
for m = 1:n
demand = floor(rand*(50-25)+26); % Round towards
if demand >= level(k) % minus infinity
Chen CL 34
income = 4000*level(k);
else
income = 4000*demand+1000*(level(k)-demand);
end
profit = income - cost(k);
cum_profit = cum_profit + profit;
end
expected_profit = cum_profit/n;
p(k,1) = level(k);
p(k,2) = expected_profit;
end
plot(p(:,1), p(:,2),o,p(:,1), p(:,2),-),...
xlabel(No. of units), ylabel(Profit ($))
Chen CL 35
25 30 35 40 45 50
2
2.2
2.4
2.6
2.8
3
3.2
3.4
3.6
3.8
x 10
4
No. of units
P
r
o
f
i
t

(
$
)
Chen CL 36
Test Your Understanding
T7.3-2
In T7.3-1, what is the optimum production level and resulting prot
if we sell unsold units at the end of the season at cost ($2, 000),
instead of below cost ?
(ANS: 50 units; prot = $46, 000)
Chen CL 37
Random Number Generation
Normally Distributed Random Numbers
randn(n) n n ND random number, mean=0, SD=1
randn(m, n) mn ND random number, mean=0, SD=1
y = x + random number, mean=, SD=
Chen CL 38
Test Your Understanding
T7.3-3
Use MATLAB to generate a vector y containing 1, 800 random
numbers normally distributed with a mean of 7 and a standard
deviation of 10. Check your results with the mean and std functions.
Why cant you us the min and max functions to check your results ?
Chen CL 39
Functions of Random Variables
y = bx + c

y
= b
x
+ c

y
= |b|
x
Chen CL 40
Ex: Statistical Analysis and Manufacturing
Tolerances
Suppose you must cut a triangular piece o the corner of a square plate by
measuring the distances x and y from the corner. The desired value of x is 10
inches, and the desired value of is 20
o
. This requires that y = 3.64 inches. We
are told that measurements of x and y are normally distributed with means of 10
and 3.64, respectively, with a standard deviation equal to 0.05 inch. Determine
the standard deviation of and plot the relative frequency histogram for .
Chen CL 41
Solution:
= arctan(y/x)
s = 0.05; % SD of x and y
n = 8000; % number of random simulation
x = 10+s*randn(1,n);
y = 3.64+s*randn(1,n);
theta = (180/pi)*atan(y./x);
mean_theta = mean(theta)
sigma_theta = std(theta)
xp = [19:.1:21];
z = hist(theta, xp);
yp = z/n;
bar(xp,yp), xlabel(theta (deg)),...
ylabel(relative frequency)
18.5 19 19.5 20 20.5 21 21.5
0
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
theta (deg)
r
e
la
t
iv
e

f
r
e
q
u
e
n
c
y
mean_theta =
20.0041
sigma_theta =
0.2700
Chen CL 42
Interpolation
Time Temp (
o
F)
7 AM 49
9 AM 57
11 AM 71
12 noon 75
x = [ 7, 9, 11, 12];
y = [49, 57, 71, 75];
x_int = [8, 10];
interp1(x,y,x_int)
ans =
53
64
7 7.5 8 8.5 9 9.5 10 10.5 11 11.5 12
45
50
55
60
65
70
75
Chen CL 43
Interpolation
Temperatures (
o
F)
Time Loc.1 Loc.2 Loc.3
7 AM 49 52 54
9 AM 57 60 61
11 AM 71 73 75
12 noon 75 79 81
x = [ 7, 9, 11, 12];
y(:,1) = [49, 57, 71, 75];
y(:,2) = [52, 60, 73, 79];
y(:,3) = [54, 61, 75, 81];
x_int = [8, 10];
interp1(x,y,x_int)
ans =
53.0000 56.0000 57.5000
64.0000 65.5000 68.0000
Chen CL 44
Two-Dimensional Interpolation
x = [0, 1];
y = [0, 2];
z = [49, 54; 53, 57];
z =
49 54
53 57
interp2(x,y,z, .6, 1.5)
ans =
54.5500
Chen CL 45
Linear Interpolation Functions
y_int=interp1(x,y,x_int)
Used to linearly interpolate a function of one variable: y=f(x).
Returns a linearly interpolated vector y_int at the specied value
x_int, using data stored in x and y.
z_int=interp2(x,y,z,x_int,y_int)
Used to linearly interpolate a function of two variables: z=f(x,y).
Returns a linearly interpolated vector z_int at the specied values
x_int,y_int, using data stored in x,y and z.
Chen CL 46
Cubic-Spline Interpolation
y
i
(x) = a
i
(x x
i
)
3
+ b
i
(x x
i
)
2
+ c
i
(x x
i
) + d
i
x [x
i
, x
i+1
]
Time Temp (
o
F)
7 AM 49
9 AM 57
11 AM 71
12 noon 75
7 x 9 y
1
(x) = 0.35(x 7)
3
+ 2.85(x 7)
2
0.3(x 7) + 49
9 x 11 y
2
(x) = 0.35(x 9)
3
+ 0.75(x 9)
2
+ 6.9(x 9) + 57
11 x 12 y
3
(x) = 0.35(x 11)
3
1.35(x 11)
2
+ 5.7(x 11) + 71
Chen CL 47
Cubic-Spline Interpolation
x = [ 7, 9, 11, 12];
y = [49, 57, 71, 75];
x_int = [7 :.01 :12];
y_int = spline(x,y,x_int);
plot(x,y,o,x,y,--,x_int,y_int),...
xlabel(Time (hrs)), ylabel(Temperature (deg F)),...
title(Temperature measurements at a specific location),...
axis([7 12 45 80])
7 7.5 8 8.5 9 9.5 10 10.5 11 11.5 12
45
50
55
60
65
70
75
80
Time (hrs)
T
e
m
p
e
r
a
t
u
r
e

(
d
e
g

F
)
Temperature measurements at a specific location
Chen CL 48
Polynomial Interpolation Functions
x = [ 7, 9, 11, 12];
y = [49, 57, 71, 75];
[breaks,coeffs, m, n] = unmkpp(spline(x,y))
breaks =
7 9 11 12
coeffs =
-0.3500 2.8500 -0.3000 49.0000
-0.3500 0.7500 6.9000 57.0000
-0.3500 -1.3500 5.7000 71.0000
m =
3
n =
4
Chen CL 49
Polynomial Interpolation Functions
x = [ 7, 9, 11, 12];
y = [49, 57, 71, 75];
[breaks,coeffs, m, n] = unmkpp(pchip(x,y))
breaks =
7 9 11 12
coeffs =
-0.1023 0.9545 2.5000 49.0000
-0.9920 2.9385 5.0909 57.0000
-0.0588 -0.8824 4.9412 71.0000
m =
3
n =
4
Chen CL 50
Polynomial Interpolation Functions
y_est=interp1(x,y,x_est,spline)
Returns a column vector y_est that contains the estimated values of y that
correspond to the x values specied in the vector x_est, using cubic spline
functions.
y_int=spline(x,y,x_int)
Computes a cubic-spline interpolation where x and y are vectors containing the
data and x_int is a vector containing the values of the independent variable
x at which we wish to estimate the dependent variable y. The result y_int is
a vector the same size as x_int containing the interpolated values of y that
correspond to x_int.
[breaks, coeffs, m, n] = unmkpp(spline(x,y))
Computes the coecients of the cubic-spline polynomials for the data in x and
y. The vector breaks contains the x values, and coeffs is an m n matrix
containing the polynomial coecients. m is the number of polynomials and n is
the number of coecients for each polynomial.
Chen CL 51
Polynomial Interpolation Functions: 1D
y_est=interp1(x,y,x_est,method)
This command species alternate methods. The default is linear interpolation.
Available methods are:
nearest - nearest neighbor interpolation
linear - linear interpolation
spline - piecewise cubic spline interpolation (SPLINE)
pchip - piecewise cubic Hermite interpolation (PCHIP)
cubic - same as pchip
Chen CL 52
Polynomial Interpolation Functions: 2D
z_est=interp2(x,y,z,x_est,y_est,method)
This command species alternate methods. The default is linear interpolation.
Available methods are:
nearest - nearest neighbor interpolation
linear - bilinear interpolation (or bilinear)
spline - cubic spline interpolation (SPLINE)
cubic - bicubic interpolation
Chen CL 53
1D Interpolation: Grid Data
x = 0 : 1 : 4*pi;
y = sin(x).*exp(-x/5);
xi = 0 : 0.1 : 4*pi;
y1 = interp1(x, y, xi, nearest);
y2 = interp1(x, y, xi, linear);
y3 = interp1(x, y, xi, spline);
y4 = interp1(x, y, xi, cubic);
plot(x,y,ko), axis([0 14 -0.4 1])
hold on
plot(xi,y1,b-);
plot(xi,y2,r:);
plot(xi,y3,LineStyle,-.,Color,[0,0.40,0]);
plot(xi,y4,LineStyle,--,Color,[0.50,0.23,0.13]);
%plot( x, y,o, ...
% xi,y1,g-, ...
% xi,y2,r:, ...
% xi,y3,LineStyle,-.,Color,[0,0.40,0], ...
% xi,y4,LineStyle,--,Color,[0.50,0.23,0.13]);
legend(Original,Nearest,Linear,Spline,Cubic)
hold off
Chen CL 54
0 2 4 6 8 10 12 14
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Chen CL 55
0 2 4 6 8 10 12 14
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Chen CL 56
0 2 4 6 8 10 12 14
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Chen CL 57
0 2 4 6 8 10 12 14
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Chen CL 58
0 2 4 6 8 10 12 14
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Chen CL 59
0 2 4 6 8 10 12 14
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Original
Nearest
Linear
Spline
Cubic
Chen CL 60
2D Interpolation: Grid Data
[x, y ] = meshgrid(-3 : 1 : 3);
[xi,yi] = meshgrid(-3 : 0.25 : 3);
z = peaks(x, y);
zi1 = interp2(x, y, z, xi, yi, nearest);
zi2 = interp2(x, y, z, xi, yi, bilinear);
zi3 = interp2(x, y, z, xi, yi, bicubic);
subplot(2,2,1); surf(x, y, z);
axis tight; title(Original);
subplot(2,2,2); surf(xi, yi, zi1);
axis tight; title(Nearest);
hold on
subplot(2,2,2); plot3(x, y, z, ko); hold off
subplot(2,2,3); surf(xi, yi, zi2);
axis tight; title(Bilinear);
hold on
subplot(2,2,3); plot3(x, y, z, ko); hold off
subplot(2,2,4); surf(xi, yi, zi3);
axis tight; title(Bicubix);
hold on
subplot(2,2,4); plot3(x, y, z, ko); hold off
Chen CL 61
2
0
2
2
0
2
4
2
0
2
4
Original
2
0
2
2
0
2
4
2
0
2
4
Nearest
2
0
2
2
0
2
4
2
0
2
4
Bilinear
2
0
2
2
0
2
4
2
0
2
4
Bicubix
Chen CL 62
subplot(2,2,1); contour(x, y, z, 20); title(Original);
subplot(2,2,2); contour(xi,yi,zi1,20); title(Nearest);
subplot(2,2,3); contour(xi,yi,zi2,20); title(Bilinear);
subplot(2,2,4); contour(xi,yi,zi3,20); title(Bicubic);
3 2 1 0 1 2 3
3
2
1
0
1
2
3
Original
3 2 1 0 1 2 3
3
2
1
0
1
2
3
Nearest
3 2 1 0 1 2 3
3
2
1
0
1
2
3
Bilinear
3 2 1 0 1 2 3
3
2
1
0
1
2
3
Bicubic
Chen CL 63
2D Spline Interpolation
[x, y ] = meshgrid(-3:1 :3);
[xi,yi] = meshgrid(-3:0.25:3);
z = peaks(x, y);
zi4=interp2(x,y,z,xi,yi,spline);
surf(xi, yi, zi4);
axis tight;
title(Spline);
hold on
plot3(x, y, z, ko);
hold off
3
2
1
0
1
2
3
3
2
1
0
1
2
3
5
0
5
Spline
Chen CL 64
2D Interpolation: Scatter Data
x = 6*rand(100,1)-3; % uniform distribution in -3,3
y = 6*rand(100,1)-3;
z = peaks(x, y);
[xi,yi] = meshgrid(-3:0.2:3, -3:0.2:3);
zi = griddata(x, y, z, xi, yi);
close all
mesh(xi, yi, zi);
hold on
plot3(x, y, z, ko);
hold off
axis tight;
hidden off
Chen CL 65
3
2
1
0
1
2
3
3
2
1
0
1
2
3
4
2
0
2
4
6
Chen CL 66
Ex: Application to Robot Path Control
A robot arm having two joints:
(a) Dimensions and arm angles
(b) The hands path is not a straight
line, but a complicated curve
Chen CL 67
The hands coordinates (x, y)
x
elbow
= L
1
cos
1
y
elbow
= L
1
sin
1
x =
x
elbow
..
L
1
cos
1
+L
2
cos(
1
+
2
)
y = L
1
sin
1
. .
y
elbow
+L
2
sin(
1
+
2
)
R
2
= x
2
+ y
2
cos
2
=
R
2
L
2
1
L
2
2
2L
1
L
2
cos =
R
2
+L
2
1
L
2
2
2L
1
R
= arctan
y
x

1
=
_
+ if
2
< 0
if
2
0
If we use a cubic polynomial to express the joint angles as functions of time for
0 t T, then we can specify four conditions for each angle. Two of these
conditions should require the polynomial to pass through the starting value (0)
and the ending value (T). The other two conditions require that the slope of
the polynomial should be zero at the start and nish because we want the robot
to start from rest and to nish at rest. The cubic polynomial that satises these
Chen CL 68
conditions is
(t) = at
3
+ bt
2
+ (0)
a = 2[(0) (T)]/T
3
b = 3[(0) (T)]/T
2
Example: L
1
= 4, L
2
= 3 feet, x(0) = 6.5, y(0) = 0 x(2) = 0, y(2) = 6.5
Solution:
1
(0) = 18.717
o
,
2
(0) = 44.049
o
;
1
(2) = 71.283
o
,
2
(2) = 44.049
o

1
(t) = 22.5t
3
+ 67.5t
2
18.717;
2
(t) = 44.049
Suppose we want to control the arm path to be a straight line connecting the
starting and ending hand positions (y = y
0
+
y
f
y
0
x
f
x
0
(x x
0
)). These points are
called knot points. To obtain smooth motion, we want the hand to pass through
these points without stopping or changing speed, so we use cubic splines to
interpolate between these points.
Write a MATLAB program to compute the arm angle solution for three knot
points. Write another program to compute the splines required to generate three
knot points and to plot the path of the robots hand. Do this problem for the
case where L
1
= 4, L
2
= 3, (x
0
, y
0
) = (6.5, 0), (x
f
, y
f
) = (0, 6.5).
Chen CL 69
% Solution for joint angles using n knot points
L1=4; L2=3; % Enter the arm lengths, (x0,y0), (xf,yf), and
x0=6.5; y0=0; xf=0; yf=6.5; n=4; % number of knot points n
% Obtain the points on a straight line from (x0,y0) to (xf,yf)
x = linspace(x0,xf,n+1); y = ((yf-y0)/(xf-x0))*(x-x0)+y0;
R = sqrt(x.^2+y.^2); % Begin the angle solution (in degrees)
theta2 = acos((R.^2-L1^2-L2^2)/(2*L1*L2))*(180/pi);
beta = acos((R.^2+L1^2-L2^2)/(2*L1*R ))*(180/pi);
alpha = atan2(y,x)*(180/pi);
if theta2 < 0
theta1 = alpha + beta;
else
theta1 = alpha - beta;
end
theta1, theta2
theta1 =
-28.5996 -10.1647 16.4004 42.9654 61.4004
theta2 =
44.0486 86.6409 99.2916 86.6409 44.0486
Chen CL 70
% Robot path using splines with three knot points
% Define the joint angles and times along the straight line path
theta1 = [-18.7170, -17.2129, 4.8980, 35.9172, 71.2830];
theta2 = [ 44.0486, 86.6409, 99.2916, 86.6409, 44.0486];
t_i = linspace(0, 2, 200); % Use 200 points for interpolation
theta1_i = spline(t, theta1, t_i);
theta2_i = spline(t, theta2, t_i);
x_elbow = [4*cos(theta1_i*(pi/180))]; % Compute the (x,y)
y_elbow = [4*sin(theta1_i*(pi/180))]; % coordinates of the elbow
x = x_elbow + 3*cos((theta1_i+theta2_i)*(pi/180)); % Compute (x,y)
y = y_elbow + 3*sin((theta1_i+theta2_i)*(pi/180)); % of the hand
x1=[0, x_elbow(1), x(1) ]; y1=[0, y_elbow(1), y(1) ]; % Compute
x2=[0, x_elbow(25), x(25) ]; y2=[0, y_elbow(25), y(25) ]; % arms
x3=[0, x_elbow(50), x(50) ]; y3=[0, y_elbow(50), y(50) ]; % position
x4=[0, x_elbow(75), x(75) ]; y4=[0, y_elbow(75), y(75) ]; % to
x5=[0, x_elbow(100), x(100)]; y5=[0, y_elbow(100), y(100)]; % display
x6=[0, x_elbow(125), x(125)]; y6=[0, y_elbow(125), y(125)];
x7=[0, x_elbow(150), x(150)]; y7=[0, y_elbow(150), y(150)];
x8=[0, x_elbow(175), x(175)]; y8=[0, y_elbow(175), y(175)];
x9=[0, x_elbow(200), x(200)]; y9=[0, y_elbow(200), y(200)];
Chen CL 71
% Plot the results
% plot(x,y, x1,y1, x2,y2, x3,y3, x4,y4, x5,y5),...
plot(x1,y1,Color,[1,0,0],LineWidth,2)
axis([0 7 -2 7]),...
title(Path of robot hand),...
xlabel(x (feet)), ylabel(y (feet))
hold on
plot(x, y, --, Color,[0.5,0.5,0.5])
plot(x2,y2,Color,[0.,.5,0.],LineWidth,2)
plot(x3,y3,Color,[0.,0.,1.],LineWidth,2)
plot(x4,y4,Color,[1.,.5,0.],LineWidth,2)
plot(x5,y5,Color,[1.,0.,1.],LineWidth,2)
plot(x6,y6,Color,[0.,.5,1.],LineWidth,2)
plot(x7,y7,Color,[1.,.5,.5],LineWidth,2)
plot(x8,y8,Color,[.3,.5,.6],LineWidth,2)
plot(x9,y9,Color,[.5,0.,0.],LineWidth,2)
hold off
Chen CL 72
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 73
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 74
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 75
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 76
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 77
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 78
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 79
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 80
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 81
Robot Path Control
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 82
Animation
%%%% Animation: Robot Arm
% Robot path using splines with three knot points
% Define the joint angles and times along the straight line path
theta1 = [-18.7170, -17.2129, 4.8980, 35.9172, 71.2830];
theta2 = [ 44.0486, 86.6409, 99.2916, 86.6409, 44.0486];
t = [0, 0.5, 1.0, 1.5, 2.0];
t_i = linspace(0, 2, 200); % Use 200 points for interpolation
theta1_i = spline(t, theta1, t_i);
theta2_i = spline(t, theta2, t_i);
x_elbow = [4*cos(theta1_i*(pi/180))]; % Compute the (x,y)
y_elbow = [4*sin(theta1_i*(pi/180))]; % coordinates of the elbow
x = x_elbow + 3*cos((theta1_i+theta2_i)*(pi/180)); % Compute (x,y)
y = y_elbow + 3*sin((theta1_i+theta2_i)*(pi/180)); % of the hand
% Plot the initial position and the desired path
xx=[0, x_elbow(1), x(1)]; yy=[0, y_elbow(1), y(1)];
plot(xx,yy,Color,[0,0,0],LineWidth,2);
axis([0 7 -2 7]),...
title(Path of robot hand),...
xlabel(x (feet)), ylabel(y (feet))
Chen CL 83
hold on
plot(x,y,--,Color,[0.5,0.5,0.5])
% abimation
step = 20;
for i = step:step:200
xx=[0, x_elbow(i), x(i)]; yy=[0, y_elbow(i), y(i)];
c = [0.005*i,0,1-0.005*i];
plot(xx,yy,Color,c,LineWidth,2);
drawnow
end
hold off
Chen CL 84
Robot Path Control: Animation
0 1 2 3 4 5 6 7
2
1
0
1
2
3
4
5
6
7
Path of robot hand
x (feet)
y

(
f
e
e
t
)
Chen CL 85
Thank You for Your Attention
Questions Are Welcome

Vous aimerez peut-être aussi