Professional Documents
Culture Documents
1
9
9
7
q
w
y
t
e
u
w
d
r
j
h
e
w
f
h
w
e
k
j
h
f
w
j
e
2
3
4
3
5
4
3
5
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Stem, stairs, and bar plots
stem(x,y)
stairs(x,y)
bar(x,y)
See also table 5.3-1
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Separate Y axes
plotyy(x1,y1,x2,y2) %Y
y1 v.s. x1; y2 v.s. x2
plotyy(x1,y1,x2,y2,type1,type2)
plotyy(x1,y1,x2,y2,plot,stem)%
y1 v.s. x1 plot
y2 v.s. x2 stem
Puff! The magic dragon,
Live by the sea.
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
0
10
20
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
0
10
20
x=[1:6];
y1=2*x;
y2=3*x;
plotyy(x,y1,x,y2,'plot','stem')
Puff! The magic dragon,
Live by the sea.
Polar plot
orbit eccentricity = 0.5
1
2
3
4
30
210
60
240
90
270
120
300
150
330
180 0
theta=[0:pi/90:2*pi];
r=2./(1-0.5*cos(theta));
polar(theta,r),title('orbit eccentricity = 0.5')
Puff! The magic dragon,
Live by the sea.
Self test p.271
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
The plot editor
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
5.5 Function discovery
1. Linear function
2. Power function
3. Exponential function
Experimental data
mx mx
m
b be x y
bx x y
b mx x y
) 10 ( or , ) (
) (
) (
=
=
+ =
Puff! The magic dragon,
Live by the sea.
Step of function discovery
P.277
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Applications
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
deflection=[0,.09,.18,.28,.37,.46,.55,.65,.74];
force=[0:100:800];
%plot with linear scale
subplot(2,1,1)
plot(force,deflection,'o'),...
xlabel('force(lb)'),ylabel('deflection(in)'),...
axis([0 800 0 .8])
%curve fitting with a line
p=polyfit(force,deflection,1);
k=1/p(1)
%plot the fitted line
f=[0:2:800];
x=f/k;
subplot(2,1,2)
plot(f,x,force,deflection,'o'),...
xlabel('force(lb)'),ylabel('deflection(in)'),...
axis([0 800 0 .8])
Puff! The magic dragon,
Live by the sea.
0 100 200 300 400 500 600 700 800
0
0.2
0.4
0.6
0.8
force(lb)
d
e
f
l
e
c
t
i
o
n
(
i
n
)
0 100 200 300 400 500 600 700 800
0
0.2
0.4
0.6
0.8
force(lb)
d
e
f
l
e
c
t
i
o
n
(
i
n
)
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Heat transfer
predict when will T(F) achieve 120F?
t(sec) T(F)
0 145
620 130
2266 103
3482 90
120 t=?
Puff! The magic dragon,
Live by the sea.
model
Linear model: T-68=mt+b
Exponential model: T-68=b(10)
mt
Puff! The magic dragon,
Live by the sea.
%input data
time=[0,620,2266,3482];
temp=[145,130,103,90];
%substract room temp.
temp=temp-68;
% use linear scale
subplot(2,2,1)
plot(time,temp,time,temp,'o'),...
xlabel('Time(sec)'),ylabel('temperature(F)')
%use exponential plot
subplot(2,2,2)
semilogy(time,temp,time,temp,'o'),...
xlabel('Time(sec)'),ylabel('temperature(F)')
Puff! The magic dragon,
Live by the sea.
%use linear fitting
p=polyfit(time,log10(temp),1);
m=p(1)
b=10^p(2)
%calculate the time needed to 120F
t_120=(log10(120-68)-log10(b))/m
%estimation
t=[0:10:4000];
T=68+b*10.^(m*t);
subplot(2,2,3)
semilogy(t,T-68,time,temp,'o',t_120,120-68,'+'),...
xlabel('time(sec)'),ylabel('temp(F)')
%plot fitted line
subplot(2,2,4)
plot(t,T,time,temp+68,'o',t_120,120,'+'),...
xlabel('time(sec)'),ylabel('temp(F)')
Puff! The magic dragon,
Live by the sea.
0 1000 2000 3000 4000
20
40
60
80
Time(sec)
t
e
m
p
e
r
a
t
u
r
e
(
F
)
0 1000 2000 3000 4000
10
1
10
2
Time(sec)
t
e
m
p
e
r
a
t
u
r
e
(
F
)
0 1000 2000 3000 4000
10
1
10
2
time(sec)
t
e
m
p
(
F
)
0 1000 2000 3000 4000
80
100
120
140
160
time(sec)
t
e
m
p
(
F
)
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Interpolation and extrapolation
Are the interpolation or extrapolation correct?
Are they still in the feasible region of the
model?
e.g. elastic ring, the linear elasticity is
applicable in the regions of small deformation,
not for large deformation.
Puff! The magic dragon,
Live by the sea.
Hydraulic engineering
Fluid mechanics
Fluid engineering.
volume V
pressure p
rate flow
=
=
=
=
=
f
V r f
p c f
Puff! The magic dragon,
Live by the sea.
V()
t(sec)
15
6
12
7
9
8
6
9
Puff! The magic dragon,
Live by the sea.
% problem description
cups=[6,9,12,15];
meas_times=[9,8,7,6];
meas_flow=1./meas_times;
%line fitting
p=polyfit(log10(cups),log10(meas_flow),1);
coeffs=[p(1),10^p(2)];
m=coeffs(1)
b=coeffs(2)
%log-log plot as well as linear plot
x=[6:.01:40];
y=b*x.^m;
subplot(2,1,1)
loglog(x,y,cups,meas_flow,'o'),grid,...
xlabel('V(cups)'),ylabel('1 cup full time(sec)'),...
axis([5 15 .1 .3])
% plot and predict 36 cups
subplot(2,1,2)
plot(x,1./y,cups,meas_times,'o'),grid,...
axis([5 36 0 10])
%calculate V=36 cups' fill time (sec)
V=36
f_36=b*V^m
Puff! The magic dragon,
Live by the sea.
10
1
10
-1
V(cups)
1
c
u
p
f
u
l
l
t
i
m
e
(
s
e
c
)
5 10 15 20 25 30 35
0
2
4
6
8
10
4.2sec
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
5.6 Regression
Correlation analysis
Regression
Puff! The magic dragon,
Live by the sea.
The least squares method
Find a line (y=mx+b), such that, the sum of the
square errors between the point and line is min.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
= = = = =
=
+ + + =
+ =
n
i
i
n
i
i i
n
i
i
n
i
i
n
i
i
n
i
i i
y b y x a x ab y nb x a
y b ax J
1 1 1 1
2
2
1
2
2
1
2
2 2 2
) (
0 , 0 =
c
c
=
c
c
b
J
a
J
= +
= +
= =
= = =
n
i
i
n
i
i
n
i
i i
n
i
i
n
i
i
y nb a x
y x b x a x
1 1
1 1 1
2
) (
) ( ) (
Find a,b
Such that J is min.
Puff! The magic dragon,
Live by the sea.
x=[1:9];
y=[5,6,10,20,28,33,34,36,42];
xp=[1:.01:9];
for k=1:4
coeff=polyfit(x,y,k)
yp(k,:)=polyval(coeff,xp);
J(k)=sum((polyval(coeff,x)-y).^2);
end
subplot(2,2,1)
plot(xp,yp(1,:),x,y,'o'),axis([0 10 0 50])
subplot(2,2,2)
plot(xp,yp(2,:),x,y,'o'),axis([0 10 0 50])
subplot(2,2,3)
plot(xp,yp(3,:),x,y,'o'),axis([0 10 0 50])
subplot(2,2,4)
plot(xp,yp(4,:),x,y,'o'),axis([0 10 0 50])
disp(J)
Puff! The magic dragon,
Live by the sea.
0 5 10
0
10
20
30
40
50
0 5 10
0
10
20
30
40
50
0 5 10
0
10
20
30
40
50
0 5 10
0
10
20
30
40
50
J = 71.5389 56.6727 41.8838 4.6566
S= 1562 1562 1562 1562
r^2= .9542 .9637 .9732 .9970
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Self testing p.292
Puff! The magic dragon,
Live by the sea.
Fitting other functions
y=bx
m
p=polyfit(log10(x),log10(y),1)
p(1)=m, p(2)=log
10
b
y=b(10)
mx
p=polyfit(x,log10(y),1)
P(1)=m, p(2)= log
10
b
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
The quality of a curve fit
Coefficient of determination
r^2
fitting good for % 99 r
fitting good , 1 ~ , 1
1
] [
] ) ( [
2
2
2
1
2
1
2
>
= =
=
=
=
=
=
r J
S
J
r
y y S
y x f J
m
i
i
m
i
i i
Puff! The magic dragon,
Live by the sea.
Regression and numerical
accuracy
(1)
(2)
(format long, or format long e)
e.g. fitting for the data in p.295
Puff! The magic dragon,
Live by the sea.
0 .1
2 1.884
4 2.732
6 3.388
8 3.346
10 3
12 2.644
14 2.022
16 1.65
18 1.5838
20 1.35
22 1.0082
24 .718
26 .689
28 .4308
30 .203
32 .1652
34 -.073
36 -.002
38 -.1122
40 .106
x y
05773943 . 0
20369642 . 1 15178007 . 0
10 7777 . 7 10 9209 . 1
10 1 . 2 0
217 0577394277 . 0
0774 2036964239 . 1 7153 1517800652 . 0
10 6 7777099161 . 7 10 920896532 . 1
10 09690135 . 2 10 33551 . 6
2
3 3 4 4
5 6 6
2
3 3 4 4
5 6 6 9
+
+
+
+ =
+
+
+
+ =
x x
x x
x x y
x x
x x
x x y
Puff! The magic dragon,
Live by the sea.
x1=[0:2:40];
y1=[.1,1.884,2.732,3.388,3.346,3,2.644,2.022,1.65,1.58
38,1.35,1.0082,.718,.689,.4308,.203,.1652,-.073,-.002,-
.1122,.106];
[p, s]=polyfit(x1,y1,6);
format long
x=[0:.1:40];
y = polyval(p,x);
subplot(2,1,1)
plot(x,y,x1,y1,'o')
subplot(2,1,2)
p2=round(p.*10^8)/10^8;
y2 = polyval(p2,x);
plot(x,y2,x1,y1,'o')
p
p2
Puff! The magic dragon,
Live by the sea.
0 5 10 15 20 25 30 35 40
-1
0
1
2
3
4
0 5 10 15 20 25 30 35 40
-15
-10
-5
0
5
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
p.297
~ [0, 15]; (15, 40]
8 !
Puff! The magic dragon,
Live by the sea.
Give some criticism on the following figure!
Puff! The magic dragon,
Live by the sea.
Scaling the data
x=x-x
min
or x=x-x
mean
x=x/x
mean
or x=x/x
max
Puff! The magic dragon,
Live by the sea.
Year=[1990:1999];
Veh_Flow=[2.1,3.4,4.5,5.3,6.2,6.6,6.8,7,7.4,7.8];
p=polyfit(Year,Veh_Flow,3)
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 2.791805e-019.
> In c:\matlab\toolbox\matlab\polyfun\polyfit.m at line 48
p =
1.0e+007 *
0.0000 0.0000 0.0104 -6.9010
year 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
million
2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8
Puff! The magic dragon,
Live by the sea.
Year=[1990:1999];
Veh_Flow=[2.1,3.4,4.5,5.3,6.2,6.6,6.8,7,7.4,7.8];
x=Year-1990;
p=polyfit(x,Veh_Flow,3)
J=sum((polyval(p,x)-Veh_Flow).^2);
S=sum((Veh_Flow-mean(Veh_Flow)).^2);
r2=1-J/S
p =
0.0087 -0.1851 1.5991 2.0362
r2 =
0.9972
year 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
million
2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8
Puff! The magic dragon,
Live by the sea.
2000 yield
polyval(p,10)
ans =
8.1767 million cars
year 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
million
2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8
Puff! The magic dragon,
Live by the sea.
Using residuals
P.300 example!
% time data
x=[0:19];
% contamination data
y=[6,13,23,33,54,83,118,156,210,282,...
350,440,557,685,815,990,1170,1350,1575
,1830];
%linear fitting
p1=polyfit(x,y,1);
% quadratic fitting
p2=polyfit(x,y,2);
% cubic fitting
p3=polyfit(x,y,3);
% exponential fitting
p4=polyfit(x,log10(y),1);
% residual
res1=polyval(p1,x)-y;
res2=polyval(p2,x)-y;
res3=polyval(p3,x)-y;
res4=10.^polyval(p4,x)-y;
Puff! The magic dragon,
Live by the sea.
Plot the residual
Puff! The magic dragon,
Live by the sea.
Linear model (0,0)
= =
=
=
= c c
=
=
m
i
i i
m
i
i
m
i
i i
y x x a
a J
y x a J
x a y
1 1
2
1
1
1
2
1
1
have we
0 / let
) (
Puff! The magic dragon,
Live by the sea.
Quadratic model (0,0)
= = =
= = =
=
= +
= +
= c c = c c
+ =
+ + =
m
i
i i
m
i
i
m
i
i
m
i
i i
m
i
i
m
i
i
m
i
i i i
y x x a x a
y x x a x a
a J a J
y x a x a J
a x a x a y
1 1
2
2
1
3
1
1
2
1
3
2
1
4
1
2 1
1
2
2
2
1
3 2
2
1
have we
0 / 0 / let
) (
Puff! The magic dragon,
Live by the sea.
Not pass (0,0)
0 0 1
0 0 2
2
0 1
) (
) ( ) (
y x x a y
y x x a x x a y
+ =
+ + =
Puff! The magic dragon,
Live by the sea.
Multiple linear regression
y=a
0
+a
1
x
1
+a
2
x
2
Data (y, x
1
, x
2
)
(
(
(
(
(
(
=
(
(
(
(
(
(
=
(
(
(
=
=
n
n n
y
y
y
y
x x
x x
x x
x x
a
a
a
...
1
...
1
1
1
3
2
1
2 1
23 13
22 12
21 11
2
1
0
y
X a
y Xa
Puff! The magic dragon,
Live by the sea.
Example 5.6-3
x1=[0:3]';x2=[5,7,8,11]';
y=[7.1,19.2,31,45]';
X=[ones(size(x1)) x1 x2];
a=X\y
yp=X*a;
Max_percent_error=100*max(abs((yp-y)./y))
a =
0.8000
10.2429
1.2143
Max_percent_error =
3.2193
y=0.8+10.2429x
1
+1.2143x
2
Puff! The magic dragon,
Live by the sea.
Linear-in-the-parameters
regression
P.305 example
Self testing
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
5.7 the basic fitting interface
P.306
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
5.8 3-D Plots
3-d line plots
Surface mesh plot
Contour plots
Puff! The magic dragon,
Live by the sea.
t=[0:pi/50:10*pi];
plot3(exp(-.05*t).*sin(t),exp(-.05*t).*cos(t),t),...
xlabel('x'),ylabel('y',zlabel('z'),grid)
-1
-0.5
0
0.5
1
-1
-0.5
0
0.5
1
0
10
20
30
40
x
z
t z
t e y
t e x
t
t
=
=
=
cos
sin
05 . 0
05 . 0
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
[X,Y]=meshgrid(-2:.1:2);
Z=X.*exp(-((X-Y.^2).^2+Y.^2));
mesh(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z')
] ) [(
2 2 2
y y x
xe z
+
=
Puff! The magic dragon,
Live by the sea.
[X,Y]=meshgrid(-2:.1:2);
Z=X.*exp(-((X-Y.^2).^2+Y.^2));
contour(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z')
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
x
y
] ) [(
2 2 2
y y x
xe z
+
=
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.