March 29 2016 1 Project II Optimization The project has two
March 29, 2016 1 Project II: Optimization The project has two parts 1. Animate the Matlab code Example 10.1.m. 2. Use unconstrained optimization to find and compare fitting a polynomial to a data set using least squares or by minimizing the absolute error between the data and the polynomial. 1.1 Animating code Example 10.1.m This code uses steepest descent to determine the unconstrained minimum of a scalar function of the two variables rl, r2. The program simply odtputs a listing of the values of rl, r2 and f(xl, r2) as the search progresses This code should be rewritten so that the search is animated, i.e, an animated 2D plot of the sequence of locations of zl, r2 should be displayed. 1.2 Optimation Project The Matlab code fminune.m should be used to find the best fit of a polynomial to a data set based on 1. Standard Least Squares where the error metric is and 2. The absolute error criteria where the error metric is where f(tu) is a set of sample values (data) at timmes f 1,2,, N and gu(t) an M\'th order polynomial with a coefficient vector C Co.C.CM-il which is determined from minimizing E( two minimization methods should be compared and contrasted. Your project report should compare the two cases for both finst order polynominls and second and developed in Chapter 9). To aid you 1 present below a sample code that performs both minimization methods first order polynomials below. This code is a modification of itpoly.im that we used in Chapter 9 CM I which is determined from minimizing E, (C) or EIC), The results of the d third order polynomials (see Notes2.pdf and the oodes that were a sample code that performs both minimization methods for % program fitpolynou.n % This progr fits a first order polytonial to a set of J data points The theory is described in .. note2. pdf.. % An ideal poly is computed and then white Gaussian noise added % Also note that Matlab has a polynonial titting program called polyt it..
Solution
This program should work function p = polyfitweighted2(x,y,z,n,w) % polyfitweighted2.m % ------------------- % % least-squares fit of 2D data z(x,y) with an nth order % polynomial, weighted by w(x,y) . % P = polyfitweighted2(X,Y,Z,N,W) finds the coefficients of a polynomial % P(X,Y) of degree N that fits the data Z best in a least-squares % sense. P is a row vector of length (N+1)*(N+2)/2 containing the % polynomial coefficients in ascending powers, 0th order first. % % P = [p00 p10 p01 p20 p11 p02 p30 p21 p12 p03...] % % e.g. For a 3rd order fit, % the regression problem is formulated in matrix format as: % % wZ = V*P or % % 2 2 3 2 2 3 % wZ = [w wx wy wx xy wy wx wx y wx y wy ] [p00 % p10 % p01 % p20 % p11 % p02 % p30 % p21 % p12 % p03] % % *Note:* P is not in the format of standard Matlab 1D polynomials. Use % polval2.m to evaluate the polynomial in this format, at given values of % x,y. % % X,Y must be vectors % Z,W must be 2D arrays of size [length(X) length(Y)] % % based on polyfit.m by The Mathworks Inc. - see doc polyfit for more details % % Class support for inputs X,Y,Z,W: % float: double, single x = x(:); y = y(:); lx=length(x); ly=length(y); if ~isequal(size(z),size(w),[ly lx]) error(\'polyfitweighted2:XYSizeMismatch\',... [\' X,Y *must* be vectors\' ... \' Z,W *must* be 2D arrays of size [length(X) length(Y)]\']) end y=y*ones(1,lx); x=ones(ly,1)*x\'; x = x(:); y = y(:); z = z(:); w = w(:); pts=length(z); % Construct weighted Vandermonde matrix. V=zeros(pts,(n+1)*(n+2)/2); V(:,1) = w; %V(:,1) = ones(pts,1); ordercolumn=1; for order = 1:n for ordercolumn=ordercolumn+(1:order) V(:,ordercolumn) = x.*V(:,ordercolumn-order); end ordercolumn=ordercolumn+1; V(:,ordercolumn) = y.*V(:,ordercolumn-order-1); end % Solve least squares problem. [Q,R] = qr(V,0); ws = warning(\'off\',\'all\'); p = R\\(Q\'*(w.*z)); % Same as p = V\\(w.*z); warning(ws); if size(R,2) > size(R,1) warning(\'polyfitweighted2:PolyNotUnique\', ... \'Polynomial is not unique; degree >= number of data points.\') elseif condest(R) > 1.0e10 warning(\'polyfitweighted2:RepeatedPointsOrRescale\', ... [\'Polynomial is badly conditioned. Remove repeated data points\ \' ... \' or try centering and scaling as described in HELP POLYFIT.\']) end %r = z - (V*p)./w; p = p.\'; % Polynomial coefficients are row vectors by convention.