By Adam Lambert, 8.3 Practical Functions
In most cases we're going to put more than one or two lines of code in a function. Typically, we isolate portions of code that perform a specific task and then package them in a function which is built to be as adaptive as possible.
Consider the Ideal Gas Law.
PV = nRT
In earlier chapters we plotted pressure vs: volume for several different temperatures. This nested calculation is a great time to use a function. We can create a function which will accept n,R,T, and array for V as input variables and then return the pressure array.
First, we create the function in a new m-file. Remember that the name of the function and the name of the m-file must match. I'm going to name my function ideal_gas and save the file as ideal_gas.m. The function will accept 4 values as inputs and return one value. The output value and one of the input values will be arrays, but MATLAB will figure that out automatically. Inside the function, we will loop through the volume array and calculate each element of the pressure array.
It is good practice to give a clear description at the top of any non-trivial functions. In some cases it may be appropriate to include more detailed instructions.
function P = ideal_gas(n,R,T,V)
% This function uses the ideal gas
% law to calculate pressure over a
% given volume array.
% Pay careful attention to units.
% n: number of mols
% R: universal gas constant
% T: temperature
% V: volume (array)
% P: pressure (array)
for i = 1:length(V)
P(i) = (n*R*T)/V(i);
Notice that a single line of code is used to calculate the P array. The for-loop and all of the indexing has been packaged into the ideal gas function. The code is tucked away in a separate m-le, so we don't have to worry about breaking it with accidental editing. Also, the single line of code is much easier for the user the interpret.
Because the function Workspace and the main Workspace are completely separate, we can use the same variable names in both the testing script and the function. This improves readability and interpretability. However, because the function only accepts values and not the variables themselves, it is important to remember the order that is required when we call the function. The function will always take the first value and assign it to the variable n. The second will always be assigned to the variable R. If we pass the values in a different order, then MATLAB will perform the calculation with the values assigned to the wrong variable. This is one reason why it is good practice to provide clear instructions at the top of a function, including variable definitions.
We can write a short loop to plot isotherms like we made in Chapter 6. This time our script will be much simpler and easy to read.
% This script utilizes the
% ideal_gas function to plot
% ideal gas law isotherms.
clear all; close all; clc;
% number of mols
n = 1;
% gas constant (J/(mol*K))
R = 8.314;
% temperature (K)
T = 300:100:700;
% volume (mˆ3)
V = [0.01:0.01:0.1];
% function call
% plot inside loop
for i = 1:length(T)
P = ideal_gas(n,R,T(i),V);
% format plot
title('Ideal Gas Law Isotherms')
legend('T= 300','T= 400','T= 500','T= 600','T= 700')
Rather than interpreting a nested loop, we can easily see that we are looping through the temperature values
and calculating the pressure-volume curve for each one.