# Puff! The magic dragon, Live by the sea…. 第 5 章 繪圖及模型建構 plotting & model building.

## Presentation on theme: "Puff! The magic dragon, Live by the sea…. 第 5 章 繪圖及模型建構 plotting & model building."— 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….

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

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

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) 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) 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) 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)

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

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

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

Puff! The magic dragon, Live by the sea…. 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')

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

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)') gtext 最好在都 plot 完之後再執行, 以免亂掉

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

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

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

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

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

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

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) 0145 620130 2266103 348290 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 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….

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

Puff! The magic dragon, Live by the sea…. V( 杯 ) 倒滿一杯需 時 t(sec) 156 127 98 69

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

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 21.884 42.732 63.388 83.346 103 122.644 142.022 161.65 181.5838 201.35 221.0082 24.718 26.689 28.4308 30.203 32.1652 34-.073 36-.002 38-.1122 40.106 xy

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

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 year1990199119921993199419951996199719981999 車流量 million 2.13.44.55.36.26.66.877.47.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 year1990199119921993199419951996199719981999 車流量 million 2.13.44.55.36.26.66.877.47.8

Puff! The magic dragon, Live by the sea…. 2000 yield polyval(p,10) ans = 8.1767 million cars year1990199119921993199419951996199719981999 車流量 million 2.13.44.55.36.26.66.877.47.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)

Puff! The magic dragon, Live by the sea…. Quadratic model (0,0)

Puff! The magic dragon, Live by the sea…. Not pass (0,0)

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 )

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)

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

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

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

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

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

Download ppt "Puff! The magic dragon, Live by the sea…. 第 5 章 繪圖及模型建構 plotting & model building."

Similar presentations