Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.14 KB | None | 0 0
  1. a <- 0
  2. b <- 1
  3. eps <- 0.001
  4.  
  5. H <-  function(x,y){
  6.   return (- 0.1 * sin (x * (0.5 + y * y)))
  7. }
  8.  
  9. f <- function(x){
  10.   return (x + 0.1)
  11. }
  12.  
  13. Integ <- function(x,res,num){
  14.   A <- numeric(length(x))
  15.   A[c(1,n)] <- (x[2]-x[1])/2
  16.   A[2:(n - 1)] <- (x[2]-x[1])
  17.   result <- numeric(1)
  18.   for(i in 1:length(x))
  19.     result <- result + A[i]*H(res[num],x[i])*res[i]
  20.   return (result)
  21. }
  22.  
  23. n <- 5
  24. repeat {
  25.   h <-  (b - a)/(n - 1)
  26.  
  27.   x <- numeric(n)
  28.   for(i in 1:n)
  29.     x[i] <- a + h * (i - 1)
  30.  
  31.   f_mat <-  matrix(f(x))
  32.  
  33.   H_mat <- matrix(nrow = n, ncol = n)
  34.   for(i in 1:n)
  35.     for(j in 1:n)
  36.       H_mat[i,j] <-  H(x[i],x[j])
  37.  
  38.   A <- numeric(n)
  39.   A[1] <- h/2
  40.   A[n] <- h/2
  41.   A[2:(n - 1)] <- h
  42.  
  43.   D <- matrix(nrow = n, ncol = n)
  44.   for(i in 1:n)
  45.     for(j in 1:n)
  46.       D[i,j] <- ifelse(i == j, 1, 0) - A[j] * H(x[i],x[j])
  47.  
  48.   res <- solve(D, f_mat)
  49.   finish <- T
  50.   print(n)
  51.   for(i in 1:3){
  52.     num <- 1 +(i - 1)*(n - 1)/2
  53.     finish <- finish & (abs(res[num] - Integ(x, res, num)-f(res[num])) < eps)
  54.     print(res[num])
  55.     print(abs(res[num] - Integ(x, res, num)-f(res[num])))
  56.   }
  57.   if(finish)
  58.     break
  59.   n <- (n - 1) * 2 + 1
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement