midpoint rule for integration
173 views (last 30 days)
Show older comments
Hello, I took an entry level Matlab course last semester and for a Calculus 2 assignment I have to write a program to find the left,right,and midpoint rule as well as simpson's rule and the trapezoidal rule.
Right now I am almost done. I have the left, right, trap, and simpsons rule working. I'm trying to include the midpoint rule in this but its not working. I keep getting this error,
- Attempted to access f(1.5); index must be a positive integer or logical. *
Error in calc2 (line 68) mpr=mpr+f((2*k+1)/2)*deltax; %midpoint rule_
So far I have:
close all
clear all
disp(' ');
disp('This program will calculate the approximate integral');
disp('using the left, right, and midpoint rules, as well as');
disp('the trapezoidal and simpsons rule');
disp('-------------------------------------------------------');
disp(' ');
lower=1;
upper=2;
N=10;
% % % % % % % % lower=input('Enter the lower value of the integral: '); %lower value of integral
% % % % % % % % upper=input('Enter the upper value of the integral: '); %top value of integral
% % % % % % % % N=input('Enter the number of steps: ');
deltax=(upper-lower)/N;
x=[lower:deltax:upper]; %creates a vector of n+1 evenly spaced points
% % % % % % % % f=input('Enter the function (in proper matlab format): '); %function
f=(x.^3-1).^(1/2);
%LEFTPOINT, RIGHTPOINT MIDPOINT, AND TRAPEZOIDAL RULES
%initialize lpr,rpr,mpr,tr
lpr=0; %left point rule
rpr=0;%right point rule
mpr=0; %midpoint rule
tr=0;%trapezoidal rule
for k=1:N;
lpr=lpr+f(k)*deltax; %left point rule
rpr=rpr+f(k+1)*deltax; %right point rule
mpr=mpr+f((2*k+1)/2)*deltax; %midpoint rule %<<<<<<<< where im running into trouble
tr=tr+(f(k)+f(k+1))/2*deltax; %trapazoidal rule
end;
%SIMPSONS RULE
if numel(f)>1 % If the input provided is a vector
N=numel(f)-1;
sr= deltax/3*(f(1)+2*sum(f(3:2:end-2))+4*sum(f(2:2:end))+f(end));
else % If the input provided is an anonymous function
sr= deltax/3*(f(x(1))+2*sum(f(x(3:2:end-2)))+4*sum(f(x(2:2:end)))+f(x(end)));
end
%fprintf for results.
disp(' ');
fprintf('Left-endpoint approximation = %f.\n',lpr);
fprintf('Right-endpoint approximation = %f.\n',rpr);
fprintf('Midpoint approximation = %f.\n',mpr);
fprintf('Trapezoidal approximation = %f.\n',tr);
fprintf('Simpsons Rule approximation = %f.\n',sr);
Right now I have an example integral from 1 to 2 of (x.^3-1)^(1/2) but I'm going to remove those so you can enter your own values when you run the progam.
If anyone knows how I can fix that problem, I'd really appreciate it. This assignment is due friday so I have two more days. Thank you for the help!
0 Comments
Answers (2)
sixwwwwww
on 23 Oct 2013
Dear Joe, re-write f as follows:
f = @(x) (x.^3-1).^(1/2);
Then code should work. I hope it helps. Good luck!
0 Comments
Alperen KARAVELIOGLU
on 27 May 2020
U can also use inline method , like:
f = inline('3*.^2','x');
%hope helps..
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!