Matlab is outputting the vector and summation of vector components as one in the same and giving me a plot even though I don't call upon it, how do I fix?

2 views (last 30 days)
So using the Optimization Toolbox app, I need to be able to obtain the values of the vector, sol.xo, but it keeps giving me that it's equal to, sum(sol.xo), which is not the case here since they are both totally different types and so it must yield an error saying Matrix dimensions must agree. I was able to produce the values of the array for every iteration before I started messing with the code. And I think in doing so, I may have slotted both sol.xo and sum(sol.xo) in the same memory slot. So questions, how do I produce the array of numbers again? And is it possible for Matlab to store the expression's as one in the same, that is, sol.xo=sum(sol.xo) even though this would actually produce an error? If so, how can I extract the information again without uninstallng then reinstalling Matlab? Matlab is also returning a plot for the code in which it does not call upon it anywhere within the code. I'm assuming I'm getting this because Matlab has had a sort of hiccup and that there's nothing actually wrong with my code, so how do you fix Maltab after it goes bananas?
My code(let me know if I need to explain any terms or expressions):
function preventivemaintenance_3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code implements optimization problem for preventive maintenance
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Setting basic problem parameters
%%
%% number of maintenance intervals:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=1;
gr = 1000; %\gamma_r
gm = 100;
%%%
a2 = 0.075;
a3 = 0.025;
b = 0.5;
xmin=0.5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Now we set up the optimization problem.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Optimization problem using the Matlab Optimization Toolbox
% most of code is borrowed at
% see https://www.mathworks.com/help/optim/ug/fminunc.html
%
% we will be using function from the Optimization Toolbox called fminunc
%%%%%%%%%%%%%%%%%%%%%%%%
% set up a function to provide visualization of the progress
% see help for structure of output functions https://www.mathworks.com/help/optim/ug/output-function.html
outputfn = @(in,optimValues,state)outfun(in,optimValues,state);
% set us options for the function
%%%%%%%%%%%%%%%%%%%%%%%%
%%% Step 1 is to set up the problem and the optimization variable
prob = optimproblem('ObjectiveSense','minimize'); % we will be minimizing the objective function
% other parameters can be set later
xo = optimvar('xo',1,n); % that defines the optimization variable with a name "xo"
xo=xmin+xo.^2;
%%% Step 2: define the objective function
optfun = @(x)LifeTmCost(x,n,b,a2,a3,gr,gm);
%Hhat=@(t)Hhat_g(t,a2,a3);
prob.Objective = LifeTmCost(xo,n,b,a2,a3,gr,gm);
% Step 4 -- set up initial approximation
% set initial approximation
xB.xo = ones(1,n);
% Step 5 -- set a solver to use
%opts = optimoptions('fmincon');
opts = optimoptions('fminunc','Algorithm','quasi-newton','OutputFcn',outputfn);
% 'SubproblemAlgorithm', 'cg', ...
% 'HessianApproximation', 'finite-difference', 'OutputFcn',outputfn);
% call the solver
% Step 6 -- call the solver
[sol,fval,eflag,output] = solve(prob,xB,'Options',opts);
sol.xo
cost = LifeTmCost(sol.xo,n,b,a2,a3,gr,gm);
sum(sol.xo)
output.iterations
output.stepsize
function C = LifeTmCost(x,n,b,a2,a3,gr,gm)
tn = sum(x);
%k=1
numerC = Hhat(x(1),a2,a3); %% We will compute the expression in curly braces in (13.2.3)
%k=2
if (n>1)
sbx = b*x(1); % this will accumulate the \sum_{j=1}^{k-1} b_{j}*x_{j}
numerC = numerC + Hhat(sbx+x(2),a2,a3) - Hhat((1-b)*x(1),a2,a3);
% all other k
for k=3:n
numerC = numerC - Hhat(sbx + (1-b)*x(k-1),a2,a3);
sbx = sbx + b*x(k-1);
numerC = numerC + Hhat(sbx + x(k),a2,a3);
end
end
C = (gr + (n-1) + gm*numerC)/tn;
%%%%%%%%%%%%%%%%%%%%%%
%% function \hat{H}(t)
function z=Hhat(t,a2,a3)
z = t + a2*t^2 + a3*t^3;
%type outfun
function stop = outfun(in,optimValues,state)
persistent history searchdir fhistory
stop = false;
switch state
case 'init'
hold on
history = [];
fhistory = [];
searchdir = [];
%%%% Add print %%%
fprintf('History of the method: Iteration | xo | Minimum Total Maintenance Cost |: \n\n');
%%%%%%%%%%%%%%%%%%
case 'iter'
% Concatenate current point and objective function
% value with history. in must be a row vector.
fhistory = [fhistory; optimValues.fval];
history = [history; in(:)']; % Ensure in is a row vector
% Concatenate current search direction with
% searchdir.
% searchdir = [searchdir;...
% optimValues.searchdirection(:)'];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the hiustory of the approximations
fprintf('\n\n iteration %4i \n',optimValues.iteration);
fprintf(' %8.7f ',in)
fprintf('\n Value of MLC %8.7f \n',optimValues.fval)
%fprintf('\n stepsize %8.7f \n',optimValues.stepsize)
%fprintf('\n First Order Optimality %8.7f \n',optimValues.firstorderopt)
case 'done'
hold off
assignin('base','optimhistory',history);
assignin('base','searchdirhistory',searchdir);
assignin('base','functionhistory',fhistory);
otherwise
end

Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by