Advertisement
Cinster

Untitled

Apr 5th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. clear all clc
  2. prompt = 'Enter n: ';
  3. n = input(prompt);
  4. simprl(@(x)(exp(-x^2)),0,1,n);
  5. function s=simprl(f,a,b,n)
  6. array1 = ones(1,n);
  7. array2 = ones(1, 2*n);
  8. array3 = ones(1,n);
  9. array4 = ones(1,n);
  10. fun = @(x)(exp(-x.^2)); true = integral(fun,a,b);
  11. n = n * 2;
  12. even =0;
  13. odd = 0;
  14. y0 = f (a);
  15. yn = f (b);
  16. x = 0;
  17. h=(b-a)/( n / 2);
  18. for i = 0: (( n) + 1) x=0;
  19. even = 0;
  20. odd = 0;
  21. h=(b-a)/(i + 1);
  22. for j = 1:i
  23. x = x + a + h; r = mod(j,2);
  24. if (r == 0)
  25. even = even + f(x);
  26. else
  27. end end
  28. end
  29. I = (h / 3) * ((y0 + yn) + 2 * even + 4 * odd);
  30. odd = odd + f(x);
  31. fprintf('Value after %i', n/2 ) fprintf(' iterations: %.16f \n', I);
  32. fprintf('True value for the integral: %.16f' , true);
  33. even =0;
  34. odd = 0;
  35. y0 = f (a);
  36. yn = f (b);
  37. x = 0;
  38. h=(b-a)/( n );
  39. for i = 0: ((2 * n) + 1) x=0;
  40. even = 0;
  41. odd = 0;
  42. h=(b-a)/(i + 1);
  43. for j = 1:i
  44. x = x + a + h; r = mod(j,2);
  45. if (r == 0)
  46. even = even + f(x);
  47. else
  48. end end
  49. y = (h / 3) * ((y0 + yn) + 2 * even + 4 * odd); array2(1, i + 1) = y;
  50. end
  51. even =0;
  52. odd = 0;
  53. y0 = f (a);
  54. yn = f (b);
  55. x = 0;
  56. h=(b-a)/(n / 2);
  57. for i = 0: n - 1
  58. x=0;
  59. even = 0;
  60. odd = 0;
  61. h=(b-a)/(i + 1);
  62. odd = odd + f(x);
  63.  
  64. for j = 1:i
  65. x = x + a + h;
  66. r = mod(j,2);
  67. if (r == 0)
  68. even = even + f(x);
  69. else
  70. end end
  71. y = (h / 3) * ((y0 + yn) + 2 * even + 4 * odd); array1(1, i + 1) = y;
  72. array4(1, i + 1) = i;
  73. l = array1(i + 1);
  74. m = array2(2 * (i + 1));
  75. rich= ( ((2^8)*m)-l)/((2^8)-1); error = (rich - true ) / true;
  76. error = abs(error);
  77. array3(1, i + 1) = error;
  78. end
  79. a = array3(2:2:end);
  80. b = array4(2:1:(n/2)
  81. loglog(b,a)
  82. title('Composite
  83. xlabel('iterations') ylabel('error %');
  84. end
  85. + 1);
  86. Simpsons 1/3 rule error %'); ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement