Info

This question is closed. Reopen it to edit or answer.

Odd results when using lsqnonlin. Please offer advice.

1 view (last 30 days)
Vicky
Vicky on 13 Nov 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello,
I'm new to Matlab and the concepts behind non-linear optimization, so I would like to ensure that have a good understanding of what my code is doing. I have two sets of data x and y and am to use lsqnonlin to fit the following function:
if x(k) < a
y = c*(1.5*(x/a)-0.5*(x(/a)^3);
else
y(k) = c;
To do so, I use the main script:
x = vrgdata(:,1);
v = vrgdata(:,1);
lsqnonlin(@(B) eqn2(B,x,v),[0.2 0.2],x,v)
which calls the function:
function y = eqn2(B,x,v)
a2 = B(1);
c2 = B(2);
for k = 1:length(x)
if x(k) < a2
y(k) = c2*(1.5*(x(k)/a2) - 0.5*(x(k)/a2)^3);
else
y(k) = c2;
end
end
When I run the code I do receive an output; however, there are some issues I have with my results. Firstly, I had expected that my function would return to me a vector B containing the coefficients a2 and c2. Secondly, the results that I received were accompanied by warnings that the lengths of the lower and upper bounds were greater than the length of x. I'd really appreciate it if someone could explain to me what's causing these issues and also let me know if I am using the lsqnonlin function improperly.

Answers (1)

Walter Roberson
Walter Roberson on 13 Nov 2015
lsqnonlin(@(B) eqn2(B,x,v),[0.2 0.2],x,v) is using x and v twice, once passed in by the anonymous function, and once as explicit arguments in the lower-bound and upper-bound parameter positions.
  5 Comments
Torsten
Torsten on 13 Nov 2015
You forgot to subtract your experimental v-data from the function values. Thus at the moment, your v-values do not influence the computation at all.
Best wishes
Torsten.

Community Treasure Hunt

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

Start Hunting!