Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.94 KB | None | 0 0
  1. x = linspace(-5, 5, 11)
  2. x=x';
  3. y = [-5.8643; -6.7445; -5.2378; -3.2868; -2.2393;...
  4.     -0.5084; -1.2237; -0.7893; -4.8761; -11.0466; -20.0868]
  5.  
  6. %Ax=b
  7.  
  8. [A1, b1] = matrixer(x, y, 1);
  9. [A2, b2] = matrixer(x, y, 2);
  10. [A3, b3] = matrixer(x, y, 3);
  11. [A4, b4] = matrixer(x, y, 4);
  12. [A5, b5] = matrixer(x, y, 5);
  13. [A6, b6] = matrixer(x, y, 6);
  14. [A7, b7] = matrixer(x, y, 7);
  15. [A8, b8] = matrixer(x, y, 8);
  16. [A9, b9] = matrixer(x, y, 9);
  17. [A10, b10] = matrixer(x, y, 10);
  18.  
  19.  
  20. % without QR factorization
  21. coefficients1=flipud(inv(A1'*A1)*(A1'*b1));
  22. coefficients2=flipud(inv(A2'*A2)*(A2'*b2));
  23. coefficients3=flipud(inv(A3'*A3)*(A3'*b3));
  24. coefficients4=flipud(inv(A4'*A4)*(A4'*b4));
  25. coefficients5=flipud(inv(A5'*A5)*(A5'*b5));
  26. coefficients6=flipud(inv(A6'*A6)*(A6'*b6));
  27. coefficients7=flipud(inv(A7'*A7)*(A7'*b7));
  28. coefficients8=flipud(inv(A8'*A8)*(A8'*b8));
  29. coefficients9=flipud(inv(A9'*A9)*(A9'*b9));
  30. coefficients10=flipud(inv(A10'*A10)*(A10'*b10));
  31.  
  32.  
  33. xs = linspace(-5, 5);
  34. ys1 = polyval(coefficients1, xs);
  35. ys2 = polyval(coefficients2, xs);
  36. ys3 = polyval(coefficients3, xs);
  37. ys4 = polyval(coefficients4, xs);
  38. ys5 = polyval(coefficients5, xs);
  39. ys6 = polyval(coefficients6, xs);
  40. ys7 = polyval(coefficients7, xs);
  41. ys8 = polyval(coefficients8, xs);
  42. ys9 = polyval(coefficients9, xs);
  43. ys10 = polyval(coefficients10, xs);
  44.  
  45. figure
  46. plot(x,y,'o')
  47. hold on
  48. grid on
  49. plot(xs,ys1)
  50. plot(xs,ys2)
  51. plot(xs,ys3)
  52. plot(xs,ys4)
  53. plot(xs,ys5)
  54. legend('exact points', 'n=1', 'n=2', 'n=3', 'n=4', 'n=5');
  55. legend('Location','best')
  56. title('Interpolating without QR factorization (1-5)')
  57. ylim([-15,2])
  58. hold off
  59.  
  60. figure
  61. plot (x,y,'o')
  62. hold on
  63. grid on
  64. plot(xs,ys6)
  65. plot(xs,ys7)
  66. plot(xs,ys8)
  67. plot(xs,ys9)
  68. plot(xs,ys10)
  69. legend('exact points', 'n=6', 'n=7', 'n=8', 'n=9', 'n=10');
  70. legend('Location','best')
  71. title('Interpolating without QR factorization (6-10)')
  72. ylim([-15,2])
  73. hold off
  74.  
  75. % calculating errors
  76. residuum1 = b1 - A1*coefficients1;
  77. residuum2 = b2 - A2*coefficients2;
  78. residuum3 = b3 - A3*coefficients3;
  79. residuum4 = b4 - A4*coefficients4;
  80. residuum5 = b5 - A5*coefficients5;
  81. residuum6 = b6 - A6*coefficients6;
  82. residuum7 = b7 - A7*coefficients7;
  83. residuum8 = b8 - A8*coefficients8;
  84. residuum9 = b9 - A9*coefficients9;
  85. residuum10 = b10 - A10*coefficients10;
  86.  
  87.  
  88.  
  89. error1 = norm(residuum1);
  90. error2 = norm(residuum2);
  91. error3 = norm(residuum3);
  92. error4 = norm(residuum4);
  93. error5 = norm(residuum5);
  94. error6 = norm(residuum6);
  95. error7 = norm(residuum7);
  96. error8 = norm(residuum8);
  97. error9 = norm(residuum9);
  98. error10 = norm(residuum10);
  99.  
  100. order2 =[6, 7, 8, 9, 10];
  101. order1 = [1, 2, 3, 4, 5];
  102. errors1 = [error1, error2, error3, error4, error5];
  103. errors2 = [error6, error7, error8, error9, error10]
  104.  
  105. figure
  106. plot(order1,errors1, '--o')
  107. legend('Errors depending on order of polynomial')
  108. title('Errors without QR factorization (1-5)')
  109.  
  110. figure
  111. plot(order2,errors2, '--o')
  112. legend('Errors depending on order of polynomial')
  113. title('Errors without QR factorization (6-10)')
  114.  
  115. beep;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement