Answer To Exercises Anonymous
Answer To Exercises Anonymous
SW 10
SW 11
Solution with MATLAB
>> S0 = [100;0.20;0];
>> L0 = [452/3;0;1];
>> S1 = [0;1/300;0.33];
>> UNDERFLOW = @(x) 1/3-x;
>> r_leachcounter2(S0,L0,S1,3,UNDERFLOW);
% Objective is already reached with N = 3.
Zoomed in
SW 12
Solution with MATLAB
>> S0 = [20;0.10;0.05];
>> L0 = [50.64;0;1];
>> S1 = [0;0.04;34]./51.04;
>> UNDERFLOW = @(x) 2*(1-x)/3;
>> r_leachcounter2(S0,L0,S1,3,UNDERFLOW);
% N is increased until the objective is reached.
% In this case, it is reached when N = 10.
>> r_leachcounter2(S0,L0,S1,10,UNDERFLOW);
Zoomed in.
The script was created assuming that the DELTA is above (and thus, line is drawn from the underflow
instead of from overflow).
SW 13
Experimental 𝑁 vs. 𝑦𝐴 is fitted using a cubic polynomial (R is very close to 1)
function TAB = r_leachcounter2(S0,L0,S1,N,UFfunc)
%LEACHCOUNTER2 Countercurrent Multistage Leaching
% Detailed explanation goes here
%Solution is for unknown number of stages
%Leave S1(1) = 0.
%syms x y
OFfunc = @(x) 1 - x;
figure
hold on
XX = linspace(0,1,100);
Orang = [255 165 0]./255;
Reddo = [255 0 0]./255;
Bru = [0 0 255]./255;
n = length(S0);
S0 = r_verticalize(S0);
L0 = r_verticalize(L0);
S1 = r_verticalize(S1);
MAT = [S0,L0];
MAT(:,3) = S;
plotPoint(S,[0 0 0]) %=====================
drawLine(S0,L0,Reddo) %=====================
%Graphically obtain LN
TLf0 = r_polyfits([S1(2) S(2)],[S1(3) S(3)],1,'func','off');
plot(XX,TLf0(XX),"--","Color",Reddo) %=====================
[xLN,yLN] = intersectLines(TLf0,OFfunc);
zLN = 1 - xLN - yLN;
LN = [0;xLN;yLN;zLN];
%ILAR
S1(1) = S(1)*(S(2)-LN(2))/(S1(2)-LN(2));
LN(1) = S(1)-S1(1);
MAT(:,4:5) = [S1,LN];
plotPoint(LN,C) %=====================
varNames = ["S_0","L_0","S","S_{1_0}","L_{"+string(N)+"}"];
for i=1:N
Sc = 6+2*(i-1);
C = getColor(i+1,N+1);
if i==1
%Get DELTA
DL1 = getrLine(L0,S1);
DL2 = getrLine(LN,S0);
[xDELTA,yDELTA] = intersectLines(DL1,DL2);
frDELTA = L0(1)-S1(1);
zDELTA = 1 - xDELTA - yDELTA;
dx_ = [xDELTA;yDELTA;zDELTA;1].*frDELTA;
DELTA = [0;xDELTA;yDELTA;0];
plotPoint(DELTA,Orang) %=====================
drawLine(S1,DELTA,Orang) %=====================
drawLine(S0,DELTA,Orang) %=====================
MAT(:,Sc) = DELTA;
%Get SN from LN
O = [0;0;0;0];
OLN = getrLine(O,LN);
plotPoint(O,[0 0 0]) %=====================
drawLine(O,LN,Bru) %=====================
[xSN,ySN] = intersectLines(OLN,UFfunc);
zSN = 1 - xSN - ySN;
SN = [0;xSN;ySN;zSN];
MAT(:,Sc+1) = SN;
plotPoint(SN,C); %=====================
varNames = [varNames,"DELTA","S_{"+string(N)+"}"];
SNprev = SN;
%CHECKING
% disp("=================================================")
% A = [[LN(2:4);1],-[S0(2:4);1]]
% A \ dx_
else
%Get L_{N-(i-1)} from LN
LSD = getrLine(SNprev,DELTA);
[xLnow,yLnow] = intersectLines(LSD,OFfunc);
drawLine(SNprev,DELTA,Orang) %=====================
zLnow = 1 - xLnow - yLnow;
Lnow = [0;xLnow;yLnow;zLnow];
MAT(:,Sc) = Lnow;
plotPoint(Lnow,C); %=====================
Snow = [0;xSnow;ySnow;zSnow];
MAT(:,Sc+1) = Snow;
plotPoint(Snow,C) %=====================
MAT(1,Sc-1) = frSnprev;
MAT(1,Sc) = frLnow;
varNames = [varNames,"L_{"+string(N-(i-1))+"}","S_{"+string(N-(i-1))+"}"];
else
varNames = [varNames,"L_{"+string(N-(i-1))+"}","S_{"+string(N-(i-1))+"}"];
end
SNprev = Snow;
end
end
%Tabulate
TA = table(convertStringsToChars(string(varNames.')));
TAB = splitvars(TB);
TABd = splitvars(TBd);
TAB = [TA,TAB];
TABd = [TA,TABd];
TAB.Var1 = categorical(TAB.Var1);
TAB.Properties.VariableNames = ["Stream","Flowrate","x","y","z"];
TABd.Var1 = categorical(TABd.Var1);
TABd.Properties.VariableNames = ["Stream","Flowrate","x","y","z"];
disp(TABd)
%Plot remaining stuff
plot(XX,OFfunc(XX),"-","Color",[0 128 0]./255,"MarkerSize",15)
UFcurve = r_removePoints([XX.',UFfunc(XX.')],[UFfunc(XX.')],@(x) x<0);
plot(UFcurve(:,1),UFcurve(:,2),"-","Color",[0 128 0]./255,"MarkerSize",15)
line([0 0],[0 1],"LineStyle","-","Color",[0 0 0],"MarkerSize",10)
line([0 1],[0 0],"LineStyle","-","Color",[0 0 0],"MarkerSize",10)
boundX = [min(0,xDELTA) max(1,xDELTA)].*1.1;
boundY = [min(0,yDELTA) max(1,yDELTA)].*1.1;
xlim(boundX)
ylim(boundY)
end
end
function fy = getrLine(Sx,Lx)
fy = r_polyfits([Sx(2) Lx(2)],[Sx(3) Lx(3)],1,'func','off');
end
function plotPoint(Sx,C,varargin)
plot(Sx(2),Sx(3),'.',"Color",C,"MarkerSize",20)
end
function drawLine(Sx,Lx,C)
line([Sx(2) Lx(2)],[Sx(3) Lx(3)],"Color",C,"LineStyle","--")
end
function C = getColor(i,N)
Blue = [0 0 255]/255; %start at blue
Red = [255 0 0]/255; %gets redder as it gets to the end
C = Red*(i/N)+Blue*(N-i)/N;
end
function Vv = r_verticalize(Vh)
%VERTICALIZE Vertical vector
% Vv = r_verticalize(Vh), turns the vector Vh into a vertical
% vector Vv only if Vh is horizontal.
[r,c] = size(Vh);
if (r==1)&&(c>1)
Vv = Vh.';
else
Vv = Vh;
end
end
function fy = r_polyfits(X,Y,N,varargin)
%POLYFITS Polynomial Fit-Symbolic
% fy = r_polyfits(X,Y,N), obtains the symbolic equation of the fitted
% polynomial
% fy = r_polyfits(X,Y,N,'func'), obtains fy as a matlab function.
syms x y
C = polyfit(X,Y,N);
Xs = x.^(N:-1:0);
Ys = sum(Xs.*C);
fy = y == Ys;
else
fymf = matlabFunction(rhs(fy));
disp("Residuals:")
disp(fymf(X)-Y)
end
end
function M = r_removePoints(varargin)
%REMOVEPOINTS Conditionally Remove Points
% M = r_removePoints(M,f), evaluates f for every value in the row of
% M. If f returns true on any value in a row, removes the whole row of M.
% M = r_removePoints(M,N,f), evaluates f on N instead.
switch (numel(varargin))
case 2
M = varargin{1};
N = M;
f = varargin{2};
case 3
M = varargin{1};
N = varargin{2};
f = varargin{3};
otherwise
disp("Error, invalid number of arguments!")
return
end
[r,~] = size(M);
for i=r:-1:1
F = f(N(i,:));
if sum(F)>=1
M(i,:) = [];
end
end
end