From: Alex Thompson on
Hello, I am quite new to matlab and I am trying to troubleshoot my program, without much success. Basically I am trying to use lsqcurvefit to fit a curve to a data set, and each data set will have a constant value named "dip" associated with it. "Dip" is part of the function I am trying to optimize, however it seems that the lsqcurvefit does not recognize it as a already declared value and issues an error saying that it is an undeclared function of variable. I seem to be having a hard time describing this, so here's a portion of my code below.

% Inputs, retrieves most from file called “data”;
zi=data(:,1);
ui=data(:,2);
height= data(1,3);
dip=0.6745*height;

fun = inline('0.23395*(log(zi./p(1)) - ((zi-p(1))./( dip -p(1))).^3./3) + p(2).*sin(pi.*(zi-p(1))./2./( dip - p(1))).^2','p','zi');
p0=[0.1,0.328];
p = lsqcurvefit(fun,p0, zi, ui);

I have tried a couple of things, but they haven't been successful, so any help would be appreciated.
Thanks,
Alex
From: Steven Lord on

"Alex Thompson" <modmyds(a)yahoo.com> wrote in message
news:hu8rp6$kic$1(a)fred.mathworks.com...
> Hello, I am quite new to matlab and I am trying to troubleshoot my
> program, without much success. Basically I am trying to use lsqcurvefit to
> fit a curve to a data set, and each data set will have a constant value
> named "dip" associated with it. "Dip" is part of the function I am trying
> to optimize, however it seems that the lsqcurvefit does not recognize it
> as a already declared value and issues an error saying that it is an
> undeclared function of variable. I seem to be having a hard time
> describing this, so here's a portion of my code below.
>
> % Inputs, retrieves most from file called &#8220;data&#8221;;
> zi=data(:,1);
> ui=data(:,2);
> height= data(1,3);
> dip=0.6745*height;
>
> fun = inline('0.23395*(log(zi./p(1)) - ((zi-p(1))./( dip -p(1))).^3./3) +
> p(2).*sin(pi.*(zi-p(1))./2./( dip - p(1))).^2','p','zi');

Don't use an inline object here. Use an anonymous function instead. The
inline object does not perform the same sort of "capturing" of variables
that appear in their "body" as anonymous functions do.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html#f4-71621

If you're using an older version of MATLAB, one that doesn't have anonymous
functions, have your inline function accept p, zi, and dip as three input
arguments and use the P1, P2, etc. input arguments listed in HELP
LSQCURVEFIT to pass dip in as an additional input argument that LSQCURVEFIT
will automatically pass into your objective function.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com


From: Alex Thompson on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hu91ng$6d1$1(a)fred.mathworks.com>...
>
> "Alex Thompson" <modmyds(a)yahoo.com> wrote in message
> news:hu8rp6$kic$1(a)fred.mathworks.com...
> > Hello, I am quite new to matlab and I am trying to troubleshoot my
> > program, without much success. Basically I am trying to use lsqcurvefit to
> > fit a curve to a data set, and each data set will have a constant value
> > named "dip" associated with it. "Dip" is part of the function I am trying
> > to optimize, however it seems that the lsqcurvefit does not recognize it
> > as a already declared value and issues an error saying that it is an
> > undeclared function of variable. I seem to be having a hard time
> > describing this, so here's a portion of my code below.
> >
> > % Inputs, retrieves most from file called &#8220;data&#8221;;
> > zi=data(:,1);
> > ui=data(:,2);
> > height= data(1,3);
> > dip=0.6745*height;
> >
> > fun = inline('0.23395*(log(zi./p(1)) - ((zi-p(1))./( dip -p(1))).^3./3) +
> > p(2).*sin(pi.*(zi-p(1))./2./( dip - p(1))).^2','p','zi');
>
> Don't use an inline object here. Use an anonymous function instead. The
> inline object does not perform the same sort of "capturing" of variables
> that appear in their "body" as anonymous functions do.
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html#f4-71621
>
> If you're using an older version of MATLAB, one that doesn't have anonymous
> functions, have your inline function accept p, zi, and dip as three input
> arguments and use the P1, P2, etc. input arguments listed in HELP
> LSQCURVEFIT to pass dip in as an additional input argument that LSQCURVEFIT
> will automatically pass into your objective function.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

Dear Mr. Lord,
Thank you very much for your help, I changed my function to an anoymous function and it worked beautifully. I can't really thank you enough, I was trying to make that work for about a week now and all I got was a headache.
Regards,
Alex
From: Alan Weiss on
Try writing your objective function as a function handle:

% Inputs, retrieves most from file called &#8220;data&#8221;;
zi=data(:,1);
ui=data(:,2);
height= data(1,3);
dip=0.6745*height;

fun = @(p)0.23395*(log(zi./p(1)) - ((zi-p(1))./( dip -p(1))).^3./3) +
p(2).*sin(pi.*(zi-p(1))./2./( dip - p(1))).^2;
p0=[0.1,0.328];
p = lsqcurvefit(fun,p0, zi, ui);

For more information, see
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brhkghv-7.html

Alan Weiss
MATLAB mathematical toolbox documentation

On 6/3/2010 2:21 PM, Alex Thompson wrote:
> Hello, I am quite new to matlab and I am trying to troubleshoot my
> program, without much success. Basically I am trying to use lsqcurvefit
> to fit a curve to a data set, and each data set will have a constant
> value named "dip" associated with it. "Dip" is part of the function I am
> trying to optimize, however it seems that the lsqcurvefit does not
> recognize it as a already declared value and issues an error saying that
> it is an undeclared function of variable. I seem to be having a hard
> time describing this, so here's a portion of my code below.
>
> % Inputs, retrieves most from file called &#8220;data&#8221;;
> zi=data(:,1);
> ui=data(:,2);
> height= data(1,3);
> dip=0.6745*height;
>
> fun = inline('0.23395*(log(zi./p(1)) - ((zi-p(1))./( dip -p(1))).^3./3)
> + p(2).*sin(pi.*(zi-p(1))./2./( dip - p(1))).^2','p','zi');
> p0=[0.1,0.328];
> p = lsqcurvefit(fun,p0, zi, ui);
>
> I have tried a couple of things, but they haven't been successful, so
> any help would be appreciated.
> Thanks,
> Alex