Is it possible to use the Matlab function "lsqcurvefit" to this
purpose? Which is, in detail, the way to do it?
Is there any other way to solve this curve fitting?
Thanks
Simone
>> help lsqcurvefit
LSQCURVEFIT Solves non-linear least squares problems.
LSQCURVEFIT solves problems of the form:
min sum {(FUN(X,XDATA)-YDATA).^2} where X, XDATA, YDATA and the values
X returned by FUN can be vectors or
matrices.
X=LSQCURVEFIT(FUN,X0,XDATA,YDATA) starts at X0 and finds
coefficients X to best fit the nonlinear function FUN(X,XDATA)
to the data YDATA (in the least-squares sense). FUN is an M-file
that computes a function of X and XDATA and returns a matrix of
the objective function values: F=FUN(X,XDATA).
NOTE: YDATA must be the same size as the matrix F returned by FUN.
X=LSQCURVEFIT(FUN,X0,XDATA,YDATA,LB,UB) defines a set of lower and upper
bounds on the design variables, X, so that the solution is in
the range LB <= X <= UB. Use empty matrices for LB and UB
if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below;
set UB(i) = Inf if X(i) is unbounded above.
X=LSQCURVEFIT(FUN,X0,XDATA,YDATA,LB,UB,OPTIONS) minimizes with the default
parameters replaced by values in the structure OPTIONS, an argument
created with the OPTIMSET function. See OPTIMSET for details. Used
options are Display, TolX, TolFun, DerivativeCheck, Diagnostics, Jacobian,
JacobPattern, LineSearchType, LevenbergMarquardt, MaxFunEvals, MaxIter,
DiffMinChange and DiffMaxChange, LargeScale, MaxPCGIter, PrecondBandWidth,
TolPCG, TypicalX. Use the Jacobian option to specify that FUN may be
called with two output arguments where the second, J, is the Jacobian
matrix: [F,J] = feval(FUN,X,XDATA). If FUN returns a vector (matrix) of m
components when X has length n, then J is an m-by-n matrix where J(i,j)
is the partial derivative of F(i) with respect to x(j). (Note that the
Jacobian J is the transpose of the gradient of F.)
X=LSQCURVEFIT(FUN,X0,XDATA,YDATA,LB,UB,OPTIONS,P1,P2,..) passes the
problem-dependent parameters P1,P2,... directly to the function FUN:
FUN(X,XDATA,P1,P2,...). Pass empty matrices for OPTIONS to use the
default values.
[X,RESNORM]=LSQCURVEFIT(FUN,X0,XDATA,YDATA,...) returns the value of the
squared 2-norm of the residual at X: sum {(FUN(X,XDATA)-YDATA).^2}.
[X,RESNORM,RESIDUAL]=LSQCURVEFIT(FUN,X0,...) returns the value of residual,
FUN(X,XDATA)-YDATA, at the solution X.
[X,RESNORM,RESIDUAL,EXITFLAG]=LSQCURVEFIT(FUN,X0,XDATA,YDATA,...) returns
a string EXITFLAG that describes the exit condition of LSQCURVEFIT.
If EXITFLAG is:
> 0 then LSQCURVEFIT converged to a solution X.
0 then the maximum number of function evaluations was reached.
< 0 then LSQCURVEFIT did not converge to a solution.
[X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT]=LSQCURVEFIT(FUN,X0,XDATA,YDATA,...)
returns a structure OUTPUT with the number of iterations taken in
OUTPUT.iterations, the number of function evaluations in OUTPUT.funcCount,
the algorithm used in OUTPUT.algorithm, the number of CG iterations (if used) in
OUTPUT.cgiterations, and the first-order optimality (if used) in
OUTPUT.firstorderopt.
[X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT,LAMBDA]=LSQCURVEFIT(FUN,X0,XDATA,YDATA,...)
returns the set of Lagrangian multipliers, LAMBDA, at the solution:
LAMBDA.lower for LB and LAMBDA.upper for UB.
[X,RESNORM,RESIDUAL,EXITFLAG,OUTPUT,LAMBDA,JACOBIAN]=LSQCURVEFIT(FUN,X0,XDATA,YDATA,...)
returns the Jacobian of FUN at X.
hence the answer is "yes"
hth
peter
When I write the function, I refer to xdata using xdata( :,1) or xdata
( :,2) or xdata( :,3) and it seems to work.
Es: f=x(1)*xdata(:,1)+x(2)*xdata(:,2) ….
The function can't be written in an explicit form, but I have to
express a condition on one of the input:
if xdata(:,1)>0
y=....
elseif ...
y=...
else y=...
end
when I use: if xdata(:,1)>0 evaluates as false
when I use: if xdata(1)>0 only xdata(1,1) is evaluated
I understand that this is correct during the “standard” use of these
expressions.
What is the solution of this problem?
Thanks
One way is to do something like this:
ind = xdata(:,1) > 0;
y(ind) = xdata(ind,1)+... (or whatever, using ind to pick up the rows)
ind = xdata(:,1) == 0;
y(ind) = some-other-formula
ind = some-other-logical-expression
etc.
In other words, do not use the logical expression
with if, use it to construct the indexes that satisfy
it, and then calculate the y values just for those
rows. Repeat for as many cases as you like.
It might make sense to initialize y, so that you get
it to be the correct size from the start, for example
y = ones(size(xdata(:,1))) or something like that.
Hope this helps
Jukka