[R] Fitting problem
pj-uk
paulsouthern at gmail.com
Mon Sep 27 13:13:28 CEST 2010
Hi,
I have a function that generates a set of data but I am having problems
determining the parameters using the nls fitting procedure.
####
"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
}
### I can calculate a set of data:
htest<- (-10:10)*200
dtest<- (5:15)*1e-7
mtest<- MH(field=htest,diameter=dtest)
### However when I try to reverse engineer to calculate the parameters mu,
sig, chi and Ms I run into problems. Could anyone shed some light on this
problem?
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))
### Thanks, Paul
--
View this message in context: http://r.789695.n4.nabble.com/Fitting-problem-tp2715234p2715234.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list