You are on page 1of 7

NAME: SHREYASH KHARAT Homework: HW3

(Problem 1)

> entries <- runif(100, 0, 5)


> mat <- matrix(data = entries, nrow = 10, ncol = 10)
> for(i in 1:10)
+ {
+ for(j in 1:i)
+ {
+ mat[j, i] <- mat[i, j]
+ }
+ }
> mat

[,1] [,2] [,3] [,4] [,5] [,6] [,7]


[1,] 0.4119210 4.478705 1.5745270 3.0484358 4.038276 2.6488469 0.53437886
[2,] 4.4787055 4.902192 3.9174209 4.2793208 4.229718 3.9359214 3.72815545
[3,] 1.5745270 3.917421 2.8830080 2.8893889 2.172622 4.8415837 0.57813591
[4,] 3.0484358 4.279321 2.8893889 2.4957945 2.067924 0.2679755 4.81438056
[5,] 4.0382755 4.229718 2.1726225 2.0679239 2.496712 1.8171313 3.29489572
[6,] 2.6488469 3.935921 4.8415837 0.2679755 1.817131 4.7176117 2.67693320
[7,] 0.5343789 3.728155 0.5781359 4.8143806 3.294896 2.6769332 0.07403879
[8,] 1.4668348 2.613656 0.2757281 0.2071663 4.443915 1.3645463 1.48325048
[9,] 4.0211012 2.456859 2.6753035 0.7044110 1.796290 2.5434372 0.64316116
[10,] 3.9528534 2.425395 3.0117077 0.1232930 2.192489 0.4707156 4.53628194
[,8] [,9] [,10]
[1,] 1.4668348 4.0211012 3.9528534
[2,] 2.6136556 2.4568586 2.4253953
[3,] 0.2757281 2.6753035 3.0117077
[4,] 0.2071663 0.7044110 0.1232930
[5,] 4.4439147 1.7962897 2.1924890
[6,] 1.3645463 2.5434372 0.4707156
[7,] 1.4832505 0.6431612 4.5362819
[8,] 0.3396675 3.1035405 2.7971251
[9,] 3.1035405 3.4852866 4.1282186
[10,] 2.7971251 4.1282186 0.9372923

> Jacobi <- function(m, error = 10^-3)


+ {
+ n <- nrow(m)
+ if(ncol(m) != n || any(t(m) != m))
+ {
+ stop('Matrix is not square')

1
+ }
+ x <- m
+ require('pracma')
+ y <- eye(n)
+ max_off_diag <- which.max(abs(x - diag(diag(x))))
+ pq <- arrayInd(max_off_diag, dim(x))
+ p <- pq[1]
+ q <- pq[2]
+ while(TRUE)
+ {
+ k <- x[p, q]/(x[q, q] - x[p, p])
+ a <- 1/sqrt(1 + k^2)
+ b <- a*k
+ m1 <- matrix(c(a, -b, b, a), 2, 2)
+ x[c(p, q), ] <- t(m1)%*%x[c(p, q), ]
+ x[, c(p, q)] <- x[, c(p, q)]%*%m1
+ y[, c(p, q)] <- y[, c(p, q)]%*%m1
+ max_off_diag <- which.max(abs(x - diag(diag(x))))
+ pq <- arrayInd(max_off_diag, dim(x))
+ p <- pq[1]
+ q <- pq[2]
+ if(abs(x[p,q]) < error * sqrt(sum(diag(x)^2)/n))
+ {
+ break
+ }
+ }
+ return(list(y, diag(x)))
+ }
> Jacobi(mat)

[[1]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.62478818 0.0968592637 -0.19295321 -0.012531618 -0.13006445 0.3261199
[2,]-0.31353957 0.0003992368 -0.15794352 -0.660941908 0.11459662 0.4503175
[3,]-0.09177772 -0.4721792734 0.70784697 0.004256019 -0.28887988 0.3107114
[4,]-0.11272456 0.1963831359 -0.08192404 0.471651799 -0.36748879 0.2630421
[5,] 0.27306611 0.3028626553 0.39755835 0.269992646 0.48081779 0.3431262
[6,] 0.14788817 -0.6827937730 -0.35265826 0.184222681 0.37215296 0.3165741
[7,]-0.02104749 0.2347448248 -0.22759017 0.042174266 0.01212105 0.2736033
[8,]-0.47564534 0.2598387159 0.10473522 0.132359387 0.39662743 0.2293427
[9,]-0.32952032 -0.0387107478 -0.27021020 0.290770143 -0.37223551 0.3063353
[10,] 0.24187096 0.2168999398 0.10891842 -0.360341842 -0.29282790 0.2930431
[,7] [,8] [,9] [,10]
[1,] 0.40210797 0.3234283 0.129493374 -0.39564361
[2,] -0.06859155 -0.1434012 -0.284128692 -0.34349146
[3,] 0.27999403 0.0462209 -0.071237356 0.04423972

2
[4,] -0.34555678 0.2440156 -0.570265537 -0.10566379
[5,] -0.19531824 -0.4456423 0.016628080 -0.12967779
[6,] -0.23558114 0.1083828 -0.006438606 0.21820216
[7,] 0.54314885 -0.2369228 -0.275106329 0.62373363
[8,] 0.13867379 0.6006993 0.293018030 0.04377044
[9,] 0.01522812 -0.3983983 0.561985773 -0.15866715
[10,] -0.47715569 0.1646606 0.307466735 0.48145040

[[2]]
[1] -1.9487330 5.3586237 -0.6494891 -1.4390868 1.9575819 26.1847587
[7] -8.7007561 -4.3199123 5.6989788 0.6015593

> eigens <- eigen(mat)


> eigens$values

[1] 26.1847590 5.6989809 5.3586276 1.9575992 0.6015620 -0.6494894


[7] -1.4391094 -1.9487243 -4.3199239 -8.7007565

> eigens$vectors

[,1] [,2] [,3] [,4] [,5] [,6]


[1,] -0.3261152 0.129509902 -0.096997076 -0.1300817 0.39526840 -0.19337742
[2,] -0.4503041 -0.284384962 -0.000783388 0.1160375 0.34357097 -0.15809451
[3,] -0.3106787 -0.071132830 0.472271619 -0.2888821 -0.04382201 0.70789693
[4,] -0.2629884 -0.570052652 -0.195759722 -0.3685844 0.10556500 -0.08194547
[5,] -0.3431822 0.016668360 -0.302861891 0.4801547 0.13017909 0.39746789
[6,] -0.3166184 -0.006164839 0.682868271 0.3718605 -0.21842286 -0.35256766
[7,] -0.2735899 -0.275354561 -0.234587850 0.0118583 -0.62364262 -0.22722689
[8,] -0.2293925 0.292955789 -0.259775653 0.3962247 -0.04424753 0.10486110
[9,] -0.3063242 0.562178240 0.038801173 -0.3729347 0.15868815 -0.27012732
[10,] -0.2930236 0.307130318 -0.217195355 -0.2919984 -0.48159373 0.10907254
[,7] [,8] [,9] [,10]
[1,] 0.012786637 -0.62414220 0.32492163 -0.40202414
[2,] 0.660555027 0.31327035 -0.14378456 0.06860021
[3,] -0.003205366 0.09153307 0.04603415 -0.27996317
[4,] -0.471248706 0.11331743 0.24389718 0.34560051
[5,] -0.271290376 -0.27391436 -0.44496001 0.19526912
[6,] -0.184636418 -0.14761918 0.10841508 0.23553581
[7,] -0.042484204 0.02062985 -0.23741606 -0.54313654
[8,] -0.133230508 0.47684673 0.59976728 -0.13881960
[9,] -0.289616230 0.32881726 -0.39893963 -0.01527801
[10,] 0.360965413 -0.24153546 0.16481420 0.47722402

3
> # The order isn't same, but the outputs are accurate upto 2 decimal places.

Problem 2

> f1 <- function(x)


+ {
+ return((1-x^2)^(3/2))
+ }
> f2 <- function(x)
+ {
+ return(sin(x^2))
+ }
> rect_int <- function(func, lower_lim, upper_lim, error = 0.001, n = 500)
+ {
+ a <- lower_lim; b <- upper_lim
+ integral <- integrate(func, a, b)$value
+ for(k in 1:n)
+ {
+ h <- (b-a)/k
+ y <- rep(0, k)
+ for(j in 1:k)
+ {
+ y[j] <- func(a + (j-1)*h)
+ }
+ rect_sum <- sum(y)*h
+ if(abs(rect_sum - integral) < error)
+ {
+ return(c(rect_sum, k))
+ }
+ }
+ }
> mid_int <- function(func, lower_lim, upper_lim, error = 0.001, n = 100)
+ {
+ a <- lower_lim; b <- upper_lim
+ integral <- integrate(func, a, b)$value
+ for(k in 1:n)
+ {
+ h <- (b-a)/k
+ y <- rep(0, k)
+ for(j in 1:k)
+ {
+ y[j] <- func(a + (j-1)*h + h/2)
+ }
+ mid_sum <- sum(y)*h
+ if(abs(mid_sum - integral) < error)
+ {
+ return(c(mid_sum, k))

4
+ }
+ }
+ }
> trap_int <- function(func, lower_lim, upper_lim, error = 0.001, n = 500)
+ {
+ a <- lower_lim; b <- upper_lim
+ integral <- integrate(func, a, b)$value
+ for(k in 1:n)
+ {
+ h <- (b-a)/k
+ y <- rep(0, k)
+ for(j in 1:k)
+ {
+ y[j] <- func(a + (j-1)*h)
+ }
+ areas <- rep(0, k-1)
+ for(j in 1:k-1)
+ {
+ areas[j] <- (y[j] + y[j+1])*h/2
+ }
+ trap_sum <- sum(areas)
+ if(abs(trap_sum - integral) < error)
+ {
+ return(c(trap_sum, k))
+ }
+ }
+ }
> simp_int <- function(func, lower_lim, upper_lim, error = 0.001, n = 500)
+ {
+ a <- lower_lim; b <- upper_lim
+ integral <- integrate(func, a, b)$value
+ for(k in 1:n)
+ {
+ h <- (b-a)/k
+ y <- rep(0, k)
+ for(j in 1:k)
+ {
+ y[j] <- func(a + (j-1)*h)
+ }
+ areas <- rep(0, k-1)
+ for(j in 1:k-1)
+ {
+ areas[j] <- (y[j] + y[j+1] + 4*(y[j] + y[j+1])/2)*h/6
+ }
+ simp_sum <- sum(areas)
+ if(abs(simp_sum - integral) < error)
+ {
+ return(c(simp_sum, k))

5
+ }
+ }
+ }
> comparison <- function(func, lower_lim, upper_lim, error = 0.001)
+ {
+ a <- lower_lim; b <- upper_lim
+ integral <- integrate(func, a, b)$value
+ integral
+ data <- data.frame(
+ Method = c("Rectangle", "Midpoint", "Trapezium", "Simpson"),
+ Integral = c(rect_int(func, a, b)[1], mid_int(func, a, b)[1],
+ trap_int(func, a, b)[1], simp_int(func, a, b)[1]),
+ Iterations = c(rect_int(func, a, b)[2], mid_int(func, a, b)[2],
+ trap_int(func, a, b)[2], simp_int(func, a, b)[2])
+ )
+ return(list(integral, error, data))
+ }
> ## Following is the comparison of methods for f1.
> comparison(f1, 1/4, 3/4)

[[1]]
[1] 0.3160499

[[2]]
[1] 0.001

[[3]]
Method Integral Iterations
1 Rectangle 0.3170466 155
2 Midpoint 0.3169430 3
3 Trapezium 0.3150563 147
4 Simpson 0.3150563 147

> ## Following is the comparison of methods for f2.


> comparison(f2, -1, 1)

[[1]]
[1] 0.6205366

[[2]]
[1] 0.001

[[3]]

6
Method Integral Iterations
1 Rectangle 0.6215260 27
2 Midpoint 0.6196326 20
3 Trapezium 0.6215260 27
4 Simpson 0.6196326 20

You might also like