0% found this document useful (0 votes)
25 views7 pages

Kaku

This document defines several functions for calculating integrals numerically. It starts by defining a function called my.fun that squares its input. It then defines functions like my.calc and my.int that take other functions and parameters to calculate their results on given intervals and return the results. It demonstrates calling these functions with different parameters to calculate integrals of my.fun with varying levels of precision by changing the interval widths. It further refines the functions to make them more flexible by passing the parameters in list objects.

Uploaded by

Divom Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Kaku

This document defines several functions for calculating integrals numerically. It starts by defining a function called my.fun that squares its input. It then defines functions like my.calc and my.int that take other functions and parameters to calculate their results on given intervals and return the results. It demonstrates calling these functions with different parameters to calculate integrals of my.fun with varying levels of precision by changing the interval widths. It further refines the functions to make them more flexible by passing the parameters in list objects.

Uploaded by

Divom Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Kaku

September 2, 2020

[1]: my.fun <- function(x) {


return(x^2)
}

[2]: x <- seq(0,1.99,by=0.1)


x.line <- seq(0,2,by=0.1)
y <- my.fun(x)
barplot(height=y,width = 0.1, space=0, xlim=c(0,2), ylim=c(-0.5,4))
lines(x=c(1.9,1.9),y=c(0,4), col="orange")
lines(x=x.line,y=my.fun(x.line),col="red")
text(x=1.9, y=-0.3, "1.9")

1
[3]: my.calc <- function(fun, my.grid) {
calc.results <- fun(my.grid)
return(calc.results)
}

[4]: my.calc(fun=my.fun,1:3)

1. 1 2. 4 3. 9
[5]: my.fun2 <- function(x) {
return(x^3)
}
x <- seq(0,1.99,by=0.1)

2
y.new <- my.calc(fun=my.fun,my.grid=x)
barplot(height=y.new, width = 0.1, space=0)

1 Answer 1
[6]: my.int <- function(fun,my.width,my.range){
y <- fun(seq(from=my.range[1],to=my.range[length(my.range)]-my.width,by=my.
,→width))

ret <- sum(y*my.width)


return(ret)
}

3
[7]: my.int(my.fun,0.1,c(0,2))

2.47
[8]: my.int(my.fun,0.01,c(0,2))

2.6467
[9]: my.int(my.fun,0.001,c(0,2))

2.664667
[10]: my.fun3 <- function(x) {
return(x)
}
my.int(my.fun3, 0.001, c(0,2))

1.999

2 Answer 2
[11]: my.int2 <- function(my.input.list){
fun <- get("fun",my.input.list[1])
my.range <- get("my.range",my.input.list[2])
my.width <- get("my.width",my.input.list[3])
y <- fun(seq(from=my.range[1],to=my.range[2]-my.width,by=my.width))
ret <- sum(y*my.width)
return(ret)
}

[12]: inputarg.list <- list(fun=my.fun,my.range=c(0,2),my.width=0.1)


my.int2(my.input.list = inputarg.list)

2.47
[13]: inputarg.list <- list(fun=my.fun,my.range=c(0,2),my.width=0.01)
my.int2(my.input.list = inputarg.list)

2.6467

3 Answer 3
[14]: my.int3 <- function(my.input.list){
fun <- get("fun",my.input.list[1])
my.range <- get("my.range",my.input.list[2])
my.width <- get("my.width",my.input.list[3])
y <- fun(seq(from=my.range[1],to=my.range[2]-my.width,by=my.width))
ret <- sum(y*my.width)

4
x.line <- seq(from=my.range[1],to=my.range[2]-my.width,by=my.width)
barplot(height=y,width = my.width, space=0)
lines(x=x.line,y=fun(x.line),col="red")
xtick = seq(from=my.range[1],to=my.range[2],by=my.width)
axis(side = 1,pos = -0.05, at = xtick, labels = FALSE)
axis(side = 1,pos = -0.05, at = xtick, tick = FALSE, labels = xtick)
return(ret)
}

[15]: inputarg.list$my.width <- 0.1


my.int3(inputarg.list)

2.47

5
[16]: inputarg.list$my.width <- 0.2
my.int3(inputarg.list)

2.28

[17]: inputarg.list$my.width <- 0.1


inputarg.list$my.range <- c(1,3)
my.int3(inputarg.list)

8.27

6
[ ]:

You might also like