Pada code berikut ini persamaan yang dipakai adalah m * d^2x/dt^2 + k * x = 0. Sebelum running program, pastikan sudah menginstall package deSolve.
library(deSolve)
# Function defining the ODE system
ode_function <- function(t, y, parameters) {
# Extract the variables
x <- y[1]
v <- y[2]
# Extract the parameters
k <- parameters$k
m <- parameters$m
# Definisikan ODEs
dxdt <- v # dx/dt = v
dvdt <- -k/m * x # dv/dt = -k/m * x
# Return the derivatives
return(list(c(dxdt, dvdt)))
}
# Function defining the ODE system
ode_function <- function(t, y, parameters) {
# Extract the variables
x <- y[1]
v <- y[2]
# Extract the parameters
k <- parameters$k
m <- parameters$m
# Definisikan ODEs
dxdt <- v # dx/dt = v
dvdt <- -k/m * x # dv/dt = -k/m * x
# Return the derivatives
return(list(c(dxdt, dvdt)))
}
# Tetapkan kondisi awal dan parameter
initial_conditions <- c(x = 1, v = 0) # posisi awal san kecepatan
parameters <- list(k = 1, m = 1) # kontstanta pegas dan massa
# Set time points for integration
time_points <- seq(0, 10, by = 0.1)
# Selesaikan sistem ODE dengan metode beda hingga
solution <- ode(initial_conditions, time_points, ode_function, parameters)
# Extract the position values from the solution
positions <- solution[, "x"]
# Plot hasil simulasi
plot(time_points, positions, xlab = "Time", ylab = "Position", type = "l", main = "Simulation Results")
initial_conditions <- c(x = 1, v = 0) # posisi awal san kecepatan
parameters <- list(k = 1, m = 1) # kontstanta pegas dan massa
# Set time points for integration
time_points <- seq(0, 10, by = 0.1)
# Selesaikan sistem ODE dengan metode beda hingga
solution <- ode(initial_conditions, time_points, ode_function, parameters)
# Extract the position values from the solution
positions <- solution[, "x"]
# Plot hasil simulasi
plot(time_points, positions, xlab = "Time", ylab = "Position", type = "l", main = "Simulation Results")
Tidak ada komentar:
Posting Komentar