Error using lsqcurvefit in non-linear data optimization

I have a data file of inverted airfoil different angles of attack and corresponding CL and i am trying to optimize it for maximum downforce using lsqcurvefit by the following code
clear
clc
load num_data.txt
alfa = num_data(:,1);
cl= num_data(:,2);
plot(alfa,cl,'ro')
title('alfa_vs_cl')
rho=1.225;co=0.264;
objective = @(x) -0.5*rho*(x(1)^2)*x(2)*x(3)*co*(sind(x(4)));
x0 = [0.1,0.1,0.1,0];
lb=[0,0.8385,0,-2];
ub=[90, 2.0996,1.8,18];
x = lsqcurvefit(objective,x0,alfa,cl,lb,ub)
but i get the following error
Error using regularstand>@(x)-0.5*rho*(x(1)^2)*x(2)*x(3)*co*(sind(x(4)))
Too many input arguments.
Error in lsqcurvefit (line 202)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in regularstand (line 15)
x = lsqcurvefit(objective,x0,alfa,cl,lb,ub)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
what can i do to fix it?

 Réponse acceptée

Walter Roberson
Walter Roberson le 5 Déc 2017
For lsqcurvefit your function needs to accept two input values. See https://www.mathworks.com/help/optim/ug/lsqcurvefit.html#inputarg_fun

2 commentaires

It worked thank u so much
There is another problem that x(2) in the objective function equals cl so i don't know how to represent it properly since the objective function now will have three inputs x,alpha,cl and it will give an error and if i keep cl as x(2) it will not represent true value

Connectez-vous pour commenter.

Plus de réponses (1)

Your objective function does not use ‘alfa’, so you are not doing any fitting of your data to ‘cl’. Your objective function needs to be of the form:
objective = @(x,alfa) some_function_of_x_and_alfa;

8 commentaires

That was very helpful thank you
My pleasure.
There is another problem that x(2) in the objective function equals cl so i don't know how to represent it properly since the objective function now will have three inputs x,alpha,cl and it will give an error and if i keep cl as x(2) it will not represent true value
I am not sure what you are doing. To use lsqcurvefit, your ‘objective’ must be a function of only your independent variable and your parameter vector. The lsqcurvefit function will fit it to your dependent variable.
If you have more than one independent variable, you can concatenate ‘alfa’ and ‘cl’ in the same array and refer to the individual columns, for example:
iv = [alfa cl]; % Independent Variable Matrix
fcn = @(x,dv) x(1).*dv(:,1) + x(2).*dv(:,2);
This creates: fcn = x1*alfa + x2*cl. You would then regress this against whatever you define your dependent variable to be.
If you are doing optimization and both ‘alfa’ and ‘cl’ are part of ‘objective’ and you want to estimate ‘x’, you must use another optimization function, such as fmincon.
Note: for fitting problems you would want to minimize the difference between the projected data and the experimental data. To do that, you would not minimize the projection function: you would minimize
sum(projection_results.^2)
@Adel — Note that I mentioned in my original Answer that you have to incorporate ‘alfa’ in ‘objective’ and then fit it to ‘cl’ using lsqcurvefit.
Since ‘alfa’ does not appear in ‘objective’, lsqcurvefit is not fitting the equation to your data. I have no idea what that equation should be or where ‘alfa’ should appear in it, so I cannot write it for you.
I started to get it right, thank u so much I really appreciate the time and effort u took to help me

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by