How can I solve the optimal solution of a system of overdetermined nonlinear equations using R

library(BB)
library(nleqslv)
library(reprex)
library(ktsolve)


yfunc<-function(x){
   y<-vector()
   y[1]<-a+b*sin((238.61/365)+c)-(39393*0.00341802+149-273.15)
   y[2]<-a+b*sin((1821.2/365)+c)-(38795*0.00341802+149-273.15)
   y[3]<-a+b*sin((1946.8/365)+c)-(38875*0.00341802+149-273.15)
   y[4]<-a+b*sin((2072.4/365)+c)-(39231*0.00341802+149-273.15)
   y[5]<-a+b*sin((2111.36/365)+c)-(38505*0.00341802+149-273.15)
   y[6]<-a+b*sin((2223.12/3650)+c)-(37962*0.00341802+149-273.15)
   y
}
guess=list(a=8,b=15,c=30)
solv1<-ktsolve(yfunc,guess = guess)
#> Error in ktsolve(yfunc, guess = guess): Fewer guesses than equations.  System is underdefined.

I want to use multiple equations to find the optimal solution of a nonlinear equation. How should I configure the function?

Created on 2021-09-01 by the reprex package (v2.0.1)

Without thinking about the real problem I would do something like the code below.
But you know "without thinking" is dangerous :wink:

yfunc <- function(x){
   a <- x[1] ;  b <- x[2] ;  c <- x[3] ;
   y1 <-a+b*sin((238.61/365)+c)-(39393*0.00341802+149-273.15)
   y2 <-a+b*sin((1821.2/365)+c)-(38795*0.00341802+149-273.15)
   y3 <-a+b*sin((1946.8/365)+c)-(38875*0.00341802+149-273.15)
   y4 <-a+b*sin((2072.4/365)+c)-(39231*0.00341802+149-273.15)
   y5 <-a+b*sin((2111.36/365)+c)-(38505*0.00341802+149-273.15)
   y6 <-a+b*sin((2223.12/365)+c)-(37962*0.00341802+149-273.15) # removed 0 from 3650
   abs(y1) + abs(y2) + abs(y3) + abs(y4) + abs(y5) + abs(y6)  
}

# guess=list(a=8,b=15,c=30)

optim(c(a=8,b=15,c=30),yfunc)
#> $par
#>         a         b         c 
#> 11.864951  4.428084 30.447914 
#> 
#> $value
#> [1] 5.730242
#> 
#> $counts
#> function gradient 
#>      366       NA 
#> 
#> $convergence
#> [1] 0
#> 
#> $message
#> NULL
Created on 2021-09-01 by the reprex package (v2.0.0)

Thank you for your reminder, I will think more about it in the future :wink:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.