### MATLAB >> Problem with lsqcurvefit

Hello!

I have a problem with lsqcurvefit. The goal is to find the right
parameters in three paralell ODE's to fit my data.

My initial guess is the following:

x0 = [50000, 50000, 3.5000, 0.1000;
9.85e+015, 145000, 4.0000, 0.96;
1.20e+015, 155000, 36.000, 0.8000];

It seams as the larger values do not change during optimization. I have
read that you could use the 'TypicalX' option but it seams this is of
no help.

Is there any other possibility to scale the data in some way?

/Dan

### MATLAB >> Problem with lsqcurvefit

Hey, I hope this is a really simple problem, it's my first time using matlab so sorry if I'm missing something obvious! Any help is much appreciated,
Thanks

This is my function

function f = myfun(x,xdata)
F = real((1/((1/x(1))+(((2*pi*xdata)*(x(2)*(x(3)/x(4))))/(-(sqrt(-1)))))+(1/((2*pi*xdata)*(2*(x(2)*(x(3)*(1/x(4))))))))

and this is the code im using to call it

x0 = [10; 10; 10; 10] % Starting guess
[x,resnorm] = lsqcurvefit(@myfun,x0,freq1,imp1)

I then get the following error,

??? Error using ==> lsqncommon at 101
LSQCURVEFIT cannot continue because user supplied objective function failed
with the following error:
Error using ==> feval
Error: File: myfun.m Line: 2 Column: 120
Expression or statement is incorrect--possibly unbalanced (, {, or [.

Error in ==> lsqcurvefit at 186
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

I'm trying to fit the data in imp1 to the output of my function F with inputs freq1

Cheers
Ben

### MATLAB >> Problem with lsqcurvefit

Both the name of the return parameter must consistent (Matlab is case
sensitive). So you want to write either

function f = myfun(x, xdata)
f = real(...)

or

function F = myfun(x, xdata)
F = real(...)

Also, a right parenthesis is missing at the end of the function definition.

function f = myfun(x,xdata)
f =
real((1/((1/x(1))+(((2*pi*xdata)*(x(2)*(x(3)/x(4))))/(-(sqrt(-1)))))+(1/((2*pi*xdata)*(2*(x(2)*(x(3)*(1/x(4)))))))))

Regards,
-- Jean-Marc

### MATLAB >> Problem with lsqcurvefit

Hello,

I am trying to use lsqcurvefit but I get the following error:

"??? Error using ==> C:\matlabR12\toolbox\optim\private\lsqncommon
User supplied function failed with the following error:

Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
same number of rows.

Error in ==> C:\matlabR12\toolbox\optim\lsqcurvefit.m
On line 129 ==> [x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ..."

The code of my function is as follows:

"function y = idealcurve(x,xdata)

len = length(xdata);

diff1 = xdata-x(3);
index1 = find(diff1>=0);
diff1 = diff1(index1(1):length(diff1));

if length(diff1)<len
difference = len -length(diff1);
end

curve1 = ((x(1)*(exp(diff1./x(2))-1)));

curve2 = x(4)*(xdata.^x(5));

y = curve1.^x(6)+curve2;"

I am at a lost as to why I am getting the error message and would appreciate any insights that may be offered.

### MATLAB >> Problem with lsqcurvefit

Hello Gerard
The error message obviously indicates that the objective function have errors . I would suggest you call the objective function with a sample 'x' and 'xdata' and debug it. I can tell one thing about the objective (when I tried)
- diff1 = diff1(index1(1):length(diff1)); % depends on 'index1' being non empty but it is not guaranteed. This may result in error.

Hth,
Rakesh

### MATLAB >> Problem with lsqcurvefit

This is true, but I would expect/hope that if someone saw a function name
they didn't recognize in an error message they'd look for HELP on it, and
the help for HORZCAT (which hasn't changed in quite a while) starts:

HORZCAT Horizontal concatenation.
[A B] is the horizontal concatenation of matrices A and B. A and B
must have the same number of rows. [A,B] is the same thing. Any
number of matrices can be concatenated within one pair of brackets.
Horizontal and vertical concatenation can be combined together as in
[1 2;3 4].

Perhaps my expectation is not correct -- what do you think?

--
Steve Lord
XXXX@XXXXX.COM
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

### MATLAB >> Problem with lsqcurvefit

Hello,

I have already tried this, multiple times with different data sets and the function works okay when called on its own. The problem only seems to occur when called via lsqcurvefit.

Regards,

Gerard

### MATLAB >> Problem with lsqcurvefit

Hello,

I was aware of horzcat before this problem, the function works correctly when called using the function name. As I mention above the error only seems to occur when the function is called using the handle with lsqcurvefit.

Since then I have stepped through lsqcurvefit and I have noticed that the error occurs when the I called the function via lsqncommon.

The error states:
"??? Error: Functions cannot be indexed using {} or . indexing."

### MATLAB >> Problem with lsqcurvefit

...
...

My experience is that inexperienced users don't seem to react as
logically as we would like and that they tend to just react rather than
think. Not sure there's much can be done about that, unfortunately.
Having spent multitude of years in a development group for large nuclear
design codes for the production/design people, the ability to write help
and code error messages that will be able to keep all users able to
solve their own problems doesn't seem to exist... :) (or :(, I'm not sure).

I see OP doesn't quite fall into that; I wrote the answer on an
assumption not true in this case, apparently.

### MATLAB >> problem with lsqcurvefit

i'm getting this error while using lsqcurvefit

Optimisation terminated: norm of the current step is less than OPTIONS.TolX

Any suggestions?

Best Regards

### MATLAB >> problem with lsqcurvefit

i've tried to change the value of TolX via optimset and i'm still getting the same error.

i've tried 1e-20 1e-6 1 e-4 and still the same error.

Any suggestions???

### MATLAB >> problem with lsqcurvefit

Why do you presume it is an error?

"terminated" is a synonym for "done".

John

### MATLAB >> problem with lsqcurvefit

Hi, I am sorry that this message confused you. This is NOT an error
message. Instead, it is simply a message telling you the reason
lsqcurvefit stopped. It is likely that your results are good. For more
information on checking to see if you results are OK, see
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/br44i2r.html
and look at "Local minimum possible."

Exit messages are much more friendly and clear in recent versions of
MATLAB. For lsqcurvefit, the messages were changed in R2009b.

Alan Weiss
MATLAB mathematical toolbox documentation

### MATLAB >> problem with lsqcurvefit

i wrote the following M.file

%--------------------------------------------------------------------------
% @description: Attempt to estimate the parameter of the market price
% of risk, lamda of the Vasicek model 1977 of the term structure
%model of yields. We do this by using curve fitting to the observed data.
%
% Xdata: - vector of times to maturity .
% ydata - vector of observed yields-to-maturity that correspond to
% the matching value by the yields-to-maturity correspinf to
% the vasicek model wich has the following form:
% R(T) = R(alpha) +(r0 - (beta - R(alpha)) *
% (1- exp (- beta*T))/ (beta *T) +
% ((sigma^2)*(1- exp (- beta*T))^2)/(4*(beta^3)*T).
%
%
%where R(alpha)= (beta - ((lamda * sigma)/ alpha)- ((sigma^2)/ (2*(alpha^2)))
% and T is time to maturity.
%
% xdata = [0.5 1 2 3 5 7 10];
% ydata =[0.079 0.0782 0.0791 0.0793 0.0791 0.0802 0.0798];
%--------------------------------------------------------------------------

xdata = ...
[0.5 1 2 3 5 7 10];
ydata = ...
[0.079 0.0782 0.0791 0.0793 0.0791 0.0802 0.0798];
alpha = 0.051356;
beta = 0.1374;
sigma = 0.0118;
r = 0.012;
lamda0 = - 0.031; % Starting guess%
Function F = myfun(lamda,xdata);
F = Ralpha +((1-exp(-beta*xdata))/(beta*xdata))*(r-Ralpha)+...
((sigma.^2)/(4*(beta.^3)*xdata)*((1-exp(-xdata)).^2));

Ralpha =(beta -(lamda*sigma)/alpha)-(sigma.^2)/(2*(alpha.^2));
[x,resnorm] = lsqcurvefit(@myfun,lamda0,xdata,ydata);
plot (xdata,ydata,lsqcurvefit);

-----------------------------
but i get the following answer in the workspace

??? Attempt to execute SCRIPT function as a function:
C:\Program Files\MATLAB\R2009a\toolbox\matlab\lang\function.m

Error in ==> fit at 33
Function F = myfun(lamda,xdata);

please can you help me and tell me where is the problem

### MATLAB >> problem with lsqcurvefit

There are two problems here. The first is that this appears to be a script

*snip*

is that when you use the keyword function to start a function declaration,
it must be written in all lowercase.

*snip*

--
Steve Lord
XXXX@XXXXX.COM
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

```Hi

I have a dataset for x,y which I need to fit with following equation(y=f(x,y)).
y=(4.1/lp)*((1./(4*(1-(x/L-(y/K0))).^2))+x/L-(y/K0)-0.25);
where lp, L and K0 are the parameters used for optimization.

I am using lsqcurvefit to ooptimize the parameters and here is my .m file.

x=l(:,1);
y=l(:,2);
data=[x y];
zdata=y;
c0=[0.7 1 50000];
F=(4.1/c0(1))*((1./(4*(1-(x/c0(2))+(y/c0(3))).^2))+(x/c0(2))-(y/c0(3))-0.25);
[c, resnorm]=lsqcurvefit(@myfun,c0,data,zdata)
end

However, when I run this, it generates following error:

?? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit.  Be aware that exceeding your available stack space can

Error in ==> optimget

Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.

I changed the recursion limit to 5000 and run. Unfortunately this crashes the matlab.

Looking for help.

Thank you

rakshit
```

```I'm running Matlab 6.5 (Student Version) and the Optimization Toolbox
version 2.2

When I run lsqcurvefit on my data, with no options specified, I get the
message:

Optimization terminated successfully:
Relative function value changing by less than OPTIONS.TolFun

When I use the "optimset" command to set the Display option:

options = optimset('Display','notify');
x = lsqcurvefit(f,x0,x1data,ydata,lb,ub,options);

I get the same "Optimization terminated successfully" message.

I thought that setting "Display" to "notify" meant that I'd only get
output if the function failed to converge, and "Optimization terminated
successfully" would seem to indicate that the function *did* converge.

Am I misunderstanding the meaning of "Optimization terminated successfully"?

Am I misunderstanding what "Display = notify" does?

Is this a bug?

FWIW, "Display = final", "Display = iter" and "Display = off" appear to
work as expected.   Note that "Display = final" and "Display = notify"
give the same result.

Bob Pownall
```

```I have a set of (x,y) that I want to fit using the allometric relation:

y = a*(y-b)^c

so I would need to estimate 3 variables (a,b,c). However, after modifying numerous online examples and doing my own, I always get the QR error about complex values (even when I set the upper and lower bounds to real numbers).
For example:

xdata=[0.006828287172237330
0.790682195827279000
0.003735589633394300
0.588505280987413000
0.049935384458074200
0.249101128117032000
];

ydata=[0.000422158958434916
0.476084662925104000
-0.003271705711918840
0.335717811245073000
0.006198209732454980
0.062046706155897800
];

predicted = @(a,x) a(1)*(x - a(2)).^a(3);
a0=[2;2;2];
[ahat,resnorm]=lsqcurvefit(predicted,a0,xdata,ydata);

------> Results in:
"??? Error using ==> qr
Complex sparse QR is not yet available. ..."

Any ideas? Did I use this incorrectly?
Eric
```

```i'm trying to fit two curves using the lsqcurvefit.
to do that i need to define a function

predicted=@(x,Xdata_3500)
x(1)*x(2)*(x(3)+1)*(((Xdata_3500-(-2.5))./x(4)).^(x(3))).*exp(-x(2)*(((Xdata_3500-(-2.5))./x(4)).^(x(3)+1)))+(1-x(1))*x(5)*(x(6)+1)*(((Xdata_3500-(-2.5))./x(7)).^(x(6))).*exp(-x(5)*(((Xdata_3500-(-2.5))./x(7)).^(x(6)+1))) ;
x0=[0.5;5;2;10;6;1;80];
[ahat,resnorm,residual,exitflag,output,lambda,jacobian]=...
lsqcurvefit(predicted,x0,Xdata_3500,Ydata_3500);

i've noticed that each time i change x0 values and run lsqcurvefit again, i'm obtaining new parameters for ahat.

although the 2 curves are well coherent, i have constraints concerning the parameters, and i need them to respect certain intervalls.