You are on page 1of 138

Puff!

The magic dragon,


Live by the sea.
5
plotting & model building
Puff! The magic dragon,
Live by the sea.


Puff! The magic dragon,
Live by the sea.
xy plotting functions
y=f(x)
abscissa ~ x (independent variable)
ordinate ~ y (dependent variable)
Scale
tick mark
axis label or axial title
data symbol or point marker
Legend ~

Puff! The magic dragon,
Live by the sea.
line ~ for theoretical calculation data
data symbol ~ for experimental data
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Requirements of correct plot
P.243-244
Read it NOW!
Puff! The magic dragon,
Live by the sea.
Plot, Label & Title commands
x=[0:.1:52];
y=.4*sqrt(1.8*x);
plot(x,y),xlabel('()'),ylabel('()'),...
title('')
grid on, axis([0 52 0 5])

Copy figure
Paste it in the word file
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Plot of complex numbers
plot(y)=plot(real(y),imag(y)) %if y is complex

z=.1+.9i;
n=[0:0.01:10];
plot(z.^n),xlabel('real part'),ylabel('imaginary
part')
Puff! The magic dragon,
Live by the sea.
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1

Puff! The magic dragon,


Live by the sea.
Puff! The magic dragon,
Live by the sea.
The function plot command fplot
fplot(string,[xmin xmax])
fplot(string,[xmin xmax ymin ymax])

f='cos(tan(x))-tan(sin(x))';
fplot(f,[1 2])

Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
x=[1:.01:2];
y=cos(tan(x))-tan(sin(x));
plot(x,y)


Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Plotting polynomials
x=[-6:0.01:6];
p=[3,2,-100,2,-7,90];
plot(x,polyval(p,x)),xlabel('x'),ylabel('p')
-6 -4 -2 0 2 4 6
-3000
-2000
-1000
0
1000
2000
3000
4000
5000
x
p
Puff! The magic dragon,
Live by the sea.
Self testing
pp.250-251
Write down the program now!
Puff! The magic dragon,
Live by the sea.
Subplot
subplot(m,n,p) %mn p

x=[0:.01:5];
y=exp(-1.2*x).*sin(10*x+5);
subplot(1,2,1)
plot(x,y),xlabel('x1'),ylabel('y1'),axis([0 5 -1 1])
x=[-6:.01:6];
y=abs(x.^3-100);
subplot(1,2,2)
plot(x,y),xlabel('x2'),ylabel('y2'),axis([-6 6 0 350])
Puff! The magic dragon,
Live by the sea.
0 1 2 3 4 5
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
x1
y
1
-5 0 5
0
50
100
150
200
250
300
350
x2
y
2
Puff! The magic dragon,
Live by the sea.
overlay plot
P.253
A ~ m*n
plot(A) % n(A)
plot(x,A) % Ax
plot(A,x) % xA(m)
plot(A,B) % BA

Puff! The magic dragon,
Live by the sea.
a=[1 2 3;6 5 4;1 5 3]

a =

1 2 3
6 5 4
1 5 3

?plot(a)
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
1
1.5
2
2.5
3
3.5
4
4.5
5
5.5
6
plot(A) % n(A)
Puff! The magic dragon,
Live by the sea.
x =

10 20 30 40

?a

a =

1 2 3
6 5 4
1 5 3
8 8 8

?plot(x,a)
10 15 20 25 30 35 40
1
2
3
4
5
6
7
8
plot(x,A) % Ax
Puff! The magic dragon,
Live by the sea.
x =

10 20 30 40

?a

a =

1 2 3
6 5 4
1 5 3
8 8 8

?plot(a,x)
1 2 3 4 5 6 7 8
10
15
20
25
30
35
40
plot(A,x) % xA(m)
Puff! The magic dragon,
Live by the sea.
a =

1 2 3
6 5 4
1 5 3
8 8 8

?b

b =

1 2 3
2 4 6
3 6 9
4 8 12

?plot(a,b)
1 2 3 4 5 6 7 8
0
2
4
6
8
10
12
Puff! The magic dragon,
Live by the sea.
Data markers and line types
Puff! The magic dragon,
Live by the sea.
Data markers and line types
plot(x,y,o)
plot(x,y,x,y,o)

Puff! The magic dragon,
Live by the sea.
plot(x,y,o)
plot(x,y,x,y,o)
Puff! The magic dragon,
Live by the sea.
plot(x,y, *,x,y,:)
Puff! The magic dragon,
Live by the sea.
Labeling curves and data
x=[0:.01:2];
y=sinh(x);
z=tanh(x);
plot(x,y,x,z,'--'),xlabel('x'), ...
ylabel('Hyperbolic Sine and Tangent'), ...
legend('sinh(x)','tanh(x)')
Puff! The magic dragon,
Live by the sea.
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0
0.5
1
1.5
2
2.5
3
3.5
4
x
H
y
p
e
r
b
o
l
i
c

S
i
n
e

a
n
d

T
a
n
g
e
n
t
sinh(x)
tanh(x)
Puff! The magic dragon,
Live by the sea.
Legend
plot

(grid line)legend

axes(legend(string1,string2)) %

refresh %
Puff! The magic dragon,
Live by the sea.
x=[0:.01:1];
y=tan(x);
z=sec(x);
plot(x,y,x,z),xlabel('x'), ...
ylabel('Tangent and Secant'),gtext('tan(x)'), ...
text(.3,1.2,'sec(x)')
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
x
T
a
n
g
e
n
t

a
n
d

S
e
c
a
n
t
tan(x)
sec(x)
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
x=[-30:.01:30]; %x=tan(x)
y=tan(x);
z=6*x;

plot(x,y,x,z),xlabel('x'), ...
ylabel('6x=tan(x) '),
axis([-5 5 -100 100])
[x,y]=ginput(5)
Puff! The magic dragon,
Live by the sea.
Graphical solution of equations

Example 5.2-1 load line analysis of a circuit
Puff! The magic dragon,
Live by the sea.
30
15
30
1
) 2 ( 0
) 1 ( ) 1 ( * 16 . 0 current load
30 load
15 y powersuppl
2
1
1
2
1
1
2 1 1 1
12 . 0
1
1
1
2
+ = + =
=
=
=
=
v
R
v
v
R
i
v R i v
e i
R
V v
v
Puff! The magic dragon,
Live by the sea.
0 2 4 6 8 10 12 14 16 18 20
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
v
2
(Volts)
i
1
(
A
m
p
e
r
e
)
load line
current
v_2=[0:.01:20];
i_11=.16*(exp(.12*v_2)-1);
i_12=-1/30*v_2+.5;
plot(v_2,i_11,v_2,i_12), grid,xlabel('v_2(Volts)'),...
ylabel('i_1(Ampere)'),axis([0 20 0 1]),...
gtext('load line'),gtext('current')
30
15
30
1
) 2 ( 0
) 1 ( ) 1 ( * 16 . 0 current load
30 load
15 y powersuppl
2
1
1
2
1
1
2 1 1 1
12 . 0
1
1
1
2
+ = + =
=
=
=
=
v
R
v
v
R
i
v R i v
e i
R
V v
v
Puff! The magic dragon,
Live by the sea.
The answer:
i1=0.25 Ampere,
v2=7.5V

Puff! The magic dragon,
Live by the sea.
axis([7 8 .2 .3])% room-in view

7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8
0.2
0.21
0.22
0.23
0.24
0.25
0.26
0.27
0.28
0.29
v
2
(Volts)
i
1
(
A
m
p
e
r
e
)
load line
current
Puff! The magic dragon,
Live by the sea.
The hold command
2 plot commands
p.258-259
x=[-1:.01:1];
y1=3+exp(-x).*sin(6*x);
y2=4+exp(-x).*cos(6*x);
plot((.1+.9i).^[0:.01:10]),hold,plot(y1,y2),...
gtext('y2 VS y1'),gtext('Imag(z) VS Real(z)')

gtextplot,
Puff! The magic dragon,
Live by the sea.
-1 0 1 2 3 4 5 6
-1
0
1
2
3
4
5
6
7
y2 VS y1
Imag(z) VS Real(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.
Annotating plots
Using TEX
Puff! The magic dragon,
Live by the sea.
Note for improving plots
p.262-263
Puff! The magic dragon,
Live by the sea.
Special plot types
Logarithmic plots
Frequency response plots and filter circuits
Controlling tick-mark spacing and labels
Stem, stairs, and bar plots
Separate Y axes
Polar plots
Puff! The magic dragon,
Live by the sea.
2 2 2
2 2 2
1 . 0 ) 1 (
02 . 0 ) 01 . 0 1 ( 100
x x
x x
y
+
+
=
Puff! The magic dragon,
Live by the sea.
Logarithmic plots
2 2 2
2 2 2
1 . 0 ) 1 (
02 . 0 ) 01 . 0 1 ( 100
x x
x x
y
+
+
=
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Note for using logarithmic scale
P.264

loglog(x,y)
semilogx(x,y)
semilogy(x,y)
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
1
1
v.s. / plot . . .
) sin( sin
+
=
=
+ = =
RCs A
A
A A f r f
t A v t A v
i
o
i o
o o i i
e
| e e
RC=0.1;
s=[1:100]*i;
M=abs(1./(RC*s+1));
loglog(imag(s),M),grid,xlabel('frequency(rad/sec)'),...
ylabel('output/input'),...
title('lowpass RC circuit frequency response(RC=0.1sec)')
Puff! The magic dragon,
Live by the sea.
10
0
10
1
10
2
10
-1
10
0
frequency(rad/sec)
o
u
t
p
u
t
/
i
n
p
u
t
lowpass RC circuit frequency response(RC=0.1sec)
Puff! The magic dragon,
Live by the sea.
Puff! The magic dragon,
Live by the sea.
Controlling tick-mark spacing and
labels
help set
help axes

set(gca,'XTick',[xmin:dx:xmax],'YTick',[ymin:
dy:ymax])

Puff! The magic dragon,
Live by the sea.
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x
y
x=[0:.01:2];
y=.25*x.^2;
plot(x,y),set(gca,'XTick',[0:.2:2],'YTick',[0:.1:1]),...
xlabel('x'),ylabel('y')
Puff! The magic dragon,
Live by the sea.
x=[1:6];
y=[13,5,7,14,10,12];
plot(x,y,'o',x,y),...
set(gca,'XTicklabel',['11'; '22'; '33'; '44'; '55'; '66']),...
set(gca,'XTick',[1:6]),axis([1 6 0 15]),xlabel('
'),...

ylabel('1997qwyteuwdrjhewfhwekjhfwje2343543
5')
Puff! The magic dragon,
Live by the sea.
11 22 33 44 55 66
0
5
10
15

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.

You might also like