COMPUTATIONAL TOOLS
Graphing Tools
We need to plot data. We'll use a package called gnuplot
(pronounced "new-plot").
-
Open terminal window
-
Type gnuplot<return>
-
plot
-
set xrange, yrange
-
set log, nolog, set log x[y], set nolog x[y]
-
set grid, nogrid, key, nokey
-
set xlabel, ylabel, title
-
printing - set term post, set output 'filename.ps', plot,
set output, set term x11
Exercise #1
Plot the following data by first copying to a file,
and then using gnuplot to read and plot from that file.
1 -13.600
2 -3.400
3 -1.511
4 -0.850
5 -0.544
6 -0.378
7 -0.278
8 -0.213
9 -0.168
10 -0.136
High Level Languages
-
Computers speak machine language, thousands of 1s and 0s.
-
We speak english (or some such human language).
-
We need some way to get the computer to understand and carry
out our instructions.
Answer? COMPRIMISE
-
We right down our instructions in a highly structured way
called a high level language.
-
The computer uses an interpreter to take the high level
language and translate it into machine language.
The set of instructions in the high level language is called
the source code.
The interpreter that translates the source code into machine
code is called the compiler.
High Level Languages
-
FORTRAN (FORTRAN 77, FORTRAN 90)
-
C, C++
-
BASIC
-
PASCAL
-
JAVA
-
etc . . .
Simple Programming
Skeleton Code
-
program name
-
variable declarations
-
initialize variables
-
calculations
-
input/output
-
end program
Example 1
program picalc
c This is a program that calculates
the value of pi
c using the intrinsic funtion asin.
The value of pi
c written to the screen after it
is calculated.
real pi
pi = 2.0 * asin(1.0)
print *, 'pi = ',pi
end program picalc
In C++ this program looks like this...
(See the online notes
from CSC 114)
//This is a program that calculates the value of pi
//using the intrinsic funtion asin. The value of pi
//is written to the screen after it is calculated.
#include < iostream> // this is needed for output to screen
#include < math.h> // this is needed for the asin function
int main(){
double pi;
pi = 2.0 * asin(1.0);
cout << "pi = " << pi << endl;
}
Example 2
program deg2rad
c This program calculates the radian
equivalent of
c angles in degrees, for every
10 degrees from 0 to 180.
c This data is written to a file
called 'data.dat' on unit 10.
real pi, degs, rads
pi = 2.0 * asin(1.0)
open(unit=10,file='data.dat')
do degs = 0.0, 180.0, 10.0
rads =
degs * pi / 180.0
write(10,*)
degs, rads
enddo
close(10)
end program deg2rad
// This program calculates the radian equivalent of
// angles in degrees, for every 10 degrees from 0 to 180.
#include < iostream>
#include < math.h>
int main(){
double pi, rads;
pi = 2.0 * asin(1.0);
for(double degs = 0.; degs <= 180.; degs += 10.;){
rads = degs * pi / 180.;
cout << degs << " " << rads << endl;
}
}
Intrinsic Functions
-
x + y, x - y
-
x * y, x / y
-
sin(x), asin(x)
-
cos(x), acos(x)
-
tan(x), atan(x)
-
log(x), exp(x)
-
log10(x)
-
x**y (xy)
Compiling &
Executing Code
-
Edit source code in editor
-
save code to file source_file.f or source_file.cc
-
compile code with: f77 -o executable_name source_file.f
compile code with: g++ -o executable_name source_file.cc
-
execute code with executable_name
Exercise #2
-
Write a program to output y = sin(x) from 0
to 180o to a file called sinewave.dat.
-
Remember that trigonometric intrinsics take arguments in
radians, not degrees.
-
Plot the data in gnuplot using the command plot 'sinewave.dat'.
-
Plot the data on the same graph with the analytic function
sin(x) with something like plot 'sinewave.dat', sin(x) (but
not exactly!).
-
Set the title and axis labels, and save a copy of the graph
to a file called sinewave.ps.
LaTeX
-
LaTeX is much like a high level computer language.
-
You provide a source file describing a document you wish
to have created (typeset).
-
LaTeX is much like the compiler, and turns the source into
a finished document.
HTML
-
Again, HTML is a language. You descibe a web page with a
source file, someone's browser builds the web page from it.