lsqcurvefit help - Field assignment to a non-structure array object

Hi,
I am getting the error "Field assignment to a non-structure array object" in the line I call the lsqcurvefit function and I don't understand why.
I try to fit 2 different models. When I fit model_1 everything is fine. When I fit model_2 I got the error.
a, b - both vectors of size [1,41].
I have the following code:
opts = optimset('Display' ,'off');
for i=1:size(Image,1)
x(i,:) = lsqcurvefit (@model_1, init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
for i=1:size(Image,1)
x(i,:) = lsqcurvefit (@model_2, init, a(1:end-1), b(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
Why do I get the error when I have one more entry in model_2? i.e. the vector b?
function y = model_2(p, a, b)
A = p(1);
B = p(2);
C = p(3);
D = p(4);
E = p(5);
y = A*exp(-a*C-b/D)+(1-A)*exp(-a*B).*(E*exp(-b/100)+(1-E)*exp(-b/40));
end
function y = model_1(p,a)
A = p(1);
B = p(2);
C = p(3);
y = A*exp(-B*a) + (1-A)*exp(-C*a);
end

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 1 月 14 日
編集済み: Walter Roberson 2019 年 1 月 15 日

0 投票

lsqcurvefit must have parameter order
  1. objective. @model_1 or @model_2
  2. x0. init in both cases
  3. xdata. a(1:end-1) in both cases
  4. ydata. Image(1:end-1) in the first case and b(1:end-1) in the second case
  5. lb. zeroes in the first case and Image(1:end-1) in the second case
  6. ub. init*10 in the first case and zeroes in the second case
  7. options. opt (a struct) in the first case and zeroes in the second case
  8. no documented parameter . absent in the first case and opt (a struct) in the second case.
Internally the code attempts to add additional fields to the struct expected in the 7th position and fails when the parameter is numeric zeroes .

16 件のコメント

Mar
Mar 2019 年 1 月 14 日
編集済み: Mar 2019 年 1 月 15 日
The model_2 takes 2 inputs a and b. I edited my question and I added that information as well. How can I overcome this problem? Y data in both cases should be Image(1:end-1). But model_2 needs both a and b vectors.
Walter Roberson
Walter Roberson 2019 年 1 月 14 日
lsqcurvefit is not designed for surface fitting. however you could probably parameterize
lsqcurvefit(@(p,a) model_2(p,a,b(1:end-1)) ....
Mar
Mar 2019 年 1 月 15 日
編集済み: Mar 2019 年 1 月 15 日
How can I define the initial values if I parametrize it? I am pretty new to coding..
Torsten
Torsten 2019 年 1 月 15 日
for i=1:size(Image,1)
x(i,:) = lsqcurvefit (@(p,a)model_2(p,a,b), init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
Mar
Mar 2019 年 1 月 15 日
編集済み: Mar 2019 年 1 月 15 日
I run the following code but I am keep getting the same values in every single iteration..
When I used @(p,a)model_2(p,a,b) the dimensions of a and b didn't agree. So I changed it to @(p,a)model_2(p,a,b(1:end-1))
for i=1:size(Image,1)
p(i,:) = lsqcurvefit (@(p,a)model_2(p,a,b(1:end-1)), init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
Torsten
Torsten 2019 年 1 月 15 日
According to the function in model_2, the dimensions of a and b must agree.
Mar
Mar 2019 年 1 月 15 日
編集済み: Mar 2019 年 1 月 15 日
Yeah that's why I changed it. The thing is that my p parameters are the same in all the iterations... Is the way that it is parametrized correct? Why do I get the same value in every single iteration?
Torsten
Torsten 2019 年 1 月 15 日
Was lsqcurvefit successful in determining p (check the exitflag) ?
Are Image(i,1:end-1) different for all i ?
Did you supply a sensful 5-element vector "init" ?
Mar
Mar 2019 年 1 月 15 日
編集済み: Mar 2019 年 1 月 15 日
Yes, Image(i, 1:end-1) is different at all i, and initial values are sensful.
How do I check the exitflag? I tried the following but nothing really happened. It only prints out 0.44
for i=1:size(Image,1)
[p(i,:), exitflag] = lsqcurvefit (@(p,a)model_2(p,a,b(1:end-1)), init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts)
end
exitflag
Torsten
Torsten 2019 年 1 月 16 日
for i=1:size(Image,1)
[p(i,:),resnorm,residual,exitflag,output] = lsqcurvefit(@(p,a)model_2(p,a,b(1:end-1)), init, a(1:end-1), Image(i, 1:end-1), zeros(size(init)), init*10, opts);
exitflag
end
Mar
Mar 2019 年 1 月 16 日
編集済み: Mar 2019 年 1 月 16 日
I got 0 at all iterations.
exitflag =
0
When I print p-values I have the same parameters at all iterations. Nothing changes.
Torsten
Torsten 2019 年 1 月 16 日
Means
Number of iterations exceeded options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations.
Mar
Mar 2019 年 1 月 16 日
Would it be possible the parametrization to be wrong?
Torsten
Torsten 2019 年 1 月 16 日
Before calling "lsqcurvefit", I'd evaluate "model_2" with init, a and b as input and see if it returns senseful values.
Mar
Mar 2019 年 1 月 16 日
I evaluated it and It does return sensful values.
Torsten
Torsten 2019 年 1 月 16 日
Then you should do as "lsqcurvefit" suggests: Set a larger value for the maximum number of iterations:
options.MaxIterations and/or options.MaxFunctionEvaluations

サインインしてコメントする。

質問済み:

Mar
2019 年 1 月 14 日

コメント済み:

2019 年 1 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by