function command hellp

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

Answers (2)

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 Comments

wht are these dots ?
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
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
r u there/?
I'm starting a new answer

Sign in to comment.

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 Comments

this is not seems to be working , can I give you my codes
this is not seems to be working , can I give you my codes
% 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)
% 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
I am waiting for your reply ?
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.
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
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;
yes i did and what will be the 2nd file
That's your function. Then in the command line, do
[R,T,Z,A,B] = foo
and you should get those values
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
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
thats it thats wht i am looking for. thx so much

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 14 Feb 2012

Edited:

on 12 Oct 2013

Community Treasure Hunt

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

Start Hunting!