Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [ output_args ] = riemann_sum( a, b, n )
- dx = (b-a)/n;
- r_sum = 0;
- for i=1:n %this begins our for-loop to perform the right-summation for our
- %chosen function
- r_sum = r_sum + (exp(-(a + i*dx)^2))*dx; %you'll modify this function for
- %whatever you'd like to sum; here, we're using f(x) = e^x
- end
- l_sum = 0;
- for i=0:(n-1) %this begins our for-loop to perform the left-sum for our
- %chosen function
- l_sum = l_sum + (exp(-(a + i*dx)^2))*dx; %you'll modify this function for
- %whatever you'd like to sum; here, we're using f(x) = e^x
- end
- m_sum = 0;
- for i=0:(n-1) %this begins our for-loop to perform the left-sum for our
- %chosen function
- m_sum = m_sum + (exp(-(a + (i-0.5)*dx)^2))*dx; %you'll modify this function for
- %whatever you'd like to sum; here, we're using f(x) = e^x
- end
- t_sum = 0;
- for i=0:(n-1) %this begins our for-loop to perform the left-sum for our
- %chosen function
- t_sum = t_sum + (exp(-(a + i*dx)^2)+exp(-(a+(i+1)*dx))^2)*dx/2; %you'll modify this function for
- %whatever you'd like to sum; here, we're using f(x) = e^x
- end
- r_sum
- l_sum
- m_sum
- t_sum
- %the above two lines display the sums we obtained. r_sum is the right sum,
- %and l_sum is the left sum.
- % PROJECT
- %
- % Part 1:
- %
- % Attempt to compute the following i) analytically and ii) numerically:
- % exp(x^2) from 0 to 5
- % sin(x^2) from 0 to pi/2
- %
- % Part 2:
- %
- % Modify the code to also yield a middle sum (ie, the height of each
- % rectangle comes from the function value at the midpoint of each interval,
- % rather than from the left-most or right-most point of each interval.)
- %
- % Bonus:
- %
- % Modify the code to also yield a trapezoidal sum (ie, computing the area
- % under the curve using trapezoids instead of rectangles.)
- %
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement