Error using fzero (line 246) FZERO cannot continue because user-supplied function_handle ==> @(X)BEMTR(​X,v,omega,​r,Beta,tws​t,alpha_da​ta,cl_data​,cd_data,s​igma_r) failed with th

4 次查看(过去 30 天)
function f = BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data,sigma_r)
W = sqrt(v^2*(1-X(1))^2 + omega^2*r^2 * (1 + X(2)^2)); % local velocity
phi = asind(v*(1 -X(1))/W); % flow velocity
alpha = phi - Beta - twst; % local angle of attack
% two index of having desired value between them
vec1 = find(alpha_data<=alpha);
vec2 = find(alpha_data>=alpha);
index = [vec1(end) vec2(1)];
cl = (cl_data(index(2))-cl_data(index(1)))/...
(alpha_data(index(2))-alpha_data(index(1))) * ...
(alpha -alpha_data(index(1))) + cl_data(index(1)); % local lift coff
cd = (cd_data(index(2))-cd_data(index(1)))/...
(alpha_data(index(2))-alpha_data(index(1))) * ...
(alpha -alpha_data(index(1))) + cd_data(index(1)); % local drag coff
Cx = cl*cos(phi) + cd*sin(phi);
Cy = cl*sin(phi) - cd*cos(phi);
RHS1 = sigma_r/(4*sin(phi^2)) *(Cx - sigma_r/(4*sin(phi)^2) *Cy^2);
RHS2 = sigma_r * Cy /(4*sin(phi)*cos(phi));
delta_CT = X(1) - RHS1*(1-X(1));
dleta_Q = X(1) + RHS2*(1 + X(2));
f = [delta_CT;dleta_Q];
end
------------------------------------------------------------------------------------------------------------------------------
Ig = [1/3; 0.1];
BEMTr = @(X) BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data...
,sigma_r);
x = fzero(BEMTr,Ig)

采纳的回答

Walter Roberson
Walter Roberson 2021-6-3
The variables such as v and omega must be defined before you can run your function.
Ig = [1/3; 0.1];
BEMTr = @(X) BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data...
,sigma_r);
x = fzero(BEMTr,Ig)
Error using fzero>localFirstFcnEval (line 729)
FZERO cannot continue because user-supplied function_handle ==> @(X)BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data,sigma_r) failed with the error below.

Unrecognized function or variable 'v'.

Error in fzero (line 226)
fa = localFirstFcnEval(FunFcn,FunFcnIn,a,varargin{:});
function f = BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data,sigma_r)
W = sqrt(v^2*(1-X(1))^2 + omega^2*r^2 * (1 + X(2)^2)); % local velocity
phi = asind(v*(1 -X(1))/W); % flow velocity
alpha = phi - Beta - twst; % local angle of attack
% two index of having desired value between them
vec1 = find(alpha_data<=alpha);
vec2 = find(alpha_data>=alpha);
index = [vec1(end) vec2(1)];
cl = (cl_data(index(2))-cl_data(index(1)))/...
(alpha_data(index(2))-alpha_data(index(1))) * ...
(alpha -alpha_data(index(1))) + cl_data(index(1)); % local lift coff
cd = (cd_data(index(2))-cd_data(index(1)))/...
(alpha_data(index(2))-alpha_data(index(1))) * ...
(alpha -alpha_data(index(1))) + cd_data(index(1)); % local drag coff
Cx = cl*cos(phi) + cd*sin(phi);
Cy = cl*sin(phi) - cd*cos(phi);
RHS1 = sigma_r/(4*sin(phi^2)) *(Cx - sigma_r/(4*sin(phi)^2) *Cy^2);
RHS2 = sigma_r * Cy /(4*sin(phi)*cos(phi));
delta_CT = X(1) - RHS1*(1-X(1));
dleta_Q = X(1) + RHS2*(1 + X(2));
f = [delta_CT;dleta_Q];
end
  2 个评论
Walter Roberson
Walter Roberson 2021-6-3
Your code tries to pass in a vector of initial conditions. fzero() is restricted to functions of a single variable.
Your code tries to return a pair of values. fzero() is restricted to scalar results.
Depending on the exact values, your code might have tried to use too many iterations.
Your code fails to test for the possibility that alpha is not found within alpha_data. This is a significant problem. You should detect this and either adjust for it or else error()
Efficiency note:
If your alpha_data values are not sorted, then it does not make sense to talk about an alpha being "between" two values. If your alpha_data values are sorted, then vec2 is always going to either equal vec1 or else be immediately adjacent to it.
Ig = [1/3; 0.1];
v = rand(); omega = rand(); r = rand()*10; Beta = randn();
twst = rand(); alpha_data = linspace(-20,20,50);
cl_data = sort(randn(size(alpha_data)));
cd_data = sort(randn(size(alpha_data)));
sigma_r = rand();
BEMTr = @(X) BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data...
,sigma_r);
options = optimoptions('fsolve', 'MaxFunctionEvaluations', 1e5, 'MaxIterations', 1e5);
x = fsolve(BEMTr, Ig, options)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = 2×1
0.1732 0.6263
function f = BEMTR(X,v,omega,r,Beta,twst,alpha_data,cl_data,cd_data,sigma_r)
W = sqrt(v^2*(1-X(1))^2 + omega^2*r^2 * (1 + X(2)^2)); % local velocity
phi = asind(v*(1 -X(1))/W); % flow velocity
alpha = phi - Beta - twst; % local angle of attack
% two index of having desired value between them
vec1 = find(alpha_data<=alpha);
vec2 = find(alpha_data>=alpha);
index = [vec1(end) vec2(1)];
cl = (cl_data(index(2))-cl_data(index(1)))/...
(alpha_data(index(2))-alpha_data(index(1))) * ...
(alpha -alpha_data(index(1))) + cl_data(index(1)); % local lift coff
cd = (cd_data(index(2))-cd_data(index(1)))/...
(alpha_data(index(2))-alpha_data(index(1))) * ...
(alpha -alpha_data(index(1))) + cd_data(index(1)); % local drag coff
Cx = cl*cos(phi) + cd*sin(phi);
Cy = cl*sin(phi) - cd*cos(phi);
RHS1 = sigma_r/(4*sin(phi^2)) *(Cx - sigma_r/(4*sin(phi)^2) *Cy^2);
RHS2 = sigma_r * Cy /(4*sin(phi)*cos(phi));
delta_CT = X(1) - RHS1*(1-X(1));
dleta_Q = X(1) + RHS2*(1 + X(2));
f = [delta_CT;dleta_Q];
end
mostafa kareem
mostafa kareem 2021-6-4
Thank you so much for your answer .the variables such as v and omega are defined in my code but forgot to copy them during posting the question.in the theory i'm implementing ,alpha should always exist in the vector but i will set the codition in case something wrong happened. yes , fsolve solved the problem and the code worked sucessfully. thank you so much i really appreciate your help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Optimization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by