Advertisement
Guest User

Untitled

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