Help in R with Error: 'Cannot evaluate function at initial parameters


I have a really difficult topic (for me its difficult, because im a newbie in R).
I want to optimize a function so i can make a matrix between two coordinate Systems (Lab and RGB).

Here is my code:
setwd("C:/Users/Tobi_Laptop/Documents/Tobi/Studium/6. Semester/Farbmetrik/Projekt/Umrechnung")
Messwerte <- read.csv2("Messwerterot.csv", sep=";", dec=",")

DeltaL_lin1_fun <- function(Lab_Werte, par){with(Lab_Werte, sum((Messwerte$L99o.soll - (Messwerte$R*par[1]+Messwerte$G*par[2] + Messwerte$B*par[3]))^2))}
L_lin1_opt <- optimx(par=c(1,1,1), fn=DeltaL_lin1_fun, Lab_Werte=Messwerte,mehtod="BGFS")

Deltaa_lin1_fun <- function(Lab_Werte, par){with(Lab_Werte, sum((Messwerte$a99o.soll - (Messwerte$R*par[1]+Messwerte$G*par[2] + Messwerte$B*par[3]))^2))}
a_lin1_opt <- optimx(par=c(1,1,1), fn=Deltaa_lin1_fun, Lab_Werte=Messwerte,mehtod="BGFS")

Deltab_lin1_fun <- function(Lab_Werte, par){with(Lab_Werte, sum((Messwerte$b99o.soll - (Messwerte$R*par[1]+Messwerte$G*par[2] + Messwerte$B*par[3]))^2))}
b_lin1_opt <- optimx(par=c(1,1,1), fn=Deltab_lin1_fun, Lab_Werte=Messwerte,mehtod="BGFS")

M_lin1<-matrix(c(L_lin1_opt$p1,L_lin1_opt$p2,L_lin1_opt$p3,a_lin1_opt$p1,a_lin1_opt$p2,a_lin1_opt$p3,b_lin1_opt$p1,b_lin1_opt$p2,b_lin1_opt$p3), nrow=3, byrow=TRUE)
The Line with L_lin_opt gets the Error:
'Error in optimx.check(par, optcfg$ufn, optcfg$ugr, optcfg$uhess, lower, : Cannot evaluate function at initial parameters'

Thanks for the fast answer :)

I could not upload my RMD file or my original excel file to this site so you sadly not have the data.
Last edited by a moderator:


Ambassador to the humans
You need to write you function so that first input is par. You also misspelled "method" but I don't think that's the issue.