The Error in lsqcurvefit

2 views (last 30 days)
Saham Sharifi
Saham Sharifi on 22 Apr 2021
Answered: Walter Roberson on 23 Apr 2021
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
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.

Sign in to comment.

Answers (2)

Saham Sharifi
Saham Sharifi on 23 Apr 2021
Thank you Walter for your reply,
Actually, I did define a packFun function, as follow. It refers to the whole calculation process.
(I attached T1000 in txt format since the web page did not accept it in .dat )

Walter Roberson
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.

Categories

Find more on Historical Contests in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!