第 5 章 繪圖及模型建構 plotting & model building

Post on 02-Jan-2016

124 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

第 5 章 繪圖及模型建構 plotting & model building. 圖比表好 表比文字好. 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 ~ 說明. - PowerPoint PPT Presentation

TRANSCRIPT

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) % 將視窗分成 m 列 n 行的陣列平面 第 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

y1

-5 0 50

50

100

150

200

250

300

350

x2

y2

Puff! The magic dragon,Live by the sea….

overlay plot 層疊圖• P.253

• A ~ m 列 *n 行• plot(A) % n 條曲線 (A 的行向量對其索引的圖 )

• plot(x,A) % A 矩陣對 x 向量作圖• plot(A,x) % x 向量對 A 矩陣作圖 (m 條曲線 )

• plot(A,B) % B 矩陣的行對 A 矩陣的行作圖

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 31

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 401

2

3

4

5

6

7

8

•plot(x,A) % A矩陣對 x向量作圖

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 810

15

20

25

30

35

40

plot(A,x) % x向量對 A矩陣作圖 (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 20

0.5

1

1.5

2

2.5

3

3.5

4

x

Hyp

erbo

lic S

ine

and

Tang

ent

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 10

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

x

Tang

ent

and

Sec

ant

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.0currentload

30load

15y powersuppl

2

1

12

11

2111

12.01

1

1

2

v

R

vv

Ri

vRiv

ei

ΩR

Vv

v

Puff! The magic dragon,Live by the sea….

0 2 4 6 8 10 12 14 16 18 200

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

v2(Volts)

i 1(Am

pere

)

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.0currentload

30load

15y powersuppl

2

1

12

11

2111

12.01

1

1

2

v

R

vv

Ri

vRiv

ei

ΩR

Vv

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 80.2

0.21

0.22

0.23

0.24

0.25

0.26

0.27

0.28

0.29

v2(Volts)

i 1(Am

pere

)

load line

current

Puff! The magic dragon,Live by the sea….

The hold command

• 2 plot commandsp.258-259x=[-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)')

gtext 最好在都 plot 完之後再執行 , 以免亂掉

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

222

222

1.0)1(

02.0)01.01(100

xx

xxy

Puff! The magic dragon,Live by the sea….

•Logarithmic plots

222

222

1.0)1(

02.0)01.01(100

xx

xxy

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

RCsA

A

AAfrf

tAvtAv

i

o

io

ooii

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

100

101

102

10-1

100

frequency(rad/sec)

outp

ut/in

put

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 20

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('1997qwyteuwdrjhewfhwekjhfwje23435435')

Puff! The magic dragon,Live by the sea….

11 22 33 44 55 660

5

10

15

月 份

1997

qwyt

euw

drjh

ewfh

wek

jhfw

je23

4354

35

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 60

10

20

1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 60

10

20x=[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

mxmx

m

bbexy

bxxy

bmxxy

)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 scalesubplot(2,1,1)plot(force,deflection,'o'),... xlabel('force(lb)'),ylabel('deflection(in)'),... axis([0 800 0 .8])%curve fitting with a linep=polyfit(force,deflection,1);k=1/p(1)%plot the fitted linef=[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 8000

0.2

0.4

0.6

0.8

force(lb)

defle

ctio

n(in

)

0 100 200 300 400 500 600 700 8000

0.2

0.4

0.6

0.8

force(lb)

defle

ctio

n(in

)

Puff! The magic dragon,Live by the sea….

Puff! The magic dragon,Live by the sea….

Heat transferpredict when will T(F) achieve 120F?

t(sec) T(F)

0 145

620 130

2266 103

3482 90

120t=?

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 datatime=[0,620,2266,3482];temp=[145,130,103,90];%substract room temp.temp=temp-68;% use linear scalesubplot(2,2,1)plot(time,temp,time,temp,'o'),...xlabel('Time(sec)'),ylabel('temperature(F)')%use exponential plotsubplot(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 fittingp=polyfit(time,log10(temp),1);m=p(1)b=10^p(2)%calculate the time needed to 120Ft_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 linesubplot(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 400020

40

60

80

Time(sec)

tem

pera

ture

(F)

0 1000 2000 3000 400010

1

102

Time(sec)

tem

pera

ture

(F)

0 1000 2000 3000 400010

1

102

time(sec)

tem

p(F

)

0 1000 2000 3000 400080

100

120

140

160

time(sec)

tem

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

volumeV

pressurep

rate flow

f

Vrf

pcf

Puff! The magic dragon,Live by the sea….

V( 杯 )

倒滿一杯需時 t(sec)

15 612 79 86 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….

101

10-1

V(cups)

1 cu

p fu

ll tim

e(se

c)

5 10 15 20 25 30 350

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

ii

n

iii

n

ii

n

ii

n

ii

n

iii

ybyxaxabynbxa

ybaxJ

1111

22

1

22

1

2

222

)(

0,0

b

J

a

J

n

ii

n

ii

n

iii

n

ii

n

ii

ynbax

yxbxax

11

111

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);endsubplot(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 100

10

20

30

40

50

0 5 100

10

20

30

40

50

0 5 100

10

20

30

40

50

0 5 100

10

20

30

40

50

J = 71.5389 56.6727 41.8838 4.6566S= 1562 1562 1562 1562r^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=bxm

p=polyfit(log10(x),log10(y),1)

p(1)=m, p(2)=log10b

y=b(10)mx

p=polyfit(x,log10(y),1)

P(1)=m, p(2)= log10b

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 goodfor %99r

fitting good,1~,1

1

][

])([

2

2

2

1

2

1

2

rJ

S

Jr

yyS

yxfJ

m

ii

m

iii

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

107777.7109209.1

101.20

2170577394277.0

07742036964239.171531517800652.0

1067777099161.710920896532.1

1009690135.21033551.6

2

3344

566

2

3344

5669

xx

xx

xxy

xx

xx

xxy

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.5838,1.35,1.0082,.718,.689,.4308,.203,.1652,-.073,-.002,-.1122,.106];[p, s]=polyfit(x1,y1,6);format longx=[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')pp2

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-xmin or x’=x-xmean

• x’=x/xmean or x’=x/xmax

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 48p = 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/Sp = 0.0087 -0.1851 1.5991 2.0362r2 = 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 datax=[0:19];% contamination datay=[6,13,23,33,54,83,118,156,210,282,...350,440,557,685,815,990,1170,1350,1575

,1830];

%linear fittingp1=polyfit(x,y,1);% quadratic fittingp2=polyfit(x,y,2);% cubic fittingp3=polyfit(x,y,3);

% exponential fittingp4=polyfit(x,log10(y),1);% residualres1=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)

m

iii

m

ii

m

iii

yxxa

aJ

yxaJ

xay

11

21

1

1

21

1

have we

0/let

)(

Puff! The magic dragon,Live by the sea….

Quadratic model (0,0)

m

iii

m

ii

m

ii

m

iii

m

ii

m

ii

m

iiii

yxxaxa

yxxaxa

aJaJ

yxaxaJ

axaxay

11

22

1

31

1

2

1

32

1

41

21

1

22

21

322

1

have we

0/0/let

)(

Puff! The magic dragon,Live by the sea….

Not pass (0,0)

001

0022

01

)(

)()(

yxxay

yxxaxxay

Puff! The magic dragon,Live by the sea….

Multiple linear regression

• y=a0+a1x1+a2x2

• Data (y, x1, x2)

n

nn

y

y

y

y

xx

xx

xx

xx

a

a

a

...

1

...

1

1

1

3

2

1

21

2313

2212

2111

2

1

0

y

Xa

yXa

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\yyp=X*a;Max_percent_error=100*max(abs((yp-y)./y))a = 0.8000 10.2429 1.2143Max_percent_error = 3.2193

y=0.8+10.2429x1+1.2143x2

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

00.5

1

-1

-0.5

0

0.5

10

10

20

30

40

x

z

tz

tey

text

t

cos

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

])[( 222 yyxxez

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

])[( 222 yyxxez

Puff! The magic dragon,Live by the sea….

Puff! The magic dragon,Live by the sea….

Puff! The magic dragon,Live by the sea….

top related