to find Upper and Lower Riemann Sum with exact accuracy

9 views (last 30 days)
Hi, In order to answer these questions, I faced problems with my code(matlab doesn't give any answer to code!! :O )!= questions= 1.Write a program which conclude approximate integral of the function=[ x^(x+sqrt(x))-e^sin(x)] , by calculating the 'Lower' and 'Upper' 'Riemann' Sum which should have maximum difference of e=0.0001 ? ###for visualizing the question and also my answer I wrote the formulas and rectangles in this picture=
in my code for ques1,I think line=[ r1=a+(i-1)*h ] makes abit trouble,but there is more!(matlab cannot run my ans totally :O ) tnx in advanve.
b=3.5;a=3.1;e=0.0001;
s1=0;s2=999;
% s1:lower Riemann Sum and s2:upper Riemann Sum
%for the "while" loop to start I had to first make "abs(s2-s1)" larger! do you recommend better way?
n=10;
%this 'n' is number of intervals or number of rectangles! which meets the need of accuracy
while abs(s2-s1) >e
s1=0;
s2=0;
for i=1:n
h=(b-a)/n; % I meant "dx" or dimension of intervals by "h"
r1=a+(i-1)*h; % for Lower Riem Sum intervals should be started from 'a'(the first bound) [I think 'cos of this line,code faces problem!]
r2=a+i*h; %but for Upper sum I started from 'a+h' or 'a+dx'
K1=r1^(r1+sqrt(r1))-exp(sin(r1));
K2=r2^(r2+sqrt(r2))-exp(sin(r2));
s1=s1+K1*h;
s2=s2+K2*h;
end
n=n+80; %here I'm acelerating accuracy to reach the disre of ques
end
s3=(s1+s2)/2; %it's appoximate integral of function
fprintf('for n=%g: lower riem=%g, Upper riem=%g, approx int=%g',n,s1,s2,s3 )
  4 Comments
Morteza
Morteza on 17 Jun 2014
Edited: Morteza on 17 Jun 2014
I didn't know difference here between cm and reply,sorry 4that,I just said "plz help me",I didn't urge u or saying that's ur problem or etc..., and finally I say my code is a mathematical algorithm,i think it's right, but matlab even cannot "run" it,when I say "no running" so how can I find the error? _ nevertheless _ i said maybe 'coz of this line:
r1=a+(i-1)*h
i have problem,i guess,plz read the post.
Morteza
Morteza on 17 Jun 2014
Edited: Morteza on 20 Jun 2014
I my self solved the problem,this was 'coz of large quantity of computation in the "while" loop,i wrote another code for that after one day!!

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!