Так что, как говорится в названии, я сейчас работаю над лабораторным заданием для оценки интеграла e^(sin(t))
. Часть предоставленного кода, которую я пытаюсь завершить, - это цикл for.
Что у меня до сих пор
### 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=""))
То, что я пытался и сделал
Я заполнил все части и установил уравнения, но я считаю, что проблема заключается в моем t1 и моем Eval_i. Я думаю о t1, что i-й интервал должен быть установлен от +1 до t0, который является левой точкой каждого интервала. Для Eval_i я думал, что это просто функция, которую я пытаюсь оценить.
Так что мне интересно, происходят ли мои разъединения раньше, или эти две переменные являются моими единственными проблемами.
редактировать
После многих проб и ошибок мне удалось найти решение этой проблемы. Было несколько проблем, связанных с моим определением различных переменных в цикле for.
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