# Compare nonlinear system to linearized system # First, simulate the nonlinear system ###################################### # Define the time of simulation maxTime <- 10 dt <- 0.01 # Precision of simulation # Prepare variable for results of simulation ts <- seq(0,maxTime,dt) N <- length(ts) # Number of samples x <- numeric( N ) # The results will go here x[1] <- 0.01 # Initial condition for (t in 2:N) { x[t] <- x[t-1] + dt*( sin(x[t-1] ) ) } # Plot the nonlinear system matplot( ts, x, xlab = "Time", ylab = "x", type = "l",col="red",add = FALSE ) # Now, simulate the linearized system ###################################### # This asks the user to click mouse on the curve xy <- locator( n=1 ) # The first value is time, the second is x t0 <- xy[[1]] x0 <- xy[[2]] # Time axis ts <- seq(t0,maxTime,dt) # Starting from t0 !!! # Analytic formula xPred = pi + (x0-pi)*exp(-(ts-t0)) # Plot matplot( ts, xPred, type = "l",col="green",add = TRUE )