Using Global Variables Matlab Help

Using Global Variables
Engineers often need to estimate the pressures and volumes of a gas in a container. ideal gas law provides one way of making the estimate. The law is . RT I p = V More accurate estimates can be made with the van der Waals equation: RT a p= V -b V2
where the term b is a correction for the volume of the molecules, and the term a/y2 is correction for molecular attractions. The gas constant is R, the absolute temperature is T and the gas specific volume is Y. The value of R is the same in both equations and for gases; it is R = 0.08206 L atmosphere/mol K. The values of a and b depend on the type of gas. For example, for Chlorine (Ch), a = 6.49 and b = 0.0562 in these units. Write two user-defined functions, one to compute the pressure using the ideal g law, and one using the van der Waals equation. Develop a solution without using global variables; and one using global variables .
Solution
We can write the following function for the ideal gas law:

function P = ideal_l(T,Vhat,Rl
P = R*T. Mat; .
Note that we should use array division because the user might call the function using Vha t as a vector. However, we need not use array multiplication between R and T because R will always be a scalar. This function is independent of the units used for T, Y, and R, as long as·they are consistent with each other. The difficulty with this function is that we must always enter the value of R when we call the function. To avoid this, we can rewri e’the function as follows:
function P = ideal_2(T,Vhatl % This-requires liter, atmosphere,
R = 0.08206;
P = R*T.Vhat;
and mole units!
Now the value of R is “hard-wired” into the function. However, the danger with this approach is that the value of R depends on the units being used for Y and T. For example, in SI metric units, R = 8.314 J=mol So if the user enters values for Vhat and T in SI units, the computed value of P will be incorrect. Thus it is a good idea to put a comment to that effect within the function.However, it is not necessary to read the file in order  call the function, and users will not see the comment unless they read the file! A solution to this problem is to declare R a global variable. Then the function file would be: function P = ideal_3(T,Vhat global R
P = R*T Vhat;

With this approach, you must then declare R global wherever you call the function, either from the base workspace or from another function. For example, the following session computes the gas pressures in gases having V = 20, at two temperatures: T = 300 K and

330 K, respectively. ,
»global R
»R = 0.08206;
»ideal_l([300,330) ,20)
ans =
1. 2309 1.3540
The pressures. are approximately 1.23 and 1.35 atmospheres. With this approach, you must always enter the value of R, but this might force you to think about what units are being used for vhat , T, and P. If you are sure that you will always use the same units, then the simplest approach is to hard-wire the value of R into the function, as was done with ideal_2. In the van der Waals equation, the constants a and b depend on the particular gas. A
function r that is independent of the u~t.s used and the particular gas is function = v der waals_l(T,Vhat,R,a,b)
P = R*T./(Vhab)-a./Vhat.A2; The difficulty with this function is that you must always enter the values of R, a, and b. If you use the equation to analyze chlorine only, then you can hard-wire the constants into the function along with the value of R, as follows, but if you do so be sure you always use the proper ,units in the function’s arguments! function P = v der waals_2 (T, Vhat) % For chlorine only, and liter, atmosphere, and mole units!
R 0.08206;
a = 6.49;b = 0.0562;
P = R*T (Vhat-b) a Vhat.A2;
Note that the variable P is local to all these functions, and is available only if you assign it a value when calling the function. For example, consider the following session. > der waals_3(300,20)
ans
1.3416
»P
?? Undefined function or variable ‘P’.
»P = der waals_3(300,20)
P
1.3416
. In addition to using the global command to make values accessible within functions, we can also use it to make global variables that would otherwise be local. For .example, suppose we want to make the chlorine value of and b available to other functions. Then we could modify the der waals_2 function by adding the statement global

a b. Then any other function declaring a and b global would have access to those
values.

Posted on July 30, 2015 in function and files

Share the Story

Back to Top
Share This