fmincon should return a scalar value error

5 visualizaciones (últimos 30 días)
Laxmikant Radkar
Laxmikant Radkar el 14 de Mayo de 2019
Comentada: Stephen23 el 16 de Oct. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 16 de Mayo de 2019
I am not "Commercial/Industry", "Government", "University", or "National Lab" and so cannot fill out the registration form.
Laxmikant Radkar
Laxmikant Radkar el 16 de Mayo de 2019
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

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 17 de Mayo de 2019
Editada: Matt J el 17 de Mayo de 2019
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 comentario
Laxmikant Radkar
Laxmikant Radkar el 17 de Mayo de 2019
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.

Iniciar sesión para comentar.

Más respuestas (1)

Nazam Ali
Nazam Ali el 15 de Oct. de 2020
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 comentarios
Nazam Ali
Nazam Ali el 16 de Oct. de 2020
@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 el 16 de Oct. de 2020
"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.

Iniciar sesión para comentar.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by