I got FMINCON requires the following inputs to be of data type double: 'A' error,How it can be solved????
37 views (last 30 days)
Show older comments
Ashutosh Sharma
on 18 Jul 2018
Commented: muhammad ilyas khattak khattak
on 13 Jan 2022
% Load Solar Cell data
load elec_solar_iv_data.mat
% Display the Solar Cell model
Model = 'elec_solar';
open_system(Model);
% List of parameters and initial values prior to optimization ParsListMain = {'Is', 'Iph', 'ec', 'Rs', 'Rp'}; InitGuessMain = [ 3e-7 3.8 1.5 .004 10 ]; ParsListTemp = {'TIPH1', 'EG', 'TXIS1'}; InitGuessTemp = [ .001 1.11 3 ];
%Plot Data Versus Solar Cell Output Using Initial Parameters
load_system(Model);
set_param([Model '/Solar Cell'], 'prm', '3')
Pars = reshape([ParsListMain; cellstr(num2str(InitGuessMain'))'],1,[]);
set_param([Model '/Solar Cell'], Pars{:})
Pars = reshape([ParsListTemp; cellstr(num2str(InitGuessTemp'))'],1,[]);
set_param([Model '/Solar Cell'], Pars{:})
% Generate preliminary model curves and plot afminconinst data
num_lines = length(iv_data);
v_model = cell(1, num_lines);
i_model = cell(1, num_lines);
legend_info_data = cell(1, num_lines);
legend_info_model = cell(1, num_lines);
for idx_data = 1:num_lines
sim(Model);
v_model{idx_data} = Vo.signals.values;
i_model{idx_data} = Io.signals.values;
legend_info_data{idx_data} = [ 'Temp = ' ...
num2str(iv_data(idx_data).temperature) '\circC, Data'];
legend_info_model{idx_data} = [ 'Temp = ' ...
num2str(iv_data(idx_data).temperature) '\circC, Model'];
end
plot([iv_data.v], [iv_data.i], 'd', [v_model{:}], [i_model{:}])
xlabel('Solar cell output voltage (V)');
ylabel('Solar cell output current (A)');
legend([legend_info_data legend_info_model], 'Location', 'Best');
title('Model with Initial Parameter Values');
%Optimize Main Tab Dialog Parameters at Room Temperature (Step 1)
% Find room temperature data index
idx_data = find([iv_data.temperature]==25);%#ok
% Optimize parameters in main dialog tab of Solar Cell
ParsList = ParsListMain;
OptParsMain = fmincon(@elec_solar_lse, InitGuessMain, ...
optimset('TolX', 1e-3));
% Update Solar Cell block with optimized parameters
Pars = reshape([ParsList; cellstr(num2str(OptParsMain'))'],1,[]);
set_param([Model '/Solar Cell'], Pars{:});
% Display optimized parameters
display(sprintf(['Optimized parameters for the solar cell main ' ...
'dialog tab are:\n']));
display(sprintf('\t%5s = %s\n', Pars{:}));
%Optimize Parameters Controlling Temperature Dependence (Step 2)
% Find index into data for non-room temperatures
idx_data = find([iv_data.temperature]~=25);
% Optimize parameters in temperature dialog tab of Solar Cell
ParsList = ParsListTemp;
OptParsTemp = fmincon(@elec_solar_lse, InitGuessTemp, ...
optimset('TolX', 1e-3));
% Update Solar Cell block with optimized temperature parameters
Pars = reshape([ParsList; cellstr(num2str(OptParsTemp'))'],1,[]);
set_param([Model '/Solar Cell'], Pars{:});
% Display optimized parameters
display(sprintf(['Optimized parameters for the solar cell ' ...
'temperature dialog tab are:\n']));
display(sprintf('\t%5s = %s\n', Pars{:}));
%Display Optimized Curves
for idx_data = 1:num_lines
sim(Model);
v_model{idx_data} = Vo.signals.values;
i_model{idx_data} = Io.signals.values;
end
plot([iv_data.v], [iv_data.i], 'd', [v_model{:}], [i_model{:}])
xlabel('Solar cell output voltage (V)');
ylabel('Solar cell output current (A)');
legend([legend_info_data legend_info_model], 'Location', 'Best');
title('Model with Optimized Parameter Values');
0 Comments
Accepted Answer
Steven Lord
on 18 Jul 2018
OptParsTemp = fmincon(@elec_solar_lse, InitGuessTemp, ...
optimset('TolX', 1e-3));
You can't just skip required arguments. If you want to pass the options structure into fmincon you MUST specify the fun, x0, A, b, Aeq, beq, lb, ub, and nonlcon inputs first. You've just specified fun and x0, leaving fmincon to treat your options structure as the A input.
Since you don't appear to have any constraints, you probably don't want to use fmincon. Instead, use one of the unconstrained optimization functions. Their signatures are much shorter than that of fmincon, since they don't accept constraints.
9 Comments
Steven Lord
on 12 Jan 2022
When I copied and pasted this into MATLAB Editor I received a Code Analyzer error that one of your functions was not ended with an end. If any of the functions in your file end with an end keyword then all of the functions in your file must end with end.
When I added the end where I think it should be located, I then received an error.
Output argument "Tctotal" (and possibly others) not assigned a value in the execution with
"example410972>FCVEC4AllOutputs12January2022V3" function.
Error in example410972 (line 17)
[Tctotal,Ttotal,tTijtoRij,ttransij,texeij,tkij] = FCVEC4AllOutputs12January2022V3(x0,StoreInitialization,ttransij,texeij,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
That function doesn't appear to define that variable at all.
I'm not sure I'm going to have the time to read through and debug the 350 lines of code you have here further. One suggestion I do have to increase the readability of this code is to reduce the number of input and output arguments your functions accept by having them return and accept struct arrays. Instead of something like:
function [NumRSUs,MuRSU,MaxNumtk,E,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3] = FCVECInitializeParameters08November2021V5()
which has 33 output arguments (three of which shadow built-in functions in MATLAB: pi, i, and j) I'd have it return a struct:
function params = FCVECInitializeParameters08November2021V5()
Then whenever you need (for example) Wfog you could access it as params.Wfog or (if you're going to access it many times) you could define a local variable for that specific parameter to save on typing.
Wfog = params.Wfog;
The same holds for packing the additional parameters you want to pass into your objective function into a struct, so instead of your fun being:
fun = @(x)FCVEC06January2022V2(x,ttransij1,texeij1,theta,D_Cloud,F_Cloud,DPrc,pc,Muc,Mui,Muk,B,pi,pm,W,Wfog,t,T,cdk,j,i,TasksperVehicle,NTotal,Numv,Numtk,TijPrime,loop1,Fprime,PhikPrime,D,F,Ravailk,Phik,theta1,matrixforAlgo3);
it could be something more like:
param2 = struct(...);
fun = @(x) FCVEC06January2022V2(x, param2);
muhammad ilyas khattak khattak
on 13 Jan 2022
@Steven Lord🙏☺️☺️🙂...Thanks steven once again...I exatly did by encapsulating the parameters into a structure and then calling "fmincon"...Below are the final fmincon commands/statments.... Also in the attachment in pdf format (convertable/can be copied to .m format) , is the full code that contains all the helping functions/main functions in case it is needed to run.....If I get successful execution/obtain minimization of what I am doing , a great part of my work will be solved... Therefore, I am trying to contact again and again..... I am literarly lacking words to say thanks... THanks once again in advance, even if you spare a little time, it is much for me..
More Answers (0)
See Also
Categories
Find more on Energy Storage 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!