Problem 5.6

Nasser Abbasi

Output

» nma_HW_5_6

 

  solve problem 5.6

  Nasser Abbasi

 

enter the x values:[1 2 3 4 5]

enter the y values:[2470 2510 2410 2350 2240]

enter the polyminal degree to fit to data [1 to 4]:1

 

a =

 

  1.0e+003 *

 

    2.5820

   -0.0620

 

fitting polynomial degree=1

a(0) = 2582

a(1) = -62

 


» nma_HW_5_6

 

 

  solve problem 5.6

  Nasser Abbasi

 

enter the x values:[1 2 3 4 5]

enter the y values:[2470 2510 2410 2350 2240]

enter the polyminal degree to fit to data [1 to 4]:2

 

a =

 

  1.0e+003 *

 

    2.4520

    0.0494

   -0.0186

 

fitting polynomial degree=2

a(0) = 2452

a(1) = 49.4286

a(2) = -18.5714

»


» nma_HW_5_6

 

 

  solve problem 5.6

  Nasser Abbasi

 

enter the x values:[1 2 3 4 5]

enter the y values:[2470 2510 2410 2350 2240]

enter the polyminal degree to fit to data [1 to 4]:3

 

a =

 

  1.0e+003 *

 

    2.3260

    0.2264

   -0.0861

    0.0075

 

fitting polynomial degree=3

a(0) = 2326

a(1) = 226.429

a(2) = -86.0714

a(3) = 7.5

»

 

 

 

» nma_HW_5_6

 

 

  solve problem 5.6

  Nasser Abbasi

 

enter the x values:[1 2 3 4 5]

enter the y values:[2470 2510 2410 2350 2240]

enter the polyminal degree to fit to data [1 to 4]:4

 

a =

 

  1.0e+003 *

 

    1.8400

    1.1425

   -0.6437

    0.1425

   -0.0112

 

fitting polynomial degree=4

a(0) = 1840

a(1) = 1142.5

a(2) = -643.75

a(3) = 142.5

a(4) = -11.25

»

 



SOURCE CODE

 


function nma_HW_5_6()

%

% solve problem 5.6

% Nasser Abbasi

 

clear all; help nma_HW_5_6;

 

x=input('enter the x values:');

y=input('enter the y values:');

deg = input('enter the polyminal degree to fit to data [1 to 4]:');

 

figure;

plot(1:length(x),y,'or');

title(sprintf('fitting data using polynomial of degree %d',deg));

xlabel('Day number');

ylabel('Dow value');

grid on;

 

A= makeDesignMatrix(x,deg);

a=inv(A'*A)*A'*y'


 

%Now, add sixth day to see how the fit will extent to it

x(6)=6;

printCoeff(a);

 

   if(deg==1)  % y=a1 + a2 x

       Y=a(1)+a(2)*x;

    else

       if(deg==2)  % y=a1+a2 x+ a3 x^2

           Y=a(1)+a(2)*x+a(3)*x.^2;

       else

          if(deg==3)  % y=a1 + a2 x +a3 x^2 + a4 x^3

               Y=a(1)+a(2)*x+a(3)*x.^2+a(4)*x.^3;

          else

              if(deg==4)  % y=a1 +a2 x + a3 x^2 + a4 x^3 + a5 x^4

                  Y=a(1)+a(2)*x+a(3)*x.^2+a(4)*x.^3+a(5)*x.^4;

              end

          end

        end        

   end

 

 

 

%

% now estimate the value at day 6 using the polynomial

%

 

hold on;

plot(1:6,Y,'--');

legend('actual','fitted data');

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function A=makeDesignMatrix(x,deg)

%note: y1=1, y2=x, y3=x^2, y4=x^3, y5=x^4

 

 

for(i=1:length(x))

   for(j=1:deg+1)

       A(i,j)= x(i)^(j-1);

   end

end

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%

%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function        printCoeff(a)

 

fprintf('fitting polynomial degree=%d\n',length(a)-1);

 

for (i=1:length(a))

   fprintf('a(%d) = %g\n',i-1,a(i));

end