A way to sort data using for loops and newton-rhapson calc so data can be readily called

1 view (last 30 days)
Hello,
I have a program file that executes a Newton Rhapson script for a Heat Pump. I am trying to run two "for" loops for Ae and Ac for a range or parameters to optimize cost. I would like to be able to run the Ae and Ac loops, then have Ae, Ac, Cost, and the Newton solved parameters stored in a matrix. Once the "for" loops are done, then determine the row with the minimum cost and the associated parameters. The code is below:
if true
% Program Constants
maxiter = 50;
epsilon = 0.001;
nsys = 9; %Number of System Equations (and unknowns)
% System Constants
toa = 0; %Outside Air Temp, Deg C
tea = 24; %Exhaust Air Temp, Deg C
woa = 5; %Outside Air Flow Rate, kg/s
wea = 5; %Exhaust Air Flow Rate, kg/s
tsupply = 35;%Supply Air Temp, Deg C
Uc = 25; %Condensor Heat Transfer Rate, W/m^2 K
Ue = 25; %Evaporator Heat Transfer Rate, W/m^2 K
cpea = 1.006;%kJ/kg C
cpoa = 1.006;%kJ/kg C
for Ae = 1.58:0.01:1.7;
for Ac = 1.48:0.01:1.6;
% Define System Equations
% Evaporator
% wea*(tea-to)*cpea-Qe = 0
F1 = @(x) wea*(tea-x(4))*cpea-abs(x(2));
% tea+(te-tea)(1-exp((-Uc*Ae)/(wea*cpoa)))-t0 = 0
F2 = @(x) tea+(x(5)-tea)*(1-exp((-Ue*Ae)/(wea*cpea)))-x(4);
% Condensor
% woa*(ti-toa)*cpoa-Qc = 0
F3 = @(x) woa*(x(6)-toa)*cpoa-x(3);
% toa+(tc-toa)(1-exp((-Uc*Ac)/(woa*cpoa)))-ti = 0
F4 = @(x) toa+(x(7)-toa)*(1-exp((-Uc*Ac)/(woa*cpoa)))-x(6);
% Compressor
% W + Qe - Qc = 0
F5 = @(x) x(1)+abs(x(2))-x(3);
% Qe/W-COP = 0
F6 = @(x) (abs(x(2))/x(1))-abs(x(8));
% 7.24+0.352te-0.096tc-0.0055tetc-COP = 0
F7 = @(x) 7.24+0.352*x(5)-0.096*x(7)-0.0055*x(5)*x(7)-abs(x(8));
%Resistance Heating
% woa*(tsupply-ti)*cpoa-Qr = 0
F8 = @(x) woa*(tsupply-x(6))*cpoa-x(9);
% Overall System Energy Balance
% W+Qe-Qc-Qr = 0
F9 = @(x) x(1)+abs(x(2))-x(3)-x(9);
%Construct Matrix F
F = {F1,F2,F3,F4,F5,F6,F7,F8,F9};
% Initial Guesses and Construction of Matrix x
x(1) = 3; %W
x(2) = 3; %Qe
x(3) = 3; %Qc
x(4) = 11; %to
x(5) = 10; %te
x(6) = 9; %ti
x(7) = 10; %tc
x(8) = 2; %COP
x(9) = 1; %Qr
%Iterative calculations
nloop=0;
nconverg=0;
i = 1;
while nloop<maxiter
%Derivative Matrix
dFidxj=zeros(nsys,nsys);
for i=1:1:nsys
for j=1:1:nsys
fx=F{i};
xj=x;
%Function after the point
xj(j)=x(j)+epsilon/2;
y1=fx(xj);
%Function before the point
xj(j)=x(j)-epsilon/2;
y0=fx(xj);
%Derivation in the point
dFidxj(i,j)=(y1-y0)/epsilon;
end
end
for i=1:1:nsys
j=F{i};
D(i,1)=j(x);
end
%Calculate Error
A=inv(dFidxj);
error=A*D;
dx=error';
%Finding new iteration
x=x-dx;
%While loop counter
nloop=nloop+1;
nconverg=nconverg+1;
Converging criteria (error < epsilon)
maxdx=max(abs(error));
if maxdx<epsilon
nloop=maxiter;
end
end
i = i + 1;
Cost = 50*Ae+50*Ac+120*x(1)+0.03*4000*6.145*(x(9)+x(1))
end
end
end
I would like the data to be stored such that column one is Cost, Column Two is the value of Ae, Column three the value of Ac, etc for all parameters. My attempts at this have not gone so well. Does anyone know of a means to do this?
Thanks

Accepted Answer

Guillaume
Guillaume on 9 Dec 2014
With this sort of question, it's better to reduce your code sample to the minimum required to understand the question. I've skimmed most of it but I believe this is what you want:
AeValues = 1.58:0.01:1.7;
AcValues = 1.48:0.01:1.6;
numParameters = 3 %other than cost, Ac, Ae. For you to replace
CostAndParams = zeros(numel(AeValues)*numel(AcValues), 3 + numparameters);
rowindex = 1;
for Ae = AeValues
for Ac = AcValues
%... do your calcs ...
Cost = ...
CostAndParams(rowindex, :) = [Cost Ac Ae ...]; %replace ... by other parameters
rowindex = rowindex + 1;
end
end
Afterward, I would store the data in a table to make it more explicit what the columns are:
CostTable = table(CostAndParams, 'VariableNames', {'Cost', 'Ac', 'Ae', ...});

More Answers (1)

Galen
Galen on 10 Dec 2014
Guillaume, this worked great. Thanks!

Categories

Find more on Fluid Network Interfaces Library 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!