Hi,
How can I do the iteration for the follwoing Ideal Gas Law? I would like to use the Vn and get the Pn value and compare the Ln value to the L. When Ln and L are very close, it will stop the iteration. I would really appreciate your help.
P=101325;
R=8.314;
r=20*10^-6;
L=117*10^-6;
V=pi*r^2*L;
T=300;
n=(P*V)/(R*T);
% Iteration should start from here
Tn=300+273;
Pn=(n/V)*R*Tn;
stress=Pn;
E=69.22*10^9;
S=stress/E;
dL=S*L;
Ln=L+dL;
Vn=pi*r^2*Ln;

1 comentario

Walter Roberson
Walter Roberson el 3 de Mzo. de 2021
Please note that I am not a category of knowledge, and so should not be named in a tag.

Iniciar sesión para comentar.

 Respuesta aceptada

Cris LaPierre
Cris LaPierre el 3 de Mzo. de 2021
Editada: Cris LaPierre el 13 de Mayo de 2021
Iterations can be done using either for loops or while loops.
If you want to repeat until a condition is met, a while loop is probably the best option. You just need to set up a conditional statement that evaluates as false until your statement is true. For example
L=1;
Ln=5;
while abs(Ln-L)>0.1
Ln=Ln-1
end
Ln = 4
Ln = 3
Ln = 2
Ln = 1
There is one thing to be careful of. It is possible to create what is called an infinite loop. This occurs when your contidional statement is always false.

5 comentarios

Sohel Rana
Sohel Rana el 3 de Mzo. de 2021
Thank you for your response. How can I use the new Vn value every time? Note that Pn depends on Vn and vice-versa. The V will be replaced by Vn every time.
Tn=300+273;
Pn=(n/V)*R*Tn;
stress=Pn;
E=69.22*10^9;
S=stress/E;
dL=S*L;
Ln=L+dL;
Vn=pi*r^2*Ln;
Cris LaPierre
Cris LaPierre el 3 de Mzo. de 2021
I would suggest setting Vn equal to V before the loop starts, and then only use Vn inside the loop. Each loop will use the previous value of Vn. It would be something similar to what I did with Ln in my previous example.
...
Vn = V;
while abs(Ln-L)>0.1
Pn=(n/Vn)*R*Tn;
Vn=pi*r^2*Ln;
end
Sohel Rana
Sohel Rana el 3 de Mzo. de 2021
Thank you so much. In the while loop, there is no value for Ln. How the loop will know about the Ln value?Should I set Ln=L before the loop?
Cris LaPierre
Cris LaPierre el 3 de Mzo. de 2021
Editada: Cris LaPierre el 3 de Mzo. de 2021
I haven't given you the whole solution. In the code you shared originally, you calculate Ln right before you use it.
Ln=L+dL;
Vn=pi*r^2*Ln;
If you need to use the current value of Ln as L in the next loop, then yes, you should set Ln=L before the loop, and update all uses of L inside the loop to be Ln.
If you don't understand how loops work, I suggest looking at Ch 13 of MATLAB Onramp. It covers for loops, not while loops, but the underlying principles are the same.
Sohel Rana
Sohel Rana el 3 de Mzo. de 2021
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Mzo. de 2021

Editada:

el 13 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by