% Problem 2 % J. Watlington, 11/19/95 % Define the x vector x = -10:10; [m,num_samples] = size( x ); % Define the y vector y = testfun( x ); % Solve for polynomials of 5, 10, and 15 terms p5 = pfit( x, y, 4 ) p10 = pfit( x, y, 9 ) p15 = pfit( x, y, 14 ) % Solve for radial basis functions of 5, 10, and 15 c5 = linspace( x(1), x(num_samples), 5 ); % pick centers (evenly dist.) c10 = linspace( x(1), x(num_samples), 10 ); c15 = linspace( x(1), x(num_samples), 15 ); r5 = rfit( x, y, c5 ) r10 = rfit( x, y, c10 ) r15 = rfit( x, y, c15 ) % Now calculate the error for the sample set y_p5 = psolve( p5, x ); y_p10 = psolve( p10, x ); y_p15 = psolve( p15, x ); y_r5 = rsolve( r5, c5, x ); y_r10 = rsolve( r10, c10, x ); y_r15 = rsolve( r15, c15, x ); is_error = [ x' (y_p5-y)' (y_p10-y)' (y_p15-y)' (y_r5-y)' (y_r10-y)' (y_r15-y)' ] % Now calculate the error for the out-of-sample set oos = -10.5:10.5; y_oos = testfun( oos ); oos_p5 = psolve( p5, oos ); oos_p10 = psolve( p10, oos ); oos_p15 = psolve( p15, oos ); oos_r5 = rsolve( r5, c5, oos ); oos_r10 = rsolve( r10, c10, oos ); oos_r15 = rsolve( r15, c15, oos ); oos_error = [ oos' (oos_p5-y_oos)' (oos_p10-y_oos)' (oos_p15-y_oos)' (oos_r5-y_oos)' (oos_r10-y_oos)' (oos_r15-y_oos)' ] % And now for some matlab fun test = -10:0.05:10; test_p = psolve( p15, test ); test_r = rsolve( r15, c15, test ); test_y = testfun( test ); plot( test, test_p, '--', test, test_r, '.', test, test_y, '-' );