Using the result as a input in the loop again

1 view (last 30 days)
I am writing a program for a model which capture the moiture from the air. I am new to the Matlab
I attached the program below
clear
clc
aw=0.7;
Ps=(2.339);
RH=95;
Tgas=293;
Tsol=293;
H=18.015;
A=0.25;
%membrane resistance (P/t)
P=(0.00000000001206);
t=(0.000055);
K=(P/t); %Resistivity constant
Cl=123.9;
mW=100;
for x=1:60*60;
t(x)=x;
Ms=(Cl/164.0);
Mw1(x)=(mW+M(x)/18.0); ---input the previous result (x-1 ) here again in the loop to find the result for the current loop
Y=Mw1(x)+(3*Ms);
Xw(x)=(Mw1(x)/Y);
Pvl=Xw(x)*Ps*aw;
Pva=(RH.*Ps)/100;
X=(Pva-Pvl(x));
Gv(x)=(K)*(X(x));
M(x)=Gv(x)*H*t(x)*A; ---- Result of the current loop for the next interation
end
How can I use the result from the previous loop to do the calculation in current loop. I need help.Thanks!

Accepted Answer

Sindar
Sindar on 29 Jan 2020
Change
Mw1(x)=(mW+M(x)/18.0);
to
% first iteration assumes Mw1(0) = 0
if x==1
Mw1(x)=mW;
% later loops access the value from the earlier iteration
else
Mw1(x)=(mW+M(x-1)/18.0);
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!