Gnuplot exercise and solutions with basic knowledge

 This exercise will help you get acquainted with some of the basic features of Gnuplot, enabling you to solve problems related to computational aspects in different parts of your lab work. Here you will see the question of gnuplot of 2018-19 which came in B.Sc Physics Exam in West Bengal, India. You can use gnuplot for your various graph related tasks.

About Gnuplot: Gnuplot is a command-line and GUI base program that can generate 2D and 3D plots of functions, data, and data files. The program runs on all major computers and OS. This program is mainly written in C programming language.

Functions in Gnuplot: Gnuplot makes a graph of any kind of function or numerical data stored in a file.

File extension in Gnuplot: There is no official standard extension, but some semi-official extension are used in gnuplot: .plt (default in win OS), .gnu, .gpi, or .gih for general gnuplot files, dat for data, fnc for function definitions Etc.

For more information about Gnuplot: Full Information Here

Gnuplot officcial site: Go to site

All Gnuplot practice question set: Click Here

Q&A

Q1. Set a function f(x) = x^3 - 4x^2 - 8. Set range of x values  from -10 to +15 and range of y values [y is treated as f(x)] from -2000 to +2500. Plot this function in the range. Both x and y axis must be drawn on the graph. The x label will be "x value" and y label will be "my function". Store the output in a file and print it.

Solution:

gnuplot graph
Q1-Solution

Q2.Set a function f(x) = sin^2(x) + 0.7x. Set range of x values from -15 to + 15 and range of y values [y is treated as f(x)] from -1 to +2.2. Plot this function in the range. Show x axis on the graph. The x label will be "x value" and y label will be "sin(x)^2 + 0.7*x". The tics in the x axis will be at interval of 2.2. Store the output in a file and print it.

Solution:

gnuplot graph
Q2-Solution

Q3.f(x) = {(x, when x>=3.5), (x^2, when -3<=x<=3.5), (-3, when x<=-3). Set range of x value from -10 to 10 and range of y value [y is treated as f(x)] from -5 to +12. Keep tics in x axis at interval 2.5. Set x label as "x value" and y label as "jerk function". Plot the function, Store it in a file and print it.

Solution:

gnuplot graph
Q3-Solution
Q4.f
(x) = {(x, when x>=3.5), (x^2, when -3<=x<=3.5), (-3, when x<=-3). Set range of x value from -10 to 10 and range of y value [y is treated as f(x)] from -5 to +12. Keep tics in x axis at interval 2.5. Set x label as "x value" and y label as "jerk function". Plot the function, Store it in a file and print it.

Solution:

gnuplot graph
Q4-Solution

Q5.Plot sinh(x), cosh(x) and tanh(x) on same graph. The line width of the curves will be gradually increasing. That is, the line width of cosh(x) will be thicker than sinh(x) and the line width of tanh(x) will be thicker than cosh(x). Set the range of the x values from -6 to +6 and the range of the y values from -8 to +8. The x axis label will be "x value" and that of the y axis will be "hyperbolic function". Store the output in a file and give a printout.

Solution:

gnuplot graph
Q5-Solution

Q6.Show the polar plots of the functions sin(2t) and cos(2t) where t stands for angle. Put horizontal axis label as "t value" and vertical axis label as "the range". Put a label "parametric plot" at the left corner (-0.85, 0.85). Set the vertical axis passing through t=0. Save the plot in a file and give a printout.

Solution:

gnuplot graph
Q6-Solution

Q7.Set a function f(x) = 2x^3 + 9x - 11. Set range of x values from -15 to +10 and range of y values [y is treated as f(x)] from -7000 to +2200. Plot this function in the range. Indicate y axis on the graph. The x label will be "x value" and y label will be "new function". Set the tics in the x axis at interval 2.5. Store the output in a file and print it.

Solution:

gnuplot graph
Q7-Solution

Q8.Set a function f(x) = cos(x) - 0.5x. Set range of x values from -20 to 20 and range of y values from -10 to +12. Plot this function in the range. Show the x axis on the graph. The x label will be "x value" and y label will be "cos(x) - 0.5x". The tics in the x axis will be at interval of 2. Store the output and print it.

Solution:

gnuplot graph
Q8-Solution

Q9.Define the following function-  f(x) = {(x, when x>=2), (x^2, when -3<=x<=2), (-x, when x<=-3)} Set range of x values from -8 to 8 and range of y values from -2 to 12. Keep tics in x axis at interval 2. Set x label as "x value" and y label as "jerk function". Plot the function, store it in a file and give a printout.

Solution:

gnuplot graph
Q9-Solution

Q10.Define three functions in the given range-  f1(x) = x, when x>2. f2(x) = x^2, when -1<x<2. f3(x) = 2, when x<-1. Set the range of x value from -6 to 6 and the range of y values from -2 to 7. Set tick label of the y axis as "piecewise continuous function". Plot the three functions, store the output in a file and give a printout.

Solution:

gnuplot graph
Q10-Solution

Q11.Plot sinh^2(x), -cosh(x) and -{1+tanh(x)} on same graph. The line width of the curves will be gradually increasing. Set the range of the x values from -6 to 6 and the range of the y values from -8 to 8. The x axis label will be "x value" and that of the y axis will be "hyperbolic function". Store the output in a file and give a printout.

Solution:

gnuplot graph
Q11-Solution

Q12.Show the polar plots of the functions tan(6t) and cot(6t) where t stands for angle. Put horizontal axis label as "t value" and vertical axis label as "the range". Set the range of both the axes from -20 to 20. Put a label "tan(6t), cot(6t)" at the position (4, 12). Set the horizontal and vertical axes passing through t=0 and range=0. Save the plot in a file and give a printout.

Solution:

gnuplot graph
Q12-Solution

Q13.Define a function y = x sin(3x). Set the range of x values from -3 to 3 and the range of y values from -4 to 4. Show both the axes on the plot. The label of x axis and y axis will be "x value" and "y value" respectively. Ticks of the x axis will be at interval of 0.5. Ticks of the y axis will be at interval of 1. Plot the function and store the plot in a file and print it.

Solution:

gnuplot graph
Q13-Solution

Q14.Plot 2x*sin(2x), 1/2*x*sin(x/2) on the same graph. The line width of the two curves to be different. Set the plot. Ticks of x axis will be at interval of 0.5. The label of x and y axis to be "x value" and "y value" respectively. Store the plot in a file and print it.

Solution:

gnuplot graph
Q14-Solution

Q15.Define the following function f(x) = {(x^2, when x<1), (x, when 1<=x<=2), (1/4*x^3, when x>2)} Set the range of x value from 0 to 3 and the range of y values from 0 to 8. Ticks of x axis will be at interval of 0.5 and that of y axis will be at interval of 1. Set title "Polynomials of x". Plot the function and store the plot in a file and print it.

Solution:

gnuplot graph
Q15-Solution

Q16.Define three functions in the given range-  f1(x) = (1/x*e^x, when x>1), f2(x) = (x^2*tanh(x), -1<x<1), f3(x) = (1/x*e^(-x), when x<-1)  Set the range of x values from -3 to 3 and the range of y values from -7 to 7. Show both the axes on the plot. Set y label "Piecewise continuous function". Set ticks of the y axis at an interval of 1. Plot the three functions and store the plot in a file and print it.

Solution:

gnuplot graph
Q16-Solution

Q17.Show the parametric plot of the functions 2*sin(t), 2*sin(2t), where t is the parameter. Set the range of t values from -pi to +pi. The label of x and y axis will be "x = 2*sin(t)" and "y = 2*sin(2t)". set title "Lissajous Figure". Draw both the axes on the plot. Store the plot in a file and print it.

Solution:

gnuplot graph
Q17-Solution

Q18.Show the polar plot of the functions 4+3*cos(t) and 3+4*cos(t)  where t is the angle made with the reference directions. (+ve x axis). Show both the axes on the plot. The label of x and y  axis will be "x = r*cos(t)" and "y = r*sin(t)". Set title "A pair of Limacon". Put label "polar plot" at left corner at (-6,6). Store the plot in a file and print it.

Solution:

gnuplot graph
Q18-Solution

Q19.Define a function y = ln(x^2). Set the range of x values from 02 to 2 and the range of y values from -3 to 3. Show both the axis on the plot. The labels of x and y axis will be "x value" and "y value" respectively. Ticks of the x axis will be at interval of 0.5. Ticks of the y axis will be at interval of 1. Plot the function and store the plot in a file and print it.

Solution:

gnuplot graph
Q19-Solution

Q20.Plot sin(x)/x, sinh(x)/x and y = 1 on the same graph. The line width of the three curve to be different. Set the range of x values -pi to pi and the range of y values from 0 to 5. Show y axis on the plot. Set title "Plot of different functions of x". The labels of x and y axis to be "x value" and "y value" respectively. Store the plot in a file and print it.

Solution:

gnuplot graph
Q20-Solution

Q21.Define the following function f(x) = {(x*cos(x), when x<0), (x^2 + 3*x, when 0<=x<=1), (ln(x) + 4, when x>1)}  Set the range of x values from -3 to 3 and the range of y values from -2 to 5. Show both the axes on the plot. Set title "Continuous function without derivative at all points". Plot the function, store it in a file and print it.

Solution:

gnuplot graph
Q21-Solution

Q22.Define three functions in the given range: f1(x) = ln(1/x), when x > 0.1, f2(x) = 3*x^2*sin(x), when -1<x<0.1, f3(x) = cos(3x), when x<-1  Set the range of x values from -3 to 3 and the range of y values from -2.5 to 2.5. Show both the axes on the plot. Set y label "piecewise continuous function". Set ticks of the y axis at an interval of 0.5. Plot the three functions and store the plot in a file and print it.

Solution:

gnuplot graph
Q22-Solution

Q23.Show the parametric plot of the functions 2*cos^3(t), 2*sin^3(t), where t is the parameter. Set the range of t values from -pi to pi. The label of x and y axis will be "x = 2*cos^3(t)" and "y = 2*sin^3(t)" . Set title "Astriod". Draw both the axes on the plot. Store the plot in a file and print it.

Solution:

gnuplot graph
Q23-Solution

Q24.Show the polar plot of the functions e^(-0.1*t) where t is the angle made with the reference direction. (+ve x axis). Set samples to 10000. Set range from 0 to 6*pi. Show both the axes on the plot. The labels of x and y axis will be "x = r*cos(t)" and "y = r*sin(t)". Set title "Spiral". Store the plot in a file and print it.

Solution:

gnuplot graph
Q24-Solution

Q25.
gnuplot graph
Q25

Solution:

gnuplot graph
Q25-Solution

Q26.
gnuplot graph
Q26

Solution:

gnuplot graph
Q26-Solution

Q27.
gnuplot graph
Q27

Solution:

gnuplot graph
Q27-Solution

Q28.
gnuplot graph
Q28

Solution:

gnuplot graph
Q28-Solution

Q29.
gnuplot graph
Q29

Solution:

gnuplot graph
Q29-Solution

Q30.
gnuplot graph
Q30

Solution:

gnuplot graph
Q30-Solution

Basic knowledge About  Graph Plotting in Gnuplot

Plot Function: gnuplot>  plot sin(x)  or pl sin(x) or p sin(x)

View default options: gnuplot> test

Plot Multiple Functions: gnuplot> p  sin(x), cos(2*x), 2*tan(3*x)

Online Help Available: gnuplot> help or help plot (for specific function)

Mathematical functions:
absolute value of x, |x| -- abs(x)
cos inverse of x -- acos(x)
e^x --  exp(x)
logx -- log(x)
hyperbolic function -- tanh(x), asinh(x), cosh(2*x)

Plotting Styles:
With lines -- p sin(3*x) w l
with line width -- p sin(3*x) w l lw 4
with points -- p sin(3*x) w p
with lines+points -- p sin(3*x) w lp
with line color -- p sin(3*x) w l lc -1
with point colors -- p sin(3*x) w p pc 3
with point shapes -- p sin(3*x) w p pt 4
with dash lines -- p sin(3*x) w l dt 5
with point size -- p sin(3*x) w p ps 2

Set range: p [-2:2] [-5:5] 2*sin(6*x) w l lc 2 lw 2

set x range : set xrange [-100:200]

set y range: set yrange [-2:9]

unset xrange: to unset applied x range

Set title: set title 'Rajtech99 only Technology'

set x and y label: set xlabel 'x value' / set ylabel 'y value'

set tics is axis : set xtics -2, 0.5, 2 (for set x axis tics and here its intercal 0.5)

reset: reset (unset all applied option and set as a default options)

To Read data file:
gnuplot> cd 'path of the data file' (use manual option in the top of the gnuplot)
gnuplot> p 'file name.extension' w l

gnuplot> f(x) = m*x+c
gnuplot> fit f(x) 'file name.extension' via m, c (to fit data)
gnuplot> p f(x) w l, 'file name.extension' w p

To save the output file in jpeg format:
gnuplot> set terminal jpeg
gnuplot> set output "outputfile"

I hope this blog will help you to learn how  to work with your Gnuplot. For more information you can visit gnuplot official site. Thank you so much...
Raj Manna

My name is Raj. I am a student of 1st year physics honorus.

If you have any doubts, Please let me know

Post a Comment (0)
Previous Post Next Post