Clear Filters
Clear Filters

Why is it Showing this error? Help me please.

1 view (last 30 days)
salman
salman on 14 Jan 2024
Commented: Walter Roberson on 14 Jan 2024
This is the coding:
cl=load("clnaca0018.mat","cl");
cd=load("cdnaca0018.mat","cd");
% Objective function for power coefficient optimization
objectiveFunction = @(x)((cl)*cosd(x(2))+((cd)*sind(x(2)))*x(1));
% Initial guess
x0 = [10, 4];
% lower&upper bounds
lb = [0, 2];
ub = [15, 8];
A=[];
b=[];
Aeq=[];
beq=[];
% Perform optimization
x = fmincon(objectiveFunction,x0,A,b,Aeq,beq,lb,ub);
% Extract optimized parameters
optimizedTSR = x(1);
optimizedtwistAngle = x(2:end);
% Display the optimized parameters
disp('Optimized Parameters:');
disp(['Optimized Tip-Speed Ratio: ', num2str(x(1))]);
disp(['Optimized Blade Twist Angle: ', num2str(x(2))]);
This is the error:
Operator '*' is not supported for operands of type 'struct'.
Error in code1>@(x)((cl)*cosd(x(2))+((cd)*sind(x(2)))*x(1)) (line 5)
objectiveFunction = @(x)((cl)*cosd(x(2))+((cd)*sind(x(2)))*x(1));
Error in fmincon (line 563)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in code1 (line 19)
x = fmincon(objectiveFunction,x0,A,b,Aeq,beq,lb,ub);

Answers (1)

Walter Roberson
Walter Roberson on 14 Jan 2024
Change
cl=load("clnaca0018.mat","cl");
cd=load("cdnaca0018.mat","cd");
to
cl=load("clnaca0018.mat","cl");
cd=load("cdnaca0018.mat","cd");
cl = cl.cl;
cd = cd.cd;
  2 Comments
salman
salman on 14 Jan 2024
this error shown
Error using fmincon
Supplied objective function must return a scalar value.
Error in code1 (line 20)
x = fmincon(objectiveFunction,x0,A,b,Aeq,beq,lb,ub);
Walter Roberson
Walter Roberson on 14 Jan 2024
That is to be expected if either you cl or your cd are non-scalar.
You could try
objectiveFunction = @(x)sum(((cl)*cosd(x(2))+((cd)*sind(x(2)))*x(1)).^2);

Sign in to comment.

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!