MATLAB: How to go about writing a loop to solve for the unknown Temp that would make z = 1

for loop

xe = 0.35;
xw = 0.25;
xb = 0.40;
Pe = exp(18.5242 - ((3578.91/(T - 50.5))));
Pw = exp(18.3036 - ((3816.44/(T - 46.13))));
Pb = exp(15.9008 - ((2788.51/(T - 52.36))));
Ke = Pe/760;
Kw = Pw/760;
Kb = Pb/760;
ye = (Ke * xe)/100;
yw = (Kw * xw)/100;
yb = (Kb * xb)/100;
z = ye + yw + yb

Best Answer

Turn the code into a function:
function z = myfun(T)
xe = 0.35;
xw = 0.25;
xb = 0.40;
Pe = exp(18.5242 - ((3578.91/(T - 50.5))));
Pw = exp(18.3036 - ((3816.44/(T - 46.13))));
Pb = exp(15.9008 - ((2788.51/(T - 52.36))));
Ke = Pe/760;
Kw = Pw/760;
Kb = Pb/760;
ye = (Ke * xe)/100;
yw = (Kw * xw)/100;
yb = (Kb * xb)/100;
z = ye + yw + yb;
end
Then simply use fzero like this:
>> T = fzero(@(t)myfun(t)-1,[100,1000])
T = 577.051115181759
>> myfun(T)
ans = 1
You can see the location of where z=1 by plotting the curve:
>> x = 1:10000;
>> y = arrayfun(@myfun,x);
>> loglog(x,y,x([1,end]),[1;1])
>> hold on
>> plot(T,1,'*g')
>> ylim([1e-100,1e+100])
giving: