I'm trying to fit nonliniear coefficient with lsqcurvefit, please help (new to matlab)

2 views (last 30 days)
I need help, i want to find coefficient of a model that best fit with the experimental value. I'm not sure if my case can be solved by lsqcurvefit or no, but here are my code so far:
xdata = [1.1111, 1.2097. 1.3274, 1.4778, 1.6304, 1.8750];
ydata = [1.0200, 1.0500, 1.0900, 1.1200, 1.1433, 1.1500];
h0 = 3;
b0 = 3;
b1 = [3.0600, 3.1500, 3.2700, 3.3600, 3.4300, 3.4500];
h1 = [2.7000, 2.4800, 2.2600, 2.0300, 1.8400, 1.6000];
r = 21;
temp = b1/sqrt(r*(h0-h1));
temp1 = temp(:, [pass]);
fun = @(x,xdata)x(1)*xdata^(x(2)*exp(-x(3)*temp1))-x(4)*h0^(-x(5))*xdata^x(6);
x0 = [100,-1];
x = lsqcurvefit(fun, x0, xdata, ydata);
But then the result shows:
Index exceeds the number of array elements (2).
Error in finding_z>@(x,xdata)x(1)*xdata^(x(2)*exp(-x(3)*temp1))-x(4)*h0^(-x(5))*xdata^x(6) (line 17)
fun = @(x,xdata)x(1)*xdata^(x(2)*exp(-x(3)*temp1))-x(4)*h0^(-x(5))*xdata^x(6);
Error in lsqcurvefit (line 225)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in finding_z (line 19)
x = lsqcurvefit(fun, x0, xdata, ydata);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
The model used to be like this:
ydata == 1.03*(xdata)^(0.94*exp(-0.45*bf/sqrt(r*(h0-hf))))-0.26*h0^(-1.4)*(xdata)^3.38
%ydata = b1/b0
%xdata = h0/h1
But i want to find the right coefficient that can fit the experimental result.
Please help me i'm new to matlab
  1 Comment
Alex Sha
Alex Sha on 13 Jun 2021
The part "x(4)*h0^(-x(5))" in the function is meaningless, can simply be replaced by x(4), the result should be same

Sign in to comment.

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 12 Jun 2021
The size of the solution x is the same as the size of x0 because the solvers use the number of elements in x0 and the size of x0 to determine the number and size of variables that fun accepts., hence as per the above function equation the x0 must be an array with six elements.
You can refer to the documentation of the function lsqcurvefit and the explanation of Input Arguments and Output Arguments for more information.
Also you might encounter an error w.r.t the equation defined and have to change the operators used in the fun equation with element wise operators. Refer to the the following page Array vs. Matrix Operations for more information.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!