#### Updated! ####
"MH"<-function(field,diameter,mu=10e-7,sig=0.1,Ms=100,chi=0){ #variables mu, sig, chi, Ms #input: field and diameter #all in CGS rho <- 5 kb <- 1.38e-16 t <- 300 length.d<-length(diameter) length.H<-length(field) M<-double(length.H) for (i in 1:length.H){ S1<-0 S2<-0 H <- field[i] for (j in 1:length.d){ d<-diameter[j] vol <- 4/3*pi*(d/2)^3 lognorm <- 1/(d*sig*sqrt(2*pi))*exp(-(log(d)-log(mu))^2/(2*sig^2)) lang <- 1/tanh(Ms*rho*vol*H/(kb*t))-1/(Ms*rho*vol*H/(kb*t)) S1 <- S1 + lognorm*vol*lang S2 <- S2 + lognorm*vol } M[i] <- Ms*S1/S2 + chi*H } M } ### htest<- (-10:10)*200 dtest<- (5:15)*1e-7 mtest<- MH(field=htest,diameter=dtest) + rnorm(21) ##also added some noise!! df <- data.frame(H=htest,M=mtest) fit <- nls(M~MH(H,(5:15)*1e-7,mu,sig,Ms,chi),data=df,start=list(mu=10e-7, sig=0.1, chi=0, Ms=100)) -- View this message in context: http://r.789695.n4.nabble.com/Fitting-problem-tp2715234p2715350.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.