You are on page 1of 19

LECTURE 1-2

LINUX PROGRAMS
March 8, 2016
Outline
• Gnuplot (http://lowrank.net/gnuplot/datafile-e.html#3dim)
Basic plot 2D and 3D
Parametric plot
Data fitting
• Postscript File
• Graphic File Conversion
• Shell Script Programming
2D Data Plot by gnuplot
Example of 2D data
file

• Comment line
starting by #
• Multiple columns
supported
• Each data is
separated by a
space
2D Data Plot by gnuplot

use the first and third columns for x-


and y-axes, respectively

2D plot command

Plot with lines

File name enclosed by single quotation marks


2D Function Plot
• Simply replace the ‘filename’ by a function: no ‘ ‘ required.
e.g.) pl sin(x)

• You can pre-define multiple functions


e.g.) a=3; b=2; f(x)=a*sin(x); g(x)=b*cos(x); pl f(x), g(x)
Note) Each command separated by ‘;’ can be typed in new lines by ‘enter’
Functions are separated by a comma. It’s the same for the multiple data files.

• Available functions
sin, cos, tan, log, log10, exp, sqrt, asin, acos, atan, …

• How to represent the exponents?


x**5=x*x*x*x*x, non-integer number can be used for the exponent.

• To read manual page for any option, type ‘? Option_name’

• To find all available pre-setting options, type ‘set’ and enter.


3D Data Plot
Example of 3D data
file

• Comment line
starting by #
Block of x=0 • Multiple columns
supported
• Each data is
separated by a
space
• Each block is
separated by a
empty line

Block of x=2.5e-7
3D Data Plot
• Basic command line for 3D data plot is similar to that of the 2D case
e.g.) spl ‘filename’ u 1:2:3 w l -> 3D surface plot with column 1 for x, 2
for y, and 3 for the value.
Note: For colored 3D surface plot, use ‘set pm3d; spl ‘file’ w pm’ instead
of ‘w l’ option.

• View angle
set view angle1, angle2
Note: angle1 is the rotation angle along ‘horizontal’ line of the screen.
angle2 is the rotation along ‘vertical’ line on the screen.

• Contour plot
Type ‘set contour’, then do the usual 3D plot command with ‘w l’ option.
By typing ‘set nosurface’, you can see simultaneously the colored surface
and contour lines.
3D Function Plot
• Just use ‘spl’ instead of ‘pl’. Most of other options are used in the same way
as the 2D and 3D data plot.

• You can define multi-variable functions


e.g.) f(x,y)=x**2+y**2+3; set pm3d; spl f(x,y) w pm

• The data file and function can be plotted together


Data Fitting
• Define a formula with undetermined variables for fitting data

• Type ‘fit f(x) ‘data’ u 1:3 via a,b,c,…’


Here a,b,c,.. Fitting parameters. Fit the 1st and 3rd columns’ data in the ‘data’ file
by the user-defined function f(x).
e.g.) f(x)=a*log(x)+b*x+c; fit f(x) ‘dataFile’ via a,b,c

• The fitting parameters are saved at ‘fit.log’ file.


PostScript File
• PostScript (PS) is a computer language for creating vector graphics.

• Gnuplot supports the plot figure in the ps format

• To save your plot as a ps-file, type ‘set term post {color} {solid}; set output
‘filename.ps’; replot
Here the ‘replot’ command means ‘plot again’ the previous plot. You need it to
send the plot result to the ‘file’ instead of to the screen.

• PS-file can be easily converted into pdf by ‘convert filename.ps filename.pdf’

• Actually it can be converted into jpg, gif, png files by ‘convert’ command.

• PS-file is basically a text-file, so it can be directly edited by vi editor.


Gnuplot Script

• You can write any gnuplot command in a separate file.


• To run those commands, in the gnuplot prompt, just do it like
gnuplot> load ‘scriptfile’

• In this way, you can keep all the complicated set of commands.
Shell Script Programming

Variables
No data type. No need to declare a variable, just assigning a value to its reference will
create it.
e.g.)
#!/bin/bash
STR="Hello World!"
echo $STR
A line starting with ‘#’ is a comment line
Value of a variable can be a numbe or, string (enclosed by “ “).
The value can be addressed by $.
Shell Script Programming
Conditionals
Sample 1: Basic conditional example if .. then ... else

#!/bin/bash
if [ "foo" = "foo" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi

Sample 2: Conditionals with variables

#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
Shell Script Programming
Loops for, while and until While sample
For sample
#!/bin/bash
#!/bin/bash COUNTER=0
for i in $( ls ); do while [ $COUNTER -lt 10 ]; do
echo item: $i echo The counter is $COUNTER
done let COUNTER=COUNTER+1
done
C-like for
#!/bin/bash Until sample
for i in `seq 1 10`;
do #!/bin/bash
echo $i COUNTER=20
done until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
Shell Script Programming
• To run the script, save the script into a file of any name

• Type ‘sh script’

• Or you change the file mode to executable. Then just type the script file
name.

• User’s arguments can also be accessed by $1, $2, …


Here $1 means the 1st argument, etc.
Exercise
Exercise 1
Plot following functions over the x-range from -1 through 1.
y = 2x1.5 y = 2e - x 2 /4
y = 2sin3/2 x - x cos1/2 x
Exercise 2
Plot in 3D the following functions over the x and y-ranges from -1 through 1
following the format as below.
1) Plot by a mono-colored mesh-surface (you do it just by spl func w l
2) Colored surface by pm3d
3) Draw contour lines
4) Removing the surface plot, leave just the contour lines as a top view.

z = (6x 2 +1)(2 - 4y2 )


Exercise 3
Create a data file with two columns of any numeric data. Just 5 to 10 points may be
enough. Plot in 2D your own data file with line, point, and linespoint.

Exercise 4
Try to fit your data by an appropriate polynomial function of any order.
Exercise
Exercise 5
Save your plot in Exercise 4 in a ps-file.

Exercise 6
Make a bash-script file, which creates directories dir1, dir2, dir3, dir4, dir5, …, dir20,
and copies your data file made in Exercise 4 into each directory. Use for-loop
combined with seq command, for which the example can be found in page 14. List
the files in each directory you made using ls dir* If you have checked everything is
done fine, then revise your script so that it removes the directories dir1, dir2, … and
the files inside them.
Homework
Question 1
Make a gnuplot-script file, which plots a color 3D hemisphere and saves it as a ps-
file. Convert the ps file to a pdf file. Send the script and the pdf file to TA by an
email.

Question 2
Make a bash-script file, which gets two numbers as arguments and yields the
product of two numbers multiplied by 1,2,3, …., 100. For example, if the script
name is sm, you run it by sh sm 2 5 (see two numbers as arguments). Then it
should show on the screen
10 20 30 …. 1000

Send the script to the TA by an email.

For any homework submission by email, indicate your name


and student number in the email title.
Due date: 24:00 Mar. 8 Tuesday 2016
To whom: Hyun-Taek Lee, diolet@unist.ac.kr
END OF
LECTURE 1-2

You might also like