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):

install.packages("import")
# install the import package

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


This document was created with rmarkdown 1.2 using the following:



“Use R to solve for the displacements and potential energy (PE) of the two-spring system.” (Chapra 181-183)



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


source("https://raw.githubusercontent.com/ggrothendieck/gsubfn/master/R/list.R")

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)

x
## [1] 4.952302 1.276851
f
## [1] -9.642204



“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



77u/LS0tCnRpdGxlOiAiUG90ZW50aWFsIEVuZXJneSBQcm9ibGVtIFNvbHZlZCBpbiBSIChiYXNlZCBvbiBHTlUgT2N0YXZlL01BVExBQiBFeGFtcGxlKSIKYXV0aG9yOiAiSXJ1Y2thIEVtYnJ5LCBFLkkuVC4iCmRhdGU6ICJgciBTeXMuRGF0ZSgpYCIKb3V0cHV0OgpodG1sX2RvY3VtZW50OgptYXRoamF4OiBkZWZhdWx0Ci0tLQoKPGJyIC8+CjxiciAvPgoKTm90ZTogSWYgeW91IHdpc2ggdG8gcmVwbGljYXRlIHRoZSBSIGNvZGUgYmVsb3csIHRoZW4geW91I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