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 个)

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 个评论

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.
lsqcurvefit is not designed for surface fitting. however you could probably parameterize
lsqcurvefit(@(p,a) model_2(p,a,b(1:end-1)) ....
How can I define the initial values if I parametrize it? I am pretty new to coding..
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
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
According to the function in model_2, the dimensions of a and b must agree.
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?
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" ?
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
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
I got 0 at all iterations.
exitflag =
0
When I print p-values I have the same parameters at all iterations. Nothing changes.
Means
Number of iterations exceeded options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations.
Would it be possible the parametrization to be wrong?
Before calling "lsqcurvefit", I'd evaluate "model_2" with init, a and b as input and see if it returns senseful values.
I evaluated it and It does return sensful values.
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