переименуй переменные в данном коде extr <- function(x,y){ r <- sqrt(x^2+y^2) return((…
- User
переименуй переменные в данном коде
extr <- function(x,y){ r <- sqrt(x^2+y^2) return(( 10 * sin(r) * cos(r) ) / r ) }
#рисуем график x <- seq(-12,12,by <- 0.2) y <- x z <- outer(x,y,extr) mt <- persp(x,y,z)
a <- c(0,2) h <- 1 d <- 0.001 ex <- c(1,0) ey <- c(0,1) fr <- function(x){ r <- sqrt(x[1]^2+x[2]^2) return(( 10 * sin(r) * cos(r) ) / r) } poisk <- function(fr,a,h,d,ex,ey){ while(h>d){ a1 <- a if(fr(a+hex)<fr(a)){ a1 <- a+hex } else if(fr(a-hex)<fr(a)){ a1 <- a-hex } else if(fr(a+hey)<fr(a)){ a1 <- a+hey } else if(fr(a-hey)<fr(a)){ a1 <- a-hey } if(all(a1 == a)){ h <- h / 2 a <- a1 } else { while(fr(2a1 - a) < fr(a1)){ a <- a1 a1 <- 2a1 - a } a <- a1 } } return(a) } poisk(fr,a,h,d,ex,ey) q <- poisk(fr,a,h,d,ex,ey)
t <- q[1] u <- q[2] v <- extr(t,u) point <- c(t,u,v) xy <- trans3d(point[1],point[2],point[3],mt) points(xyy,xy$z,col='red')
- Assistant
extr_func <- function(x_coord, y_coord){ r <- sqrt(x_coord^2 + y_coord^2) return((10 * sin(r) * cos(r)) / r) }
Создаем график
x_val <- seq(-12, 12, by = 0.2) y_val <- x_val z_val <- outer(x_val, y_val, extr_func) mt <- persp(x_val, y_val, z_val)
initial_point <- c(0, 2) step <- 1 tolerance <- 0.001 direction1 <- c(1, 0) direction2 <- c(0, 1)
Функция определения минимума методом Нелдера-Мида
find_minimum <- function(func, point, step_size, tolerance, dir1, dir2){ while(step_size > tolerance){ temp_point <- point if(func(point + step_size * dir1) < func(point)){ temp_point <- point + step_size * dir1 } else if(func(point - step_size * dir1) < func(point)){ temp_point <- point - step_size * dir1 } else if(func(point + step_size * dir2) < func(point)){ temp_point <- point + step_size * dir2 } else if(func(point - step_size * dir2) < func(point)){ temp_point <- point - step_size * dir2 } if(all(temp_point == point)){ step_size <- step_size / 2 point <- temp_point } else { while(func(2 * temp_point - point) < func(temp_point)){ point <- temp_point temp_point <- 2 * temp_point - point } point <- temp_point } } return(point) }
Находим минимум
minimum_point <- find_minimum(extr_func, initial_point, step, tolerance, direction1, direction2) q1 <- minimum_point[1] q2 <- minimum_point[2] q3 <- extr_func(q1, q2)
Отображаем найденную точку на графике
point <- c(q1, q2, q3) xy <- trans3d(point[1], point[2], point[3], mt) points(xyy, xy$z, col = 'red')