Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. function [ output_args ] = riemann_sum( a, b, n )
  2. dx = (b-a)/n;
  3.  
  4. r_sum = 0;
  5. for i=1:n %this begins our for-loop to perform the right-summation for our
  6. %chosen function
  7. r_sum = r_sum + (exp(-(a + i*dx)^2))*dx; %you'll modify this function for
  8. %whatever you'd like to sum; here, we're using f(x) = e^x
  9. end
  10.  
  11. l_sum = 0;
  12. for i=0:(n-1) %this begins our for-loop to perform the left-sum for our
  13. %chosen function
  14. l_sum = l_sum + (exp(-(a + i*dx)^2))*dx; %you'll modify this function for
  15. %whatever you'd like to sum; here, we're using f(x) = e^x
  16. end
  17. m_sum = 0;
  18. for i=0:(n-1) %this begins our for-loop to perform the left-sum for our
  19. %chosen function
  20. m_sum = m_sum + (exp(-(a + (i-0.5)*dx)^2))*dx; %you'll modify this function for
  21. %whatever you'd like to sum; here, we're using f(x) = e^x
  22.  
  23. end
  24. t_sum = 0;
  25. for i=0:(n-1) %this begins our for-loop to perform the left-sum for our
  26. %chosen function
  27. t_sum = t_sum + (exp(-(a + i*dx)^2)+exp(-(a+(i+1)*dx))^2)*dx/2; %you'll modify this function for
  28. %whatever you'd like to sum; here, we're using f(x) = e^x
  29. end
  30. r_sum
  31. l_sum
  32. m_sum
  33. t_sum
  34.  
  35. %the above two lines display the sums we obtained. r_sum is the right sum,
  36. %and l_sum is the left sum.
  37.  
  38. % PROJECT
  39. %
  40. % Part 1:
  41. %
  42. % Attempt to compute the following i) analytically and ii) numerically:
  43. % exp(x^2) from 0 to 5
  44. % sin(x^2) from 0 to pi/2
  45. %
  46. % Part 2:
  47. %
  48. % Modify the code to also yield a middle sum (ie, the height of each
  49. % rectangle comes from the function value at the midpoint of each interval,
  50. % rather than from the left-most or right-most point of each interval.)
  51. %
  52. % Bonus:
  53. %
  54. % Modify the code to also yield a trapezoidal sum (ie, computing the area
  55. % under the curve using trapezoids instead of rectangles.)
  56. %
  57.  
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement