## Notebook 1.0: Basic calculations and plotting with Euler

January 14, 2010Here is the first draft of the symbolic math notebooks I used in my Fall ’09 PChem class. You’ll need the free, open source Euler Math Toolbox to execute it. After you install the toolbox, just cut and paste the following code into a plain text file, save it with an .en file extension, and double-click the file. It should open up in the Toolbox.

All of the notebooks in this series are specifically keyed to Atkins’ Physical Chemistry, 8th edition.

As you can see, Euler makes decent graphs, and saves them as png files that are easily pasted into other documents and web pages. The syntax is clumsy. I’ve used Unix since I was a kid, so I’ve always been most comfortable with command line interfaces, but it’s tough sledding for some students. I wish Euler had a graph-making wizard.

`---------------------------------snip here-----------------------------
Notebook 1.0: Basic calculations and plotting with EULER
%
% Please read Sections 1.1-1.2 in Atkins before working through
% this notebook.
%
% This notebook will introduce basic features of EULER that will help
% you complete Task 1. You can execute the commands in this notebook
% one by one by pressing the return key, when the cursor is on the
% command line. You can also travel from command to command using the
% cursor keys.
%
% EULER (pronounced 'oiler') works like a calculator. You enter an
% expression or command, press return, and the expression or command
% is evaluated.
%
% For example, the pressure P caused by a column of fluid with density
% d and height h can be calculated from
%
% P = dgh
%
% where g is the acceleration due to gravity. You can calculate the
% pressure in Pascals at a depth of 10 m under seawater with a density of
% 1020 kg/m3 as follows:`

>1020*9.8*10

99960

% We can write the expression a little more clearly by using named

% variables:

>d=1020;

>g=9.8;

>h=10;

>P = d*g*h

99960

% If a command is followed by a semicolon (;), EULER does not print

% its output. We can enter more than one command in a line. To get

% the pressure at 20 m in atm, we can write:

>h=20; P=d*g*h

199920

% To generate a table of pressures at 0.1 m intervals from 0 to 20 m,

% we can make an array of heights as follows:

>h=0:0.1:20;

% where we've put a semicolon on the end of the line to suppress

% the output of the array of heights 0, 0.1, 0.2, ..., 20. The :

% command has the form

%

% x = start : increment : last

%

% where start is the first value in the array, increment is the space

% between points in the array, and last is the last value in the array.

%

% We can use the array of h values now to make an array of P values:

> P = d*g*h;

% We can plot the pressure values as a function of h using the plot2d()

% function. The call looks like plot2d(x,y,options) where x and

% y are data arrays. We'll look at the different options for plot2d

% later.

%

> plot2d(h,P);

% The plot is drawn in a separate window. You can press the TAB key to

% toggle between the two windows.

%

% You can give the plot a title:

> title("Dive pressure as a function of depth");

% You can also add x and y axis labels:

> xlabel("depth (m)");

> ylabel("pressure (Pa)");

% So that you don't have to keep tabbing back and forth between the text

% and plot windows, you can insert the plot right into the notebook with

% the 'insimg' (insert image) command:

% Now let's use Euler to show how pressure, temperature, and molar volume of

% an ideal gas are related. Let's try to look at molar volumes from 0

% to 1 L/mol:

>V = 0:0.01:1;

>R = 0.082059;

>T = 250;

% When we try to calculate P from R, T, and V, we run into a problem (try it):

>P = R*T/V;

Floating point error!

error in :

P = R*T/V;

^

% The trouble is that one of the V values is zero, and division by zero

% is not allowed. We'll have to start with a small (but nonzero) volume:

>V = 0.001:0.01:1;

>plot2d(V,R*T/V);

% We can add curves for different temperatures to the same plot using

% plot2d's add=1 option. We can make each curve a different color using

% the color option:

>T = 500; plot2d(V,R*T/V,add=1,color=10);

>T = 1000; plot2d(V,R*T/V,add=1,color=11);

>T = 2000; plot2d(V,R*T/V,add=1,color=12);

% We can add titles and axis labels, and insert the image into this

% notebook:

>title("Boyle's Law Isotherms");

>xlabel("Volume (L)");

>ylabel("Pressure (atm)");

>insimg;

% It's tough to see the curves with the full range of pressures shown

% on the y axis. The plot2d function can redraw the plot with any fixed

% range for the x and y values. The parameters a and b set the minimum

% and maximum x axis values; c and d set the minimum and maximum y axis

% values.

>Pmax = 1000;

>Vmax = 1;

>T=250; plot2d(V,R*T/V,a=0,b=Vmax,c=0,d=Pmax);

>T=500; plot2d(V,R*T/V,a=0,b=Vmax,c=0,d=Pmax,add=1,color=11);

>T=1000; plot2d(V,R*T/V,a=0,b=Vmax,c=0,d=Pmax,add=1,color=12);

>T=2000; plot2d(V,R*T/V,a=0,b=Vmax,c=0,d=Pmax,add=1,color=13);

>title("Boyle's Law Isotherms");

>xlabel("Volume (L)");

>ylabel("Pressure (atm)");

>insimg;

`% Compare this graph to Figure 1.4 on page 7 of Atkins.
%
% In the space below, reproduce textbook figures 1.5, 1.6, and 1.7
% as closely as possible. Be sure to title each graph and label
% all axes, and set the axis minima and maxima so that the curves
% can be clearly seen. Display the graphs within the notebook, using
% the insimg function.
%
% Finally, save the notebook (by choosing File->Save Notebook) and
% upload it to the website before the due date.
>
---------------------------------snip here-----------------------------
`

That’s it.

You can see the key below the cut.

---------------------------------snip here-----------------------------

Notebook 1.0 Key

%

>R = 0.082059;

%

% Figure 1.5

%

>V = 0.01:0.01:5;

>T=250; plot2d(1/V,R*T/V); label("T = 250",80,2000);

>T=500; plot2d(1/V,R*T/V,add=1,color=1); label("T = 500",50,2000);

>T=1000; plot2d(1/V,R*T/V,add=1,color=2); label("T = 1000",25,2000);

>T=2000; plot2d(1/V,R*T/V,add=1,color=3); label("T = 2000",0,2000);

>title("Figure 1.5. Pressure depends linearly on 1/V at constant T");

>xlabel("1/Molar Volume (mol/L)");

>ylabel("Pressure (atm)");

>insimg;

`%
% Figure 1.6
%
>T = 0:5:1000;
>P = 1; plot2d(T,R*T/P);
>P = 2; plot2d(T,R*T/P,add=1,color=1);
>P = 4; plot2d(T,R*T/P,add=1,color=2);
>P = 8; plot2d(T,R*T/P,add=1,color=3);
>P = 16; plot2d(T,R*T/P,add=1,color=4);
>title("Figure 1.6. Volume depends linearly on temperature at constant pressure");
>xlabel("Temperature (K)");
>ylabel("Volume (L)");
>insimg;
%
% Figure 1.7
%
>V = 1; plot2d(T,R*T/V);
>V = 2; plot2d(T,R*T/V,add=1,color=1);
>V = 4; plot2d(T,R*T/V,add=1,color=2);
>V = 8; plot2d(T,R*T/V,add=1,color=3);
>V = 16; plot2d(T,R*T/V,add=1,color=4);
>title("Figure 1.7. Pressure depends linearly on temperature at constant volume");
>xlabel("Temperature (K)");
>ylabel("Pressure (atm)");
>insimg;
`

## Leave a Reply