Professional Documents
Culture Documents
(Problem 1)
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$vectors
3
> # The order isn't same, but the outputs are accurate upto 2 decimal places.
Problem 2
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
[[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