% % Examples 9.15 and 9.17 % % epage 432 % % Written by: % -- % John L. Weatherwax 2008-02-20 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- clear all; close all; clc; addpath('../../Code/CSTool'); randn('seed',0); x = linspace( 0, +1, 100 ); % our spatially dependent variance: var = ones(size(x)); % <- a constant variance of ONE % generate noise with this variance: noise_eps = zeros(1,length(x)); for ii=1:length(x), noise_eps(ii) = normrnd(0,sqrt(var(ii))); end % our dependent function "y": y_truth = 4*x.^3 + 6*x.^2 - 1; y = y_truth + noise_eps; % plot the original data: fd=figure; ht = plot( x, y_truth, '-og' ); hold on; grid on; ho = plot( x, y, 'xr' ); fr=figure; hold on; line_styles={'-',':','--'}; line_colors='cmk'; data_plt_h = []; res_plt_h = []; for ii=1:3, % assume we *know* the dimension of the polynomial to fit: [pcoef,s] = polyfit(x,y,ii); % evaluate this polynomial model: yp = polyval(pcoef,x); % plot this polynomial model: figure(fd); tph=plot( x, yp, ['-',line_colors(ii)] ); data_plt_h(end+1) = tph; % plot the residuals v.s. the fitted value yp: res = y-yp; figure(fr); tph=plot( x, res, [line_styles{ii},line_colors(ii)] ); xlabel( 'predictor x' ); ylabel( 'residual' ); title( ['residual dependence plot for a polynomial model of degree', num2str(ii)] ); axis( [0 1 -2.5 2.5] ); res_plt_h(end+1) = tph; end figure(fd); legend(data_plt_h, {'poly deg 1','poly deg 2','poly deg 3'}, 'location', 'north' ); saveas( gcf, '../../WriteUp/Graphics/Chapter10/prob_10_2_truth_n_data', 'epsc' ); figure(fr); legend(res_plt_h, {'poly deg 1','poly deg 2','poly deg 3'}, 'location', 'north' ); saveas( gcf, '../../WriteUp/Graphics/Chapter10/prob_10_2_res_plts', 'epsc' ); clear functions;