Hi Jacob
I see that nobody, who has the Optimization Toolbox answers.
I may not help you with fsolve, which is in that toolbox,
because I don't have it, however, I could use my function
LMFnlsq.m from the File exchange (Id 17534). I thing that
the solution for fsolve could be similar:
x = LMFnlsq('eqns',x0);
Build a function which evaluates f_i(x), 1<=i<=n to be zero:
function f = eqns(x)
f = [expression for the 1st equation
expression for the 2nd equation
:
expression for the nth equation
imag(x).*w1
(x<0).*x.*w2
];
x is a column vextor, w1, w2 are big enough (1e4?) scalar
weights or columns of vector weights.
I hope it helps.
Mira
Remember that this is over a weekend for those
who might respond, a holiday weekend at that.
The reason why your solutions are going complex
is typically due to a problem of domain. For
example, suppose your function has the variable
a. a is unconstrained, but inside the function
you take log(a). Suppose fsolve tries to evaluate
the objective for negative values of a. SURPRISE!
Complex numbers result. They "contaminate" the
arithmetic, and suddenly fsolve is working in the
complex domain.
One solution is to constrain your parameters.
While fsolve will not accept explicit constraints,
you can use a solver like lsqnonlin.
Alternatively, use a transformation of variables.
If you will compute log(a) inside your objective,
then use a transformation of the variable.
b = log(a)
or, more importantly,
a = exp(b)
Whereas a must be non-negative for happy
completion of fsolve, b is unconstrained.
HTH,
John