fmincon should return a scalar value error

7 次查看(过去 30 天)
Hello,
I am trying to do an optimization for a function using a "fmincon". I have got an error and couldn't solve it. Error is
Error using fmincon (line 619)
Supplied objective function must return a scalar value.
Error in PVL_TestScript (line 8)
xopt= fmincon(@objective, x0, [],[],[],[],[],[],@constraint)
My whole program is as follows:
%Define Initial Guess
TiltGuess = 1
%Load Guess values into the array
x0 = [TiltGuess];
%Call solver to minimize the objective function
xopt= fmincon(@objective, x0, [],[],[],[],[],[],@constraint) %Line 8
%Retrieve Optimized sizing values for power
ACPoweropt = TestScript(xopt)
%Define function to calculate AC Power
function ACPower = TestScript(x)
ModuleParameters = pvl_sapmmoduledb(123,'SandiaModuleDatabase_20120925.xlsx');
load('SandiaInverterDatabaseSAM2014.1.14.mat');
Inverter = SNLInverterDB(793);
clear InverterNames SNLInverterDB
Tilt = x(1); % Array tilt angle (deg)
Azimuth = 180; %Array azimuth (180 deg indicates array faces South)
Ms = 10; %Number of modules in series
Mp = 1; %Number of paralell strings
a = -3.56;
b = -0.075;
TMYData = pvl_readtmy3('723650TY.csv');
TimeMatlab = TMYData.DateNumber;
Time = pvl_maketimestruct(TimeMatlab, ones(size(TimeMatlab))*TMYData.SiteTimeZone);
DNI = TMYData.DNI;
DHI = TMYData.DHI;
GHI = TMYData.GHI;
Location = pvl_makelocationstruct(TMYData.SiteLatitude,TMYData.SiteLongitude,TMYData.SiteElevation) %Altitude is optional
PresPa = TMYData.Pressure*100; %Convert pressure from mbar to Pa
[SunAz, SunEl, AppSunEl, SolarTime] = pvl_ephemeris(Time,Location,PresPa,TMYData.DryBulb);
AMa = pvl_absoluteairmass(pvl_relativeairmass(90-AppSunEl),PresPa);
AOI = pvl_getaoi(Tilt, Azimuth, 90-AppSunEl, SunAz);
Eb = 0*AOI; %Initiallize variable
Eb(AOI<90) = DNI(AOI<90).*cosd(AOI(AOI<90)); %Only calculate when sun is in view of the plane of array
EdiffSky = pvl_isotropicsky(Tilt,DHI);
Albedo = 0.2;
EdiffGround = pvl_grounddiffuse(Tilt,GHI, Albedo);
E = Eb + EdiffSky + EdiffGround; % Total incident irradiance (W/m^2)
Ediff = EdiffSky + EdiffGround; % Total diffuse incident irradiance (W/m^2)
SF=0.98;
E0 = 1000; %Reference irradiance (1000 W/m^2)
celltemp = pvl_sapmcelltemp(E, E0, a, b, TMYData.Wspd, TMYData.DryBulb, ModuleParameters.delT);
F1 = max(0,polyval(ModuleParameters.a,AMa)); %Spectral loss function
F2 = max(0,polyval(ModuleParameters.b,AOI)); % Angle of incidence loss function
Ee = F1.*((Eb.*F2+ModuleParameters.fd.*Ediff)/E0)*SF; %Effective irradiance
Ee(isnan(Ee))=0; % Set any NaNs to zero
mSAPMResults = pvl_sapm(ModuleParameters, Ee, celltemp);
aSAPMResults.Vmp = Ms *mSAPMResults.Vmp;
aSAPMResults.Imp = Mp *mSAPMResults.Imp;
aSAPMResults.Pmp = aSAPMResults.Vmp .* aSAPMResults.Imp;
ACPower = pvl_snlinverter(Inverter, mSAPMResults.Vmp*Ms, mSAPMResults.Pmp*Ms*Mp);
end
% Define a objective function for optimization
function obj = objective (x)
obj = -TestScript(x);
end
%Define constriant for optimization
function [c,ceq] = constraint (x)
c = [];
ceq = [];
end
I'd appreciate if somebody would help with this issue.
Thank you.
  8 个评论
Walter Roberson
Walter Roberson 2019-5-16
I am not "Commercial/Industry", "Government", "University", or "National Lab" and so cannot fill out the registration form.
Laxmikant Radkar
Laxmikant Radkar 2019-5-16
No problem Walter. I appreciate your quick responses. I'm trying to figure out what's wrong and once I cmplete it I'll let you know. Thanks again

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2019-5-17
编辑:Matt J 2019-5-17
AC Power reetuns 8760*1 value
If you know that your objective function code is returning a 8760x1 value, it should be pretty clear what is wrong. The error message has told you that it should be returning a scalar instead. What does it even mean to "minimize" a 8760x1 value?
  1 个评论
Laxmikant Radkar
Laxmikant Radkar 2019-5-17
In this case when I take a mean of AC Power, MATLAB returns me a single value and program runs smoothly without any error by providing me the expected results.
Thanks for the help and you quicj reponses.

请先登录,再进行评论。

更多回答(1 个)

Nazam Ali
Nazam Ali 2020-10-15
Hello, I am new to MATLAB coding and I am trying to do optimization using fmincon function and I am receiving following error:
years =
[]
Error using fmincon (line 619)
Supplied objective function must return a scalar value.
Error in Main_code3 (line 47)
[x,fval,exitflag] = fmincon(operator_profit,x0,A,b,Aeq,beq,lb,ub,confunxval);
While my whole code is as follows:
clear
clc
% -----------------------------------------------------------------------%
% Parameters Setting
theta = 0.1;
demand = 5000;
alpha = 500;
beta = 400;
bus_fare = 100;
bus_fuel_cost = 200;
distance_travelled = 10;
a0 = 4000;
x = [];
% Calculation of Mathematical Expressions
% -----------------------------------------------------------------------%
bus_probability = exp(-theta * (1./x) * alpha + beta);
passenger_flow = demand * bus_probability;
car_probability = 1 - bus_probability;
bus_operation_cost = a0 + distance_travelled * bus_fuel_cost;
% Objective Function to Maximize the Bus Operator Profit
% -----------------------------------------------------------------------%
operator_profit = @(x)...
(demand .*...
((exp(-theta * (1./x) * alpha + beta))...
/(exp (-theta * (1./x) * alpha + beta)) + (1 - car_probability))...
* (distance_travelled * bus_fare)...
- ((a0 + distance_travelled * bus_fuel_cost) *x));
real(operator_profit(20))
A = [];
b = [];
Aeq = [];
beq = [];
lb = 1;
ub = 200;
x0 = 4;
confunxval = @(x) confun(x,demand,theta,passenger_flow,alpha,car_probability,beta,a0,distance_travelled, bus_fare,bus_fuel_cost);
[x,fval,exitflag] = fmincon(operator_profit,x0,A,b,Aeq,beq,lb,ub,confunxval);
I will really appreciate if someone can help. Thank you!
Nazam
  9 个评论
Nazam Ali
Nazam Ali 2020-10-16
@Stephen,
I agree with you. But, I put "x" as a replacement of another term to make it simple. If I don't define x = []; it shows the following error:
Undefined function or variable 'x'.
Error in Main_code3 (line 19)
bus_probability = exp(-theta * ((1./x) * alpha + beta + bus_fare))...
Which means that I need to define "x" to debugg this error.
Thank you!
Stephen23
Stephen23 2020-10-16
"Which means that I need to define "x" to debugg this error."
Or, as I wrote in my previous comment, you need to define all of those formulas as anonymous functions.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by