# 5.5.2: Mutiple Inputs Example- Ideal Gas law (Lambert)

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

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.
% Variables
% n: number of mols
% R: universal gas constant
% T: temperature
% V: volume (array)
% P: pressure (array)
% calculation
for i = 1:length(V)
P(i) = (n*R*T)/V(i);
end
end

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);
plot(V,P)
hold on;
end
% format plot
title('Ideal Gas Law Isotherms')
xlabel('Volume (mˆ3)')
ylabel('Pressure (Pa)')

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.

.

This page titled 5.5.2: Mutiple Inputs Example- Ideal Gas law (Lambert) is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.