Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.17 KB | None | 0 0
  1. % Artur Wi�niewski  g.6 lab.10 zad. 5 opcja 3
  2.  
  3. x=[-12, -9, -6, -2, 1, 4, 6, 8, 9, 12];
  4. y=[3, -1, -4, -2, 2, 2, 7, 1, 0, 5];
  5.  
  6.  
  7. if length(x)==length(y)
  8.  
  9.   stopien=7;
  10.   n=length(x);
  11.  
  12.   X=zeros(stopien+1,stopien+1);
  13.   A=zeros(stopien+1,1);
  14.   Y=zeros(stopien+1,1);
  15.  
  16.   suma = 0;
  17.   potega = 0;
  18.  
  19.   for i=1:stopien+1
  20.     for j=1:stopien+1
  21.       for k=1:n
  22.         suma = suma + x(k)^potega;
  23.       end
  24.         potega = potega + 1;
  25.         X(i,j)=suma;
  26.     end
  27.       suma = 0;
  28.       potega = i;
  29.   end
  30.   disp("Macierz X");
  31.   disp(X);
  32.  
  33.   sumaY = 0;
  34.   potegaY = 0;
  35.   for i=1:stopien+1
  36.     for k=1:n
  37.       sumaY = sumaY + (x(k)^potegaY * y(k));
  38.     end
  39.     potegaY = potegaY + 1;
  40.     Y(i,1) = sumaY;
  41.   end
  42.  
  43.   disp("Macierz Y");
  44.   disp(Y);
  45.   A=inv(X)*Y;
  46.   disp("Macierz A");
  47.   disp(A);
  48.  
  49.  
  50.   Q=polyfit(x, y, stopien);
  51.   yi=zeros(1,n);
  52.   for i=1:n
  53.     yi(1,i)=0;
  54.       for j=1:stopien+1
  55.         yi(1,i)=yi(1,i) + Q(1,j)*x(1,i)^(stopien+1-j);
  56.       end
  57.   end
  58.  
  59.  
  60.   plot(x,y, 'bx--');
  61.   grid on;
  62.   hold on;
  63.   plot(x,yi, 'ro--');
  64.  
  65.  
  66. else
  67. disp("Liczba wsp��rz�dnych x nie jest r�wna liczbie wsp��rz�dnych y");
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement