Is it possible to pass fitting coefficients as arguments?

1 view (last 30 days)
Hello everyone,
I'm trying to perform a nonlinear fit via three parameters. However, I have to pass one of the parameters to an external function. Is it possible to do it? How?
Here is the part of my code where I try to do what I explained before.
zeqn = @(coefs, y) coefs(1)+lh+y./coefs(2) + invfwlc((y*coefs(3))/kT);
InitGuess = [100 0.01 10]
fSSR = @(coefs, z, y) sum(( z - zeqn(coefs, y) ).^2);
[MyCoefs, Sr] = fminsearch(@(MyCoefsDummy) fSSR(MyCoefsDummy,z,y),InitGuess,optimset('Display','iter','MaxFunEvals', 50000,'MaxIter',1000,'TolFun',1.e-8,'TolX',1.e-8));
"invfwlc" is the external function I want to evaluate.
Thank you in advanced.
  1 Comment
Star Strider
Star Strider on 21 Feb 2014
Edited: Star Strider on 21 Feb 2014
Once you identify the parameters, passing one shouldn’t be a problem. What have you already tried? Are you having problems with it? Does it give you the answers you expect?
I had to make up values for y, z, lh, and kT, and an expression for invfwlc but with those additions, the code you posted ran without problems.
-------------------------------------------------------------------
Added ahead of posted code:
z = 7; y = 3; lh = 13; kT = 273*1.38E-023; % Necessary to run code
invfwlc = @(x) exp(-x); % Necessary to run code

Sign in to comment.

Answers (1)

Alvaro
Alvaro on 24 Feb 2014
Thank you for your quick response. My main problem is that the function that causes me trouble is the next one:
function lwlc = invfwlc(y)
l = 0.9999;
f2 = @(l) (1./(4.*(1-l).^2)-0.25+l);
df = @(l) (0.5.*(1.0/(1-l).^3)+1.);
lnew = l - 0.5*(f2(l)-y)./(df(l));
while abs(y-f2(l)) > 1.e-8
l = lnew;
lnew = l - 0.5.*(f2(l)-y)./(df(l));
end
lwlc = lnew;
end
It is some kind of numerical solver to invert another function (I don't know if I am explaining well). My purpose is to pass my variable "y" to the function "invfwlc" and get its inverse according to the f2 equation. Doing it, Matlab indicates me that x and y vectors are not of the same length, which is not true, and I can't perform the fit.
Any idea to solve it?
  1 Comment
Star Strider
Star Strider on 25 Feb 2014
Edited: Star Strider on 25 Feb 2014
I’ve been working on this, but I cannot reproduce the error.
Please post the line (or lines) throwing the error. It is obvious that l is a scalar.
Is y a vector?

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!