I have a function file have code like this
--------------------------------------------
function GH = Gibbs(R,T,Z,A,B)
% Calculate the Enthalpy, Enropy, Gibbs free energy
GH = R*T*((Z-1)- log(Z-B)- A/B*log(Z+B/Z));
end
------------------------------------------
I have values of R, T, Z, A, B in another m-file , how I compute this by calling the other file for the values of R,T,Z,A,B

Antworten (2)

Honglei Chen
Honglei Chen am 14 Feb. 2012

0 Stimmen

Let's say the other file is foo.m, then you can modify the signature of foo to return those values, e.g.
function [...,R,T,Z,A,B] = foo(...)
Then you can call them in sequence like this:
[...,R,T,Z,A,B] = foo(...);
GH = Gibbs(R,T,Z,A,B);

5 Kommentare

Nasir Qazi
Nasir Qazi am 14 Feb. 2012
wht are these dots ?
Honglei Chen
Honglei Chen am 14 Feb. 2012
Those are whatever the original inputs and outputs are. If you don't have inputs and outputs, then it will reads like [R,T,Z,A,B] = foo
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
I really dont get that, The values or R, is fixed and rest of then are varying ... and the other file is not a function file and the file have values like R=1.2 and others T,Z,A,B are variable values depend on R. now I want to caluclate GH byt getting values from that other file who have the values of R,T,Z,A,B , could you not mind to write it in proper order in codes so i understand whats going on.thanks
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
r u there/?
Honglei Chen
Honglei Chen am 14 Feb. 2012
I'm starting a new answer

Melden Sie sich an, um zu kommentieren.

Honglei Chen
Honglei Chen am 14 Feb. 2012

0 Stimmen

Then I will rewrite the other file into a function, using signatures like
function [T,Z,A,B] = foo(R)
Once you do that, you can call them in order
[T,Z,A,B] = foo(R)
GH = Gibbs(R,T,Z,A,B)

13 Kommentare

Nasir Qazi
Nasir Qazi am 14 Feb. 2012
this is not seems to be working , can I give you my codes
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
this is not seems to be working , can I give you my codes
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
% This is the first file
% P = RT / V - b - alpha*a / V(V-b)
R = 8.314e-3; % MPa m3 / mole.K
n = 0;
Tr = 0.6
Tc = 540.13; % K
Pc = 2.74; % Mpa;
T = Tr*Tc;
w = 0.350;
Pn_i = 0.067;
m = 0.480+1.574*w -0.176*w^2;
al=(1+m*(1-Tr^0.5))^2;
a = 0.42748*((R*Tc)^2/Pc);
b = 0.08664*(R*Tc/Pc);
for i=1:100
% Z3 - Z2 + (A-B-B^2)Z-AB=0
A = (a*Pn_i*al)/(R*T)^2;
B = (b*Pn_i)/(R*T);
Z=roots([1 -1 A-B-B^2 -A*B]);
Zv_i=max(Z);
Zl_i=min(Z);
Zvc=isreal(Zv_i);
if Zvc == false
Zv_i=abs(Zv_i);
disp('error 1')
end
Zlc=isreal(Zl_i);
if Zlc == false
Zl_i=abs(Zl_i);
disp('error 2')
end
phiv_i=exp((Zv_i -1)-log(Zv_i -B)-(A/B) *(log((Zv_i+B)/Zv_i )));
phil_i=exp((Zl_i -1)-log(Zl_i -B)-(A/B) *(log((Zl_i+B)/Zl_i )));
Fv_i=Pn_i*phiv_i;
Fl_i=Pn_i*phil_i;
Error=(Fv_i-Fl_i);
if (abs(Error) < 10^-6);
disp(Pn_i) ;
break
else
n=n+1;
Pn = (Pn_i)*(phil_i/phiv_i);
Pn_i = Pn;
end
end
% Gibbs Free Energy
GH = R*T*((Zv_i -1)- log(Zv_i -B)-(A/B)*log(Zv_i + B/Zv_i));
disp('The values of Gh are: ')
disp(GH)
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
% this is a function file , the values of R,T,Z,A,B is taken from first file
function GH = Gibbs(R,T,Z,A,B)
% Calculate the Enthalpy, Enropy, Gibbs free energy
GH = R*T*((Z-1)- log(Z-B)- A/B*log(Z+B/Z));
end
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
I am waiting for your reply ?
Honglei Chen
Honglei Chen am 14 Feb. 2012
It looks like you want to calculate GH for each iteration? If that's the case, you need to break your first file into two. One of them just supply T,Z,A,B from R and the other one runs the loop. Then you can have a similar one to do a loop and calculate GH.
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
do this for me , just make the first file like this
R=1
T=20
A=3
B=1
R=4
--------------
%and the 2nd file like this
function GH = Gibbs(R,T,Z,A,B)
% Calculate the Enthalpy, Enropy, Gibbs free energy
GH = R*T*((Z-1)- log(Z-B)- A/B*log(Z+B/Z));
end
%the 2nd file will get the values of R,T,Z,A,B from first one .. does it make sense . just do this for me please
Honglei Chen
Honglei Chen am 14 Feb. 2012
You have to use a function to transfer these values out of the first file. Did you ever try making the first file like
function [R,T,Z,A,B] = foo
R=1;
T=20;
Z=4;
A=3;
B=1;
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
yes i did and what will be the 2nd file
Honglei Chen
Honglei Chen am 14 Feb. 2012
That's your function. Then in the command line, do
[R,T,Z,A,B] = foo
and you should get those values
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
I know that but I don't want to run it on command line , want to excute the results in another M-file and when I run it I can display the result u get my point
Honglei Chen
Honglei Chen am 14 Feb. 2012
Then you can call it within the other file
function GH = Gibbs
% Calculate the Enthalpy, Enropy, Gibbs free energy
[R,T,A,Z,B] = foo;
GH = R*T*((Z-1)- log(Z-B)- A/B*log(Z+B/Z));
end
Nasir Qazi
Nasir Qazi am 14 Feb. 2012
thats it thats wht i am looking for. thx so much

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 14 Feb. 2012

Bearbeitet:

am 12 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by