Replicate the R code

Install the packages

Note: If you wish to replicate the R code below, then you will need to copy and paste the following commands in R first to make sure you have all the packages and their dependencies installed:

install.packages(c("import", "gsubfn", "pracma", "pander"))
# install the packages and their dependencies



Load the packages

Note: If you wish to replicate the R code below, then you will need to copy and paste the following commands in R first to make sure you have all the packages loaded:

# load the required packages
install.load::load_package("pander", "gsubfn")
# load needed packages using the load_package function from the install.load
# package (it is assumed that you have already installed these packages)


# set the pander options
panderOptions("table.continues", "")
panderOptions("table.caption.prefix", "")
panderOptions("missing", "")


import::from(pracma, fminsearch)
# import fminsearch from the pracma package



Find the Solution to the Two-Spring System

"Use R to solve for the displacements and potential energy (PE) of the two-spring system."

From Chapra 181-183



PE <- function(x, ka, kb, La, Lb, F1, F2) {
    a <- 0.5 * ka * (sqrt(x[1]^2 + (La - x[2])^2) - La)^2
    b <- 0.5 * kb * (sqrt(x[1]^2 + (Lb + x[2])^2) - Lb)^2
    W <- F1 * x[1] + F2 * x[2]
    p <- a + b - W
    return(p)
}

ka <- 9
kb <- 2
La <- 10
Lb <- 10
F1 <- 2
F2 <- 4

list[x, f] <- fminsearch(PE, c(-0.5, 0.5), ka, kb, La, Lb, F1, F2)

pander(x)

4.952 and 1.277

pander(f)

-9.642



"At equilibrium, the potential energy is -9.6422 N \(\cdot\) cm & the connecting point is located 4.9523 cm to the right and 1.2769 cm above its original position."



Works Cited

Steven C. Chapra, Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd Edition, Boston, Massachusetts: McGraw-Hill, 2008, pages 181-183.

Wikimedia Foundation, Inc. Wikipedia, 17 July 2016, Potential energy



Donations accepted with Liberapay

If you would like to contribute to the continued development of Irucka Embry’s R packages and/or Irucka Embry’s R Examples, please feel free to donate via the link below:

Please feel free to review Irucka Embry (iaembry)’s profile on Liberapay.