Storing Numbers from Function in a Script
Mostrar comentarios más antiguos
I have a function designed to output thermodynamic quantities. I created it as a function, as I felt that was easiest at the time. I am trying to call the function in a script, which works successfully. My function is outputting numbers, and the right numbers. However, I want to vary T and p over a range, and store these numbers, and when I tried to store the numbers, all I got was 1X a large number complex double, and all rows and columns were 0. I will put my current code below (the one that calls the function successfully but does not store the numbers) and below that, I can put what I was trying that gave me the complex double with all zeroes.
Script that successfully calls the function and produces accurate numbers that aren't stored;
z = 1;
p = 100000:1:10000000;
T = 153:1:423;
pc = 5050000;
Tc = 154.6;
w = 0.022;
k = 0;
cpig = [26 0.01 0.005 0.0000013];
DHf = 0;
DGf = 0;
ph = 'V';
flag = 'PR';
for T = 153:1:423
for p = 100000:1:10000000
[rho,Z,phi,A,S,H,U,G] = prsrk(z,p,T,pc,Tc,w,k,cpig,DHf,DGf,ph,flag);
end
end
Code that gives me complex doubles full of zeroes;
z = 1;
p = 100000:1:10000000;
T = 153:1:423;
pc = 5050000;
Tc = 154.6;
w = 0.022;
k = 0;
cpig = [26 0.01 0.005 0.0000013];
DHf = 0;
DGf = 0;
ph = 'V';
flag = 'PR';
IntEng = [];
Gibbs = [];
Enthalpy = [];
Entropy = [];
for T = 153:1:423
for p = 100000:1:10000000
[rho,Z,phi,A,S,H,U,G] = prsrk(z,p,T,pc,Tc,w,k,cpig,DHf,DGf,ph,flag);
Gibbs(T,p) = G;
IntEng(T,p) = U;
Enthalpy(T,p) = H;
Entropy(T,p) = S;
end
end
3 comentarios
Rik
el 4 de Mzo. de 2017
Take a look here: https://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Correctly formatting your code makes it easier for other to see what happens and to spot potential errors.
What I'm noticing is that you are overwriting the values rho,Z,phi,A,S,H,U and G. So the thing you can try is changing that to rho(T_index,p_index) etc.
Jerome Campbell
el 4 de Mzo. de 2017
Editada: per isakson
el 5 de Mzo. de 2017
You are assigning a large matrix (G) to a single position in Gibbs. Does the loop below yield the results you need?
for T = 153:1:423
for p = 100000:1:10000000
[rho(T,p),Z(T,p),phi(T,p),A(T,p),S(T,p),H(T,p),U(T,p),G(T,p)] ...
= prsrk(z,p,T,pc,Tc,w,k,cpig,DHf,DGf,ph,flag)
%
Gibbs(T,p) = G(T,p);
IntEng(T,p) = U(T,p);
Enthalpy(T,p) = H(T,p);
Entropy(T,p) = S(T,p);
end
end
If so, I'll suggest another modification and move this to the answer.
Respuesta aceptada
Más respuestas (1)
Sonam Gupta
el 7 de Mzo. de 2017
Using the indices as T and p directly for storing IntEng, Gibbs, Enthalpy, Entropy array is what is creating a large array of zeros. Changing the indices such that they begin from 1 should work. I suggest you to make changes to your code as below:
p_offset = 99999; % 100000 - 1, so that after subtracting the offset, your index begins from 1
T_offset = 152; % 153 - 1 , similar thing for T also
for T = 153:1:423
for p = 100000:1:10000000
[rho,Z,phi,A,S,H,U,G] = prsrk(z,p,T,pc,Tc,w,k,cpig,DHf,DGf,ph,flag);
Gibbs(T - T_offset,p - p_offset) = G;
IntEng(T - T_offset,p - p_offset) = U;
Enthalpy(T - T_offset,p - p_offset) = H;
Entropy(T - T_offset,p - p_offset) = S;
end
end
I hope this helps in resolving the issue.
1 comentario
Jerome Campbell
el 8 de Mzo. de 2017
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!