The Error in lsqcurvefit
2 views (last 30 days)
Show older comments
Trying to fit a equation and I keep getting:
Error using lsqcurvefit (line 271)
"Function value and YDATA sizes are not equal.
Error in optim (line 63)
optimizedParameters = lsqcurvefit( ..."
Could you please have a look?
1 Comment
Walter Roberson
on 22 Apr 2021
fun = @(parameters,expStrain) packFun(strainRate(j),...
tempC(i),parameters,finalStrain);
packFun is not a Mathworks function, and you did not provide its code.
Answers (2)
Walter Roberson
on 23 Apr 2021
fun = @(parameters,expStrain) packFun(strainRate(j),...
tempC(i),parameters,finalStrain);
and
optimizedParameters = lsqcurvefit( ...
fun, ...
parameters, ...
experimentalData(1:500,1), ...
experimentalData(1:500,2), ...
lowerBounds, ...
upperBounds, ...
options);
lsqcurvefit is going to call the function, fun, passing to it a vector of trial model parameters, and some data points (for the first iteration it will not necessarily be the full set of X data.) The objective function is responsible for returning a vector the same size as the data passed in, with the output being a projection of what the y data would be for those model parameters at those x locations. If all goes well, lsqcurvefit() would then form the sum-of-squared error against the y data, giving a goodness of fit. Then it would alter the trial model parameters and try again.
However, look at the function you passed in, fun. You take the passed in second parameter, expStrain (the x data), and you... do not pass it to packFun. So unless packFun has its own copy of the x data, packFun cannot possibly return output the same size as the input.
What your packFun does return is data that is numberOfSteps x 1, where numberOfSteps is 1000 * the value of the 4th parameter -- that is, it calls solveFlowStress which returns numberOfSteps x 9, and packFun returns one column of that. That is probably not exactly the same size as the number of rows in experimentalData ... and did I mention that lsqcurvefit is permitted to pass in only a subset of the data?
You need a function that takes in a vector of coordinates, and calculates output for those coordinates.
0 Comments
See Also
Categories
Find more on Historical Contests in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!