Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %Zadanie 5.
- 2 %Opcja 3.
- 3
- 4 x=[-12, -9, -6, -2, 1, 4, 6, 8, 9, 12];
- 5 y=[3, -1, -4, -2, 2, 2, 7, 1, 0, 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