hw3 solution new

Upload: aryan

Post on 14-Jan-2016

262 views

Category:

Documents


3 download

DESCRIPTION

matlab solution HW

TRANSCRIPT

150493145

94

3.5 (b)

. MATLAB . %Prob3_5.m to get the Pade approximation for f(x) = e^(-x)f1 = inline('exp(-x)','x');M = 1; N = 1; %the degrees of Numerator Q(x) and Denominator D(x)xo = 0; %the center of Taylor series expansion[n,d] = padeap(f1,xo,M,N) %to get the coefficients of Q(x)/P(x)x0 = -1; xf = 2; %left/right boundary of the intervalpadeap(f1,xo,M,N,x0,xf) %to see the graphic results : n = -1.0001 2.0000 d = 1.0000 2.0000

:

. Pade ( ) Pade .

3.8 MATLAB spline interp1 .

[0,4] . :clear;clc;x=[0 0.5 2 3.5 4];y=[0 2 -2 2 0]; xi=[0:1:100]*((4-0)/100); yi1=spline(x,y,xi);yi2=interp1(x,y,xi,'spline'); plot(x,y,'r*',xi,yi1,'b--',xi,yi2,'k');

( 1 ).

1 cspline(x,y,KC) ( 3.5 ) KC=1,2,3 MATLAB . . help x y Not-a-Knot end condition . .

3.9

% r o b o t _ p a t h clear ;clc;x1 = [ 0 1 2 ] ; y1 = [ 0 1 4 ] ; t1 = [ 0 1 2 ] ;ti1 = [ 0 : 0.05 : 2 ] ;xi1 = cspline ( t1 , [0 x1 0] , ti1 ) ; yi1 = cspline ( t1 , [ 0 y1 0], ti1 ) ; x2 = [ 2 3 4 ] ; y2 = [ 4 3 2 ] ; t2 = [ 2 3 4 ] ;ti2 = [ 2 : 0.05 : 4 ] ;xi2 = cspline ( t2 ,[0 x2 0] , ti2 ) ; yi2 = cspline ( t2 , [0 y2 0] , ti2 ) ; x3 = [ 4 2 0 ] ; y3 = [ 2 1 0 ] ; t3 = [ 4 5 6 ] ;ti3 = [ 4 : 0.05 : 6 ] ;xi3 = cspline ( t3 ,[0 x3 0] , ti3 ) ; yi3 = cspline ( t3 ,[0 y3 0] , ti3 ) ; xx=[xi1';xi2';xi3'];yy=[yi1';yi2';yi3'];tt=[ti1';ti2';ti3']; plot(tt,xx); figure(2)plot(tt,yy) figure(3)plot(xx,yy);

x-t y-t x-y .

3.11 (a) curve_fit . curve_fit :function [ th , err , yi ] = curve_fit ( x , y , KC , C , xi , sig )% i m p l e m e n t s t h e v a r i o u s L S c u r v e - f i t t i n g s c h e m e s i n T a b l e 3 . 5% K C = t h e # o f s c h e m e i n T a b l e 3 . 5% C = o p t i o n a l c o n s t a n t ( f i n a l v a l u e ) f o r K C! = 0 (nonlinear LS)% degree of approximate polynomial for KC = 0 (standard LS)% sig = the inverse of weighting factor for WLSNx = length(x); x = x(:) ; y = y(:);if nargin == 6, sig = sig(:);elseif length(xi) == Nx, sig = xi(:); xi = x;else sig = ones(Nx,1);endif nargin < 5, xi = x; end; if nargin < 4 | C < 1, C = 1; endswitch KC case 1 A(1:Nx,:) = [(1./x)./sig ones(Nx,1)./sig]; RHS = y./sig; th = A\RHS; yi = th(1)*(1./xi) + th(2); y2 = th(1)*(1./x) + th(2); case 2 A(1:Nx,:) = [x./sig ones(Nx,1)./sig]; RHS = (1./y)./sig; th = A\RHS; yi = 1./(th(1)*(x) + th(2)); y2 = 1./(th(1)*(x) + th(2)); th=[th(2)/th(1) 1/th(1)]; case {3,4} A(1:Nx,:) = [x./sig ones(Nx,1)./sig]; RHS = log(y)./sig; th = A\RHS; yi = exp(th(1)*xi + th(2)); y2 = exp(th(1)*x + th(2)); if KC == 3, th = exp([th(2) th(1)]); else th(2) = exp(th(2)); end case 5 if nargin < 5, C = max(y) + 1; end %final value A(1:Nx,:) = [x./sig ones(Nx,1)./sig]; y1 = y; y1(find( y > C - 0.01) ) = C - 0.01; RHS = log(C-y1)./sig; th = A\RHS; yi = C - exp(th(1)*xi + th(2)); y2 = C - exp(th(1)*x + th(2)); th = [-th(1) exp(th(2))]; case 6 A(1:Nx,:) = [log(x)./sig ones(Nx,1)./sig]; y1 = y; y1(find(y < 0.01)) = 0.01; RHS = log(y1)./sig; th = A\RHS; yi = exp(th(1)*log(xi) + th(2)); y2 = exp(th(1)*log(x) + th(2)); th = [exp(th(2)) th(1)]; case 7 A(1:Nx,:) = [x./sig ones(Nx,1)./sig]; y1 = y; y1(find(y < 0.01)) = 0.01; RHS = log((y1)./(x))./sig; th = A\RHS; yi = exp(th(1)*(xi) + th(2)).*(xi); y2 = exp(th(1)*(x) + th(2)).*x; th = [exp(th(2)) th(1)]; case 8 if nargin < 5, C = y(end); end %final value A(1:Nx,:) = [x./sig ones(Nx,1)./sig]; RHS = log(C./y-1)./sig; th = A\RHS; yi = C./(exp(th(1)*xi+th(2))+1); y2 = C./(exp(th(1)*x+th(2))+1); th=[th(1) exp(th(2))]; case 9 A(1:Nx,:) = [log(x)./sig ones(Nx,1)./sig]; RHS = y./sig; th = A\RHS; yi = th(1)*log(xi) + th(2); y2 = th(1)*log(x) + th(2); otherwise %standard LS with degree C A(1:Nx,C + 1) = ones(Nx,1)./sig; for n = C:-1:1, A(1:Nx,n) = A(1:Nx,n + 1).*x; end RHS = y./sig; th = A\RHS; yi = th(C+1); tmp = ones(size(xi)); y2 = th(C+1); tmp2 = ones(size(x)); for n = C:-1:1, tmp = tmp.*xi; yi = yi + th(n)*tmp; tmp2 = tmp2.*x; y2 = y2 + th(n)*tmp2; endendth = th(:)'; err = norm(y - y2);if nargout == 0, plot(x,y,'*', xi,yi,'k-'); end

(b) nm3p11.m Figure P3.11 . . lsqcurvefit MATLAB Table 3.5 curve_fit() . . .

3.12

(xi,yi) . .function z=find_depth(xi,yi) x=[0.1 1.2 2.5 3.6 4.8];y=[0.5 1.4 2.2 3.5 5.6]; Z=[410 390 380 420 450; 395 375 410 435 455; 365 405 430 455 470; 370 400 420 445 435; 385 395 410 395 410]; [X,Y]=meshgrid(x,y); z=interp2(X,Y,Z,xi,yi);

(2,3) Command Window .>>z=find_depth(2,3) z = 415.4142

3.15

(LS) . .%Problem 3_15clear, clfx = [1:2:20]; Nx = length(x); %changing inputxi = [1:200]/10; %interpolation pointseby=ones(size(x));eby(1:2:9) = 0.1; %error bound for each yeby(2:2:10) = 0.5;y = [3.2908 4.7323 3.3264 2.4149 1.1640 0.3814 0.3515 -0.2396 ...0.1140 -0.2615];[x,i] = sort(x) ; y = y(i); %sort the data for plotting%eby = y.*eb; %our estimation of error boundsKC = 7; [thlc,err,yl] = curve_fit(x,y,KC,0,xi);[thwlc,err,ywl] = curve_fit(x,y,KC,0,xi,eby);errorbar(x,y,eby), hold onplot(xi,yl,'k', xi,ywl,'r')

. WLS LS . WLS . . .

1

5