Approximating the area under a curve with a Riemann sum using for loops
So as the title states I am currently working on a lab assignment to estimate the integral of e^(sin(t))
. The portion of provided code I am trying to complete that I am getting stuck on is the for loop.
What I have so far
### Setting Up the Problem ###
a <- 0 ## Lower Bound of t.
b <-10 ## Upper Bound of t.
n <- 5 ## Number of Intervals.
dFdt <- function(t){exp(sin(t)) } ## What function we are trying to estimate the antiderivative?
UnitIntervalLength<-((b-a)/n) ## What is the length
of each interval in terms of a, b and n?
### Plot the Function
t <- seq(a,b,.01) ## Set the t-axis to visualize.
plot(t, dFdt(t), type="l") ## Plot the function dFdt at given interval 't', DO NOT FORGET TYPE='L'.
### Use R built-in function 'for' to start the recursive evaluation of the dfdt at either left end, right end or
### midpoint depends on the method you are using.
Sum <- 0 ## Start with a sum of zero.
for(i in 1:n){
t0 <-a+(i-1)*UnitIntervalLength ## t0 is the left end of the i-th interval
t1 <-t0+i ## t1 is the right end of the i-th interval
Eval_i <- dFdt(t) ## Evaluate function at either left end, right end or midpoint.
Area_i =UnitIntervalLength*t ## Area of the current box i, which is calculated as length*width
## Will Discuss the Following Blank in Lab
Sum <-1:n ## Add this value to previous
sum to keep track of the current sum.
## Here is the Plot of Each Box (No Need to Change the Following)
segments(x0=t0,x1=t1,y0=Eval_i)
segments(y0=0,y1=Eval_i,x0=t1)
polygon(c(t0,t1,t1,t0),c(0,0,Eval_i,Eval_i),col=i,density=45)
}
### Display the Final result on the Title of the Plot.
title(main=paste("The Integral is Approximately ",round(Sum,3), sep=""))
What I have attempted and done
I have filled out all of the portions and set the equations but I believe the problem lies in my t1 and my Eval_i. My thinking for t1 is that the i-th interval needs to be set at +1 to t0 which is the left point of each interval. For Eval_i I was thinking that this is just the function I am trying to evaluate.
So I am wondering if my disconnects are happening sooner further up or if those 2 variables are my only issues.
edit
After a lot of trial an error I was able to find the solution to this. There were several issues involving my definition for different variables in the for loop.
t0 <-a+(i-1)*UnitIntervalLength
t1 <-a+i*UnitIntervalLength
Eval_i <- ((dFdt(t0)+dFdt(t1))/2)
Area_i =dFdt(t)*(t1-t0)
Sum<- Area_i+Sum
r
add a comment |
So as the title states I am currently working on a lab assignment to estimate the integral of e^(sin(t))
. The portion of provided code I am trying to complete that I am getting stuck on is the for loop.
What I have so far
### Setting Up the Problem ###
a <- 0 ## Lower Bound of t.
b <-10 ## Upper Bound of t.
n <- 5 ## Number of Intervals.
dFdt <- function(t){exp(sin(t)) } ## What function we are trying to estimate the antiderivative?
UnitIntervalLength<-((b-a)/n) ## What is the length
of each interval in terms of a, b and n?
### Plot the Function
t <- seq(a,b,.01) ## Set the t-axis to visualize.
plot(t, dFdt(t), type="l") ## Plot the function dFdt at given interval 't', DO NOT FORGET TYPE='L'.
### Use R built-in function 'for' to start the recursive evaluation of the dfdt at either left end, right end or
### midpoint depends on the method you are using.
Sum <- 0 ## Start with a sum of zero.
for(i in 1:n){
t0 <-a+(i-1)*UnitIntervalLength ## t0 is the left end of the i-th interval
t1 <-t0+i ## t1 is the right end of the i-th interval
Eval_i <- dFdt(t) ## Evaluate function at either left end, right end or midpoint.
Area_i =UnitIntervalLength*t ## Area of the current box i, which is calculated as length*width
## Will Discuss the Following Blank in Lab
Sum <-1:n ## Add this value to previous
sum to keep track of the current sum.
## Here is the Plot of Each Box (No Need to Change the Following)
segments(x0=t0,x1=t1,y0=Eval_i)
segments(y0=0,y1=Eval_i,x0=t1)
polygon(c(t0,t1,t1,t0),c(0,0,Eval_i,Eval_i),col=i,density=45)
}
### Display the Final result on the Title of the Plot.
title(main=paste("The Integral is Approximately ",round(Sum,3), sep=""))
What I have attempted and done
I have filled out all of the portions and set the equations but I believe the problem lies in my t1 and my Eval_i. My thinking for t1 is that the i-th interval needs to be set at +1 to t0 which is the left point of each interval. For Eval_i I was thinking that this is just the function I am trying to evaluate.
So I am wondering if my disconnects are happening sooner further up or if those 2 variables are my only issues.
edit
After a lot of trial an error I was able to find the solution to this. There were several issues involving my definition for different variables in the for loop.
t0 <-a+(i-1)*UnitIntervalLength
t1 <-a+i*UnitIntervalLength
Eval_i <- ((dFdt(t0)+dFdt(t1))/2)
Area_i =dFdt(t)*(t1-t0)
Sum<- Area_i+Sum
r
add a comment |
So as the title states I am currently working on a lab assignment to estimate the integral of e^(sin(t))
. The portion of provided code I am trying to complete that I am getting stuck on is the for loop.
What I have so far
### Setting Up the Problem ###
a <- 0 ## Lower Bound of t.
b <-10 ## Upper Bound of t.
n <- 5 ## Number of Intervals.
dFdt <- function(t){exp(sin(t)) } ## What function we are trying to estimate the antiderivative?
UnitIntervalLength<-((b-a)/n) ## What is the length
of each interval in terms of a, b and n?
### Plot the Function
t <- seq(a,b,.01) ## Set the t-axis to visualize.
plot(t, dFdt(t), type="l") ## Plot the function dFdt at given interval 't', DO NOT FORGET TYPE='L'.
### Use R built-in function 'for' to start the recursive evaluation of the dfdt at either left end, right end or
### midpoint depends on the method you are using.
Sum <- 0 ## Start with a sum of zero.
for(i in 1:n){
t0 <-a+(i-1)*UnitIntervalLength ## t0 is the left end of the i-th interval
t1 <-t0+i ## t1 is the right end of the i-th interval
Eval_i <- dFdt(t) ## Evaluate function at either left end, right end or midpoint.
Area_i =UnitIntervalLength*t ## Area of the current box i, which is calculated as length*width
## Will Discuss the Following Blank in Lab
Sum <-1:n ## Add this value to previous
sum to keep track of the current sum.
## Here is the Plot of Each Box (No Need to Change the Following)
segments(x0=t0,x1=t1,y0=Eval_i)
segments(y0=0,y1=Eval_i,x0=t1)
polygon(c(t0,t1,t1,t0),c(0,0,Eval_i,Eval_i),col=i,density=45)
}
### Display the Final result on the Title of the Plot.
title(main=paste("The Integral is Approximately ",round(Sum,3), sep=""))
What I have attempted and done
I have filled out all of the portions and set the equations but I believe the problem lies in my t1 and my Eval_i. My thinking for t1 is that the i-th interval needs to be set at +1 to t0 which is the left point of each interval. For Eval_i I was thinking that this is just the function I am trying to evaluate.
So I am wondering if my disconnects are happening sooner further up or if those 2 variables are my only issues.
edit
After a lot of trial an error I was able to find the solution to this. There were several issues involving my definition for different variables in the for loop.
t0 <-a+(i-1)*UnitIntervalLength
t1 <-a+i*UnitIntervalLength
Eval_i <- ((dFdt(t0)+dFdt(t1))/2)
Area_i =dFdt(t)*(t1-t0)
Sum<- Area_i+Sum
r
So as the title states I am currently working on a lab assignment to estimate the integral of e^(sin(t))
. The portion of provided code I am trying to complete that I am getting stuck on is the for loop.
What I have so far
### Setting Up the Problem ###
a <- 0 ## Lower Bound of t.
b <-10 ## Upper Bound of t.
n <- 5 ## Number of Intervals.
dFdt <- function(t){exp(sin(t)) } ## What function we are trying to estimate the antiderivative?
UnitIntervalLength<-((b-a)/n) ## What is the length
of each interval in terms of a, b and n?
### Plot the Function
t <- seq(a,b,.01) ## Set the t-axis to visualize.
plot(t, dFdt(t), type="l") ## Plot the function dFdt at given interval 't', DO NOT FORGET TYPE='L'.
### Use R built-in function 'for' to start the recursive evaluation of the dfdt at either left end, right end or
### midpoint depends on the method you are using.
Sum <- 0 ## Start with a sum of zero.
for(i in 1:n){
t0 <-a+(i-1)*UnitIntervalLength ## t0 is the left end of the i-th interval
t1 <-t0+i ## t1 is the right end of the i-th interval
Eval_i <- dFdt(t) ## Evaluate function at either left end, right end or midpoint.
Area_i =UnitIntervalLength*t ## Area of the current box i, which is calculated as length*width
## Will Discuss the Following Blank in Lab
Sum <-1:n ## Add this value to previous
sum to keep track of the current sum.
## Here is the Plot of Each Box (No Need to Change the Following)
segments(x0=t0,x1=t1,y0=Eval_i)
segments(y0=0,y1=Eval_i,x0=t1)
polygon(c(t0,t1,t1,t0),c(0,0,Eval_i,Eval_i),col=i,density=45)
}
### Display the Final result on the Title of the Plot.
title(main=paste("The Integral is Approximately ",round(Sum,3), sep=""))
What I have attempted and done
I have filled out all of the portions and set the equations but I believe the problem lies in my t1 and my Eval_i. My thinking for t1 is that the i-th interval needs to be set at +1 to t0 which is the left point of each interval. For Eval_i I was thinking that this is just the function I am trying to evaluate.
So I am wondering if my disconnects are happening sooner further up or if those 2 variables are my only issues.
edit
After a lot of trial an error I was able to find the solution to this. There were several issues involving my definition for different variables in the for loop.
t0 <-a+(i-1)*UnitIntervalLength
t1 <-a+i*UnitIntervalLength
Eval_i <- ((dFdt(t0)+dFdt(t1))/2)
Area_i =dFdt(t)*(t1-t0)
Sum<- Area_i+Sum
r
r
edited Nov 20 '18 at 0:28
bloddd
asked Nov 19 '18 at 20:29


blodddbloddd
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382177%2fapproximating-the-area-under-a-curve-with-a-riemann-sum-using-for-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382177%2fapproximating-the-area-under-a-curve-with-a-riemann-sum-using-for-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown