fork download
  1.  
  2. # Load necessary library
  3. library(uniroot)
  4.  
  5. # Define a function to represent the equation being solved
  6. polar_moment_equation <- function(di, D) {
  7. J_solid <- (pi / 32) * D^4
  8. J_hollow <- (pi / 32) * (D^4 - di^4)
  9. J_rectangular <- (1 / 12) * di
  10.  
  11. return(J_solid - (J_hollow + J_rectangular)) # This should equal 0
  12. }
  13.  
  14. # Define parameters
  15. D_min <- 1 # Minimum possible value for diameter (length of the plate)
  16. D_max <- 2 # Maximum possible value for diameter
  17. D_avg <- (D_min + D_max) / 2 # Choose an average value for D
  18.  
  19. # Use uniroot to find a root of the equation within a reasonable range for di
  20. di_solution <- uniroot(polar_moment_equation, c(0, D_avg), D = D_avg)
  21.  
  22. # Output the inner diameter di
  23. cat("The inner diameter di of the hollow circular shaft is:", di_solution$root, "\n")
  24.  
Success #stdin #stdout #stderr 0.23s 40688KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(uniroot) : there is no package called ‘uniroot’
Execution halted