# Install and load the required library
install.packages("caTools")
library(caTools)
# Set the parameters for the Mandelbrot set
xlim <- c(-2, 1)
ylim <- c(-1, 1)
resolution <- 1000
max_iter <- 1000
# Create a function to check if a point is in the Mandelbrot set
mandelbrot <- function(z, max_iter) {
c <- z
for (n in 1:max_iter) {
if (Mod(z) > 2) return(n - 1)
z <- z^2 + c
}
return(max_iter)
}
# Generate grid points in the specified range
x <- seq(xlim[1], xlim[2], length.out = resolution)
y <- seq(ylim[1], ylim[2], length.out = resolution)
# Create an empty matrix to store the results
result <- matrix(0, nrow = length(x), ncol = length(y))
# Perform the Mandelbrot set calculations
for (i in 1:length(x)) {
for (j in 1:length(y)) {
result[i, j] <- mandelbrot(complex(real = x[i], imaginary = y[j]), max_iter)
}
}
# Plot the Mandelbrot set
image(x, y, result, col = terrain.colors(max_iter), xlab = "Re", ylab = "Im", main = "Mandelbrot Set")