1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Advanced Mathematics and Mechanics Applications Using MATLAB phần 3 pptx

61 400 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 61
Dung lượng 6,33 MB

Nội dung

2: % 3: % [area,leng,X,Y,closed]=curvprop(x,y,doplot) 4: % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5: % This function passes a cubic spline curve through 6: % a set of data values and computes the enclosed 7: % area, the curve length, and a set of points on 8: % the curve. 9: % 10: % x,y - data vectors defining the curve. 11: % doplot - plot the curve if a value is input for 12: % doplot. Otherwise, no plot is made. 13: % area - the enclosed area is computed. This 14: % parameter is valid only if the curve 15: % is closed and the boundary is traversed 16: % in counterclockwise. For a curve, the 17: % area agrees with a curve closed using 18: % a line from the last point to the 19: % origin, and a line from the origin to 20: % the first point. 21: % leng - length of the curve 22: % X,Y - set of points on the curve. The output 23: % intersperses three points on each segment 24: % between the starting data values. 25: % closed - equals one for a closed curve. Equals zero 26: % for an open curve. 27: % 28: 29: % For default test data, choose an ellipse with 30: % semi-diameters of 2 and 1. 31: if nargin==0 32: m=21; th=linspace(0,2*pi,m); 33: x=2*cos(th); y=sin(th); x(m)=2; y(m)=0; 34: end 35: 36: % Use complex data coordinates 37: z=x(:)+i*y(:); n=length(z); t=(1:n)’; 38: chord=sum(abs(diff(z))); d=abs(z(n)-z(1)); 39: 40: % Use a periodic spline if the curve is closed 41: if d < (chord/1e6) 42: closed=1; z(n)=z(1); endc=5; 43: zp=spterp(t,z,1,t,endc); 44: 45: % Use the not-a-knot end condition for open curve 46: else © 2003 by CRC Press LLC 47: closed=0; endc=1; zp=spterp(t,z,1,t,endc); 48: end 49: 50: % Compute length and area 51: % plot(abs(zp)),shg,pause 52: leng=spterp(t,abs(zp),3,n,1); 53: area=spterp(t,1/2*imag(conj(z).*zp),3,n,1); 54: Z=spterp(t,z,0,1:1/4:n,endc); 55: X=real(Z); Y=imag(Z); 56: if nargin>2 57: plot(X,Y,’-’,x,y,’.’), axis equal 58: xlabel(’x axis’), ylabel(’y axis’) 59: title(’SPLINE CURVE’), shg 60: end 61: 62: %============================================ 63: 64: function [v,c]=spterp(xd,yd,id,x,endv,c) 65: % 66: % [v,c]=spterp(xd,yd,id,x,endv,c) 67: % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 68: % 69: % This function performs cubic spline interpo- 70: % lation. Values of y(x),y’(x),y’’(x) or the 71: % integral(y(t)*dt, xd(1) x) are obtained. 72: % Five types of end conditions are provided. 73: % 74: % xd, yd - data vectors with xd arranged in 75: % ascending order. 76: % id - id equals 0,1,2,3 to compute y(x), 77: % y’(x), integral(y(t)*dt,t=xd(1) x), 78: % respectively. 79: % v - values of the function, first deriva- 80: % tive, second derivative, or integral 81: % from xd(1) to x 82: % c - the coefficients defining the spline 83: % curve. If these values are input from 84: % an earlier computation, then they 85: % are not recomputed. 86: % endv - vector giving the end conditions in 87: % one of the following five forms: 88: % endv=1 or endv omitted makes 89: % c(2) and c(n-1) zero 90: % endv=[2,left_end_slope, 91: % right_end_slope] to impose slope © 2003 by CRC Press LLC 92: % values at each end 93: % endv=[3,left_end_slope] imposes the 94: % left end slope value and makes 95: % c(n-1) zero 96: % endv=[4,right_end_slope] imposes the 97: % right end slope value and makes 98: % c(2) zero 99: % endv=5 defines a periodic spline by 100: % making y,y’,y" match at both ends 101: 102: if nargin<5 | isempty(endv), endv=1; end 103: n=length(xd); sx=size(x); x=x(:); X=x-xd(1); 104: 105: if nargin<6, c=spcof(xd,yd,endv); end 106: 107: C=c(1:n); s1=c(n+1); m1=c(n+2); X=x-xd(1); 108: 109: if id==0 % y(x) 110: v=yd(1)+s1*X+m1/2*X.*X+ 111: powermat(x,xd,3)*C/6; 112: elseif id==1 % y’(x) 113: v=s1+m1*X+powermat(x,xd,2)*C/2; 114: elseif id==2 % y’’(x) 115: v=m1+powermat(x,xd,1)*C; 116: else % integral(y(t)*dt, t=xd(1) x) 117: v=yd(1)*X+s1/2*X.*X+m1/6*X.^3+ 118: powermat(x,xd,4)*C/24; 119: end 120: v=reshape(v,sx); 121: 122: %============================================== 123: 124: function c=spcof(x,y,endv) 125: % 126: % c=spcof(x,y,endv) 127: % ~~~~~~~~~~~~~~~~ 128: % This function determines spline interpolation 129: % coefficients consisting of the support 130: % reactions concatenated with y’ and y’’ at 131: % the left end. 132: % x,y - data vectors of interplation points. 133: % Denote n as the length of x. 134: % endv - vector of data for end conditions 135: % described in function spterp. 136: % © 2003 by CRC Press LLC 137: % c - a vector [c(1); ;c(n+2)] where the 138: % first n components are support 139: % reactions and the last two are 140: % values of y’(x(1)) and y’’(x(1)). 141: 142: if nargin<3, endv=1; end 143: x=x(:); y=y(:); n=length(x); u=x(2:n)-x(1); 144: a=zeros(n+2,n+2); a(1,1:n)=1; 145: a(2:n,:)=[powermat(x(2:n),x,3)/6,u,u.*u/2]; 146: b=zeros(n+2,1); b(2:n)=y(2:n)-y(1); 147: if endv(1)==1 % Force, force condition 148: a(n+1,2)=1; a(n+2,n-1)=1; 149: elseif endv(1)==2 % Slope, slope condition 150: b(n+1)=endv(2); a(n+1,n+1)=1; 151: b(n+2)=endv(3); a(n+2,:)= 152: [((x(n)-x’).^2)/2,1,x(n)-x(1)]; 153: elseif endv(1)==3 % Slope, force condition 154: b(n+1)=endv(2); a(n+1,n+1)=1; a(n+2,n-1)=1; 155: elseif endv(1)==4 % Force, slope condition 156: a(n+1,2)=1; b(n+2)=endv(2); 157: a(n+2,:)=[((x(n)-x’).^2)/2,1,x(n)-x(1)]; 158: elseif endv(1)==5 159: a(n+1,1:n)=x(n)-x’; b(n)=0; 160: a(n+2,1:n)=1/2*(x(n)-x’).^2; 161: a(n+2,n+2)=x(n)-x(1); 162: else 163: error( 164: ’Invalid value of endv in function spcof’) 165: end 166: if endv(1)==1 & n<4, c=pinv(a)*b; 167: else, c=a\b; end 168: 169: %============================================== 170: 171: function a=powermat(x,X,p) 172: % 173: % a=powermat(x,X,p) 174: % ~~~~~~~~~~~~~~~~ 175: % This function evaluates various powers of a 176: % matrix used in cubic spline interpolation. 177: % 178: % x,X - arbitrary vectors of length n and N 179: % a - an n by M matrix of elements such that 180: % a(i,j)=(x(i)>X(j))*abs(x(i)-X(j))^p 181: © 2003 by CRC Press LLC 182: x=x(:); n=length(x); X=X(:)’; N=length(X); 183: a=x(:,ones(1,N))-X(ones(n,1),:); a=a.*(a>0); 184: switch p, case 0, a=sign(a); case 1, return; 185: case 2, a=a.*a; case 3; a=a.*a.*a; 186: case 4, a=a.*a; a=a.*a; otherwise, a=a.^p; end 4.2.3 Generalizing the Intrinsic Spline Function in MATLAB The intrinsic MATLAB function spline employs an auxiliary function unmk to create the piecewise polynomial deÞnitions deÞning the spline. The polynomials can be differentiated or integrated, and then functions mkpp and ppval can be used to evaluate results. We have employed the ideas from those routines to develop func- tions splineg and splincof extending the minimal spline capabilities of MATLAB. The function splincof(xd,yd,endc) computes arrays b and c usable by mkpp and pp- val. The data vector endc deÞnes the Þrst four types of end conditions discussed above. The function splineg(xd,yd,x,deriv,endc,b,c) handles the same kind of data as function spterp. Sometimes arrays b and c may have been created from a previ- ous call to splineg or spterp. Whenever these are passed through the call list, they are used by splineg without recomputation. Readers wanting more details on spline concepts should consult de Boor’s book [7]. Two examples illustrating spline interpolation are presented next. In the Þrst pro- gram called, sinetrp, a series of equally spaced points between 0 and 2π is used to approximate y =sin(x) which satisÞes y  (x)=cos(x) ,y  (x)=−sin(x) ,  x 0 y(x)dx =1−cos(x). The approximations for the function, derivatives, and the integral are evaluated using splineg. Results shown in Figure 4.1 are quite satisfactory, except for points outside the data interval [0, 2π]. © 2003 by CRC Press LLC −0.5 0 0.5 1 1.5 2 2.5 −1.5 −1 −0.5 0 0.5 1 1.5 2 Spline Differentiation and Integration of sin(x) x / pi function values y=sin(x) data y’(x) y’’(x) ∫ y(x) dx Figure 4.1: Spline Differentiation and Integration of sin(x) © 2003 by CRC Press LLC Example: Spline Interpolation Applied to Sin(x) Program sinetrp 1: function sinetrp 2: % Example: sinetrp 3: % ~~~~~~~~~~~~~~~~~ 4: % This example illustrates cubic spline 5: % approximation of sin(x), its first two 6: % derivatives, and its integral. 7: % 8: % User m functions required: 9: % splineg, splincof 10: 11: % Create data points on the spline curve 12: xd=linspace(0,2*pi,21); yd=sin(xd); 13: 14: % Evaluate function values at a dense 15: % set of points 16: x=linspace(-pi/2,5/2*pi,61); 17: [y,b,c]=splineg(xd,yd,x,0); 18: yp=splineg(xd,yd,x,1,[],b,c); 19: ypp=splineg(xd,yd,x,2,[],b,c); 20: yint=splineg(xd,yd,x,3,[],b,c); 21: 22: % Plot results 23: z=x/pi; zd=xd/pi; 24: plot(z,y,’k-’,zd,yd,’ko’,z,yp,’k:’, 25: z,ypp,’k ’,z,yint,’k+’); 26: title([’Spline Differentiation and ’, 27: ’Integration of sin(x)’]); 28: xlabel(’x / pi’); ylabel(’function values’); 29: legend(’y=sin(x)’,’data’,’y’’(x)’,’y’’’’(x)’, 30: ’\int y(x) dx’,1); grid on 31: figure(gcf); pause; 32: % print -deps sinetrp 33: 34: %============================================== 35: 36: function [val,b,c]=splineg(xd,yd,x,deriv,endc,b,c) 37: % 38: % [val,b,c]=splineg(xd,yd,x,deriv,endc,b,c) 39: % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 40: % © 2003 by CRC Press LLC 41: % For a cubic spline curve through data points 42: % xd,yd, this function evaluates y(x), y’(x), 43: % y’’(x), or integral(y(x)*dx, xd(1) to x(j) ) 44: % for j=1:length(x).The coefficients needed to 45: % evaluate the spline are also computed. 46: % 47: % xd,yd - data vectors defining the cubic 48: % spline curve 49: % x - vector of points where curve 50: % properties are computed. 51: % deriv - denoting the spline curve as y(x), 52: % deriv=0 gives a vector for y(x) 53: % deriv=1 gives a vector for y’(x) 54: % deriv=2 gives a vector for y’’(x) 55: % deriv=3 gives a vector of values 56: % for integral(y(z)*dz) from xd(1) 57: % to x(j) for j=1:length(x) 58: % endc - endc=1 makes y’’’(x) continuous at 59: % xd(2) and xd(end-1). 60: % endc=[2,left_slope,right_slope] 61: % imposes slope values at both ends. 62: % endc=[3,left_slope] imposes the left 63: % end slope and makes the discontinuity 64: % of y’’’ at xd(end-1) small. 65: % endc=[4,right_slope] imposes the right 66: % end slope and makes the discontinuity 67: % of y’’’ at xd(2) small. 68: % b,c coefficients needed to perform the 69: % spline interpolation. If these are not 70: % given, function unmkpp is called to 71: % generate them. 72: % val values y(x),y’(x),y’’(x) or 73: % integral(y(z)dz, z=xd(1) x) for 74: % deriv=0,1,2, or 3, respectively. 75: 76: if nargin<5 | isempty(endc), endc=1; end 77: if nargin<7, [b,c]=splincof(xd,yd,endc); end 78: n=length(xd); [N,M]=size(c); 79: 80: switch deriv 81: 82: case 0 % Function value 83: val=ppval(mkpp(b,c),x); 84: 85: case 1 % First derivative © 2003 by CRC Press LLC 86: C=[3*c(:,1),2*c(:,2),c(:,3)]; 87: val=ppval(mkpp(b,C),x); 88: 89: case 2 % Second derivative 90: C=[6*c(:,1),2*c(:,2)]; 91: val=ppval(mkpp(b,C),x); 92: 93: case 3 % Integral values from xd(1) to x 94: k=M:-1:1; 95: C=[c./k(ones(N,1),:),zeros(N,1)]; 96: dx=xd(2:n)-xd(1:n-1); s=zeros(n-2,1); 97: for j=1:n-2, s(j)=polyval(C(j,:),dx(j)); end 98: C(:,5)=[0;cumsum(s)]; val=ppval(mkpp(b,C),x); 99: 100: end 101: 102: %============================================== 103: 104: function [b,c]=splincof(xd,yd,endc) 105: % 106: % [b,c]=splincof(xd,yd,endc) 107: % ~~~~~~~~~~~~~~~~~~~~~~~~~~ 108: % This function determines coefficients for 109: % cubic spline interpolation allowing four 110: % different types of end conditions. 111: % xd,yd - data vectors for the interpolation 112: % endc - endc=1 makes y’’’(x) continuous at 113: % xd(2) and xd(end-1). 114: % endc=[2,left_slope,right_slope] 115: % imposes slope values at both ends. 116: % endc=[3,left_slope] imposes the left 117: % end slope and makes the discontinuity 118: % of y’’’ at xd(end-1) small. 119: % endc=[4,right_slope] imposes the right 120: % end slope and makes the discontinuity 121: % of y’’’ at xd(2) small. 122: % 123: if nargin<3, endc=1; end; 124: type=endc(1); xd=xd(:); yd=yd(:); 125: 126: switch type 127: 128: case 1 129: % y’’’(x) continuous at the xd(2) and xd(end-1) 130: [b,c]=unmkpp(spline(xd,yd)); © 2003 by CRC Press LLC 131: 132: case 2 133: % Slope given at both ends 134: [b,c]=unmkpp(spline(xd,[endc(2);yd;endc(3)])); 135: 136: case 3 137: % Slope at left end given. Compute right end 138: % slope. 139: [b,c]=unmkpp(spline(xd,yd)); 140: c=[3*c(:,1),2*c(:,2),c(:,3)]; 141: sright=ppval(mkpp(b,c),xd(end)); 142: [b,c]=unmkpp(spline(xd,[endc(2);yd;sright])); 143: 144: case 4 145: % Slope at right end known. Compute left end 146: % slope. 147: [b,c]=unmkpp(spline(xd,yd)); 148: c=[3*c(:,1),2*c(:,2),c(:,3)]; 149: sleft=ppval(mkpp(b,c),xd(1)); 150: [b,c]=unmkpp(spline(xd,[sleft;yd;endc(2)])); 151: 152: end 4.2.4 Example: A Spline Curve with Several Parts and Corners The Þnal spline example illustrates interpolation of a two-dimensional curve where y cannot be expressed as a single valued function of x. Then we introduce a param- eter t j having its value equal to the index  for each (x j ,y j ) used. Interpolating x(t) and y(t) as continuous functions of t produces a smooth curve through the data. Function matlbdat creates data points to deÞne the curve and calls function spcry2d to compute points on a general plane curve. We also introduce the idea of ‘corner points’ where slope discontinuity allows the curve to make sharp turns needed to describe letters such as the ‘t’ in MATLAB. Each curve segment between successive pairs of corner points is parameterized using function spline. Results in Figure 4.2 show clearly that spline interpolation can represent a complicated curve. The re- lated code appears after the Þgure. The same kind of parameterization used for two dimensions also works well for three dimensional curves. Example: Spline Curve Drawing the Word MATLAB Program matlbdat 1: function matlbdat © 2003 by CRC Press LLC [...]... Word MATLAB Figure 4.2: Spline Curve Drawing the Word MATLAB © 20 03 by CRC Press LLC 2: 3: 4: 5: 6: 7: % % % % % % Example: matlbdat ~~~~~~~~~~~~~~~~~ This example illustrates the use of splines to draw the word MATLAB User m functions required: spcurv2d 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: x=[ 13 17 17 16 17 19 21 22 21 21 23 26 25 28 30 32 37 32 30 32 35 37 37 38 41... 30 32 35 37 37 38 41 42 42 42 45 39 42 42 44 47 48 48 47 47 48 51 53 57 53 52 53 56 57 57 58 61 63 62 61 64 66 64 61 64 67 67]; y=[ 63 64 58 52 57 62 62 58 51 58 63 63 53 52 56 61 61 61 56 51 55 61 55 52 54 59 63 59 59 59 59 54 52 54 58 62 58 53 51 55 60 61 60 54 51 55 61 55 52 53 58 62 53 57 53 51 53 51 51 51]; x=x’; x=x(:); y=y’; y=y(:); ncrnr=[17 22 26 27 28 29 30 31 36 42 47 52]; clf; [xs,ys]=curv2d(x,y,10,ncrnr);... compute the base points and weight factors © 20 03 by CRC Press LLC Function for Composite Gauss Integration 1: 2: 3: 4: 5: function [val,bp,wf]=gcquad(func,xlow, xhigh,nquad,mparts,varargin) % % [val,bp,wf]=gcquad(func,xlow, % xhigh,nquad,mparts,varargin) 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : 39 : 40: 41: % % % %... 9.5628e-0 03 -9.9994e-001 1.0000e+002 -6.2513e-0 03 9.5628e-0 03 2.9858e+001 1.0000e+002 8.8818e-014 9.5628e-0 03 2.0263e+002 1.0000e+002 -4.1078e-0 13 9.5628e-0 03 1.6475e-001 1.0000e+002 -1.5543e-0 13 9.5628e-0 03 (Total time using quadl)/(Total time using gcquad) equals 3. 539 5 >> Program Comparing Numerical Integration Methods 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: function [L,G,names]=quadtest(secs)... an explanation) > ’,’s’); k=str2num(K); if strcmp(K,’’) | strcmp(K,’0’); disp(’ ’),return © 20 03 by CRC Press LLC 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : 39 : 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: elseif strcmp(K,’?’) disp(’ ’), disp( ’Let f(x) have its k’’th derivative denoted by’) disp( ’F(k,x) The finite difference... matrix with rows % describing the functions © 20 03 by CRC Press LLC 19: 20: 21: 22: % which were integrated % % User functions called: ftest, gcquad % - 23: 24: global nvals 25: 26: if nargin==0, secs=60; end 27: 28: 29: fprintf(’\nPRESS RETURN TO BEGIN COMPUTATION > ’) pause 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : 38 : % Summary of the five integrands used names=strvcat(’sqrt(x)’,’log(x)’,’humps(x)’,... Let us write f (x) = c1 + c2 x + c3 x2 + c4 x3 © 20 03 by CRC Press LLC Then 1 2 f (x)dx = 2c1 + c3 3 −1 Evidently the linear and cubic terms do not inßuence the integral value Also, c 1 = f (0) and f (−1) + f (1) = 2 c1 + 2 c3 so that 1 f (x)dx = −1 1 [f (−1) + 4f (0) + f (1)] + E 3 The error E in this formula is zero when the integrand is any polynomial of order 3 or lower Expressed in terms of more... an analytically deÞned function or a function deÞned by spline interpolating through discrete data Function for Composite Simpson Rule 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30 : 31 : 32 : 33 : 34 : 35 : function area=simpson(funcname,a,b,n,varargin) % % area=simpson(funcname,a,b,n,varargin) % % Simpson’s rule integration... 29 30 31 36 42 47 52]; clf; [xs,ys]=curv2d(x,y,10,ncrnr); plot(xs,ys,’k-’,x,y,’k*’), axis off; title(’A Spline Curve Drawing the Word MATLAB ); figure(gcf); % print -deps matlbdat 26: 27: %============================================= 28: 29: 30 : 31 : 32 : 33 : 34 : 35 : 36 : 37 : function [X,Y]=spcrv2d(xd,yd,nseg,icrnr) % % [X,Y]=spcrv2d(xd,yd,nseg,icrnr) % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ % This function computes... 1, 2, , nd © 20 03 by CRC Press LLC HALF ANNULUS ABOVE A SQUARE WITH A HOLE 15 8 14 16 17 7 21 22 24 20 18 6 13 23 19 25 1 2 5 12 26 11 y axis 8 4 3 4 7 3 2 5 6 1 10 9 −1 0 1 2 3 x axis 4 5 6 7 Figure 5 .3: Geometry Showing Numbered Boundary Points The boundary curve and its derivatives are piecewise polynomial functions Exact results for the geometrical properties are obtained by using the function . 30 32 37 32 30 32 35 37 37 38 11: 41 42 42 42 45 39 42 42 44 47 48 48 12: 47 47 48 51 53 57 53 52 53 56 57 57 13: 58 61 63 62 61 64 66 64 61 64 67 67]; 14: y=[ 63 64 58 52 57 62 62 58 51 58 63. xd(2) and xd(end-1) 130 : [b,c]=unmkpp(spline(xd,yd)); © 20 03 by CRC Press LLC 131 : 132 : case 2 133 : % Slope given at both ends 134 : [b,c]=unmkpp(spline(xd,[endc(2);yd;endc (3) ])); 135 : 136 : case 3 137 :. ellipse with 30 : % semi-diameters of 2 and 1. 31 : if nargin==0 32 : m=21; th=linspace(0,2*pi,m); 33 : x=2*cos(th); y=sin(th); x(m)=2; y(m)=0; 34 : end 35 : 36 : % Use complex data coordinates 37 : z=x(:)+i*y(:);

Ngày đăng: 08/08/2014, 11:21

TỪ KHÓA LIÊN QUAN