Professional Documents
Culture Documents
Lab Manual Computer Science For FDP in Physics: #Include #Include
Lab Manual Computer Science For FDP in Physics: #Include #Include
1 Introduction
This manual discusses numerical experiments for the computer science lab pre-
scribed for students of the FDP in Physics of University of Kerala. The C
programs follow ANSI C and can be compiled by any C compiler that conform
to ANSI C.
However, examples of compilation and execution steps provided here assume
that the reader is using a Linux machine that has GNU C compiler (gcc) in-
stalled. For other operating system/ compilers please check the manuals they
provide.
To plot graphs of functions, we use the program Gnuplot. You can get
started here Start Learninng Gnuplot.
File: quadratic.c
#include <stdio.h>
#include <math.h>
void main(){
float a,b,c,d,r1,r2;
printf("\nEnter the coefficients a b c : ");
scanf("%f%f%f" ,&a,&b,&c);
if (a==0) {
printf("\nNot a quadratic equation");
}
d=b*b-4*a*c;
r1=-b/(2*a);
if (d==0){
printf("\nThe roots are real and equal");
printf("\nThe root =%f \n",r1);
}
else if(d>0){
r2=sqrt(d)/(2*a);
printf("\nThe roots are real and distinct");
printf("\nFirst root=%f \nSecond root=%f \n",r1+r2,r1-r2);
}else {
r2= sqrt(fabs(d))/(2*a);
1
printf("\nThe roots are imaginary");
printf("\nFirst root =%f +i%f", r1,r2);
printf("\nSecond root=%f -i%f \n", r1,r2);
}
}
3 Rank List
File: ranklist.c
#include <stdio.h>
#include <string.h>
#define NMAX 20
#define NAMELENGTH 10
struct studentType{
char name[NAMELENGTH];
int marks;
};
void main(){
int n,i,j,marks;
char name[NAMELENGTH];
struct studentType students[NMAX];
printf("\nEnter number of students (< %d): ",NMAX);
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter name and marks of sudent#%d: ",i+1);
scanf("%s %d",students[i].name,&students[i].marks);
}
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++){
if(students[j].marks < students[j+1].marks){
strcpy(name,students[j].name);
strcpy(students[j].name,students[j+1].name);
strcpy(students[j+1].name,name);
marks=students[j].marks;
2
students[j].marks=students[j+1].marks;
students[j+1].marks=marks;
}
}
printf("\nRank List");
printf("\n%7s %-11s %5s","Rank","Name","Marks");
for(i=0;i<n;i++){
printf("\n%7d %-11s %5d",i+1,students[i].name,students[i].marks);
}
printf("\n");
}
Rank List
Rank Name Marks
1 epsilon 96
2 beta 89
3 gamma 78
4 alpha 34
#include<stdio.h>
#include<math.h>
#define PI 3.141593
void main() {
float x,xr;
printf("\n enter the value of x in degree :");
scanf("%f" ,&x );
3
xr= x*PI/180;
printf("\n sine(%f) =%f \t tan(%f) =%f", x,sin(xr),x,tan(xr));
printf("\n e to the power of -%f is %f",x,exp(-x));
}
5 Ploting Functions
To plot a function f (x), we will create a text file with x-values in the first
column and the corresponding f (x) in the second column. In the examples
we discuss in this section, we assume the filename to be ’data.dat’. There are
many softwares available to produce graphs from the data files. Here, we use
the program Gnuplot.
5.1 Gnuplot
This program can be invoked from the terminal using the command gnuplot.
This will start the program with the prompt gnuplot>. The terminal on gnuplot
start-up looks like the following
$ gnuplot
G N U P L O T
Version 4.6 patchlevel 6 last modified September 2014
Build System: Linux x86_64
Example
4
• Set Y-axis label to ‘f(x)’
• Plot the ‘data.dat’ with x-data from colum 1 and f (x) data from
column 2. Set the legend of the plot-curve to be ‘function f(x)’
The following set of gnuplot commands achieve this.
gnuplot> exit
File: plotsin.c
#include<stdio.h>
#include<math.h>
#define PI 3.141593
void main()
{
FILE *fp;
float xstart,xend,xstep,xr,x;
printf("\n enter initial and final values of x and step in degrees :
");
scanf("%f %f %f",&xstart, &xend, &xstep);
fp=fopen("sin.dat","w");
fprintf(fp,"#x \t sin(x) ");
for(x=xstart;x<=xend;x+=xstep){
xr=x*PI/180;
fprintf(fp,"\n %f %f",x,sin(xr));
}
fclose(fp);
}
5
Compilation and execution of the program is done the following way.
The contents of the file can be viewed using a texteditor, for example gedit or
scratch.
$ gnuplot
G N U P L O T
Version 4.6 patchlevel 6 last modified September 2014
Build System: Linux x86_64
6
The graph can be saved into a PNG file named ‘sin.png’ using the following
set of commands
gnuplot> set terminal png
Terminal type set to ’png’
Options are ’nocrop font "/usr/share/fonts/truetype/liberation
/LiberationSans-Regular.ttf,12" fontscale 1.0 size 640,480 ’
gnuplot> set output ’sin.png’
gnuplot> replot
File: plottan.c
#include<stdio.h>
#include<math.h>
#define PI 3.141593
void main()
{
FILE *fp;
float xstart,xend,xstep,xr,x;
printf("\n enter initial and final values of x and step in degrees :
");
scanf("%f %f %f",&xstart, &xend, &xstep);
fp=fopen("tan.dat","w");
fprintf(fp,"#x \t tan(x) ");
for(x=xstart;x<=xend;x+=xstep){
xr=x*PI/180;
7
fprintf(fp,"\n %f %f",x,tan(xr));
}
fclose(fp);
}
This creates the file ‘tan.dat’ from which Gnuplot creates the graph
File: plotexp.c
#include<stdio.h>
#include<math.h>
8
void main()
{
FILE *fp;
float x,xstart,xend,xstep;
printf("\n enter initial and final values of x and step : ");
scanf("%f %f %f",&xstart, &xend, &xstep);
fp=fopen("exp.dat","w");
fprintf(fp,"#x \t exp(-x) ");
for(x=xstart;x<=xend;x+=xstep){
fprintf(fp,"\n %f %f",x,exp(-x));
}
fclose(fp);
}
This creates the file ‘exp.dat’ from which Gnuplot creates the graph
9
6 Product of two matrices
File: matrix.c
#include<stdio.h>
#include<stdlib.h>
#define ROWS 10
#define COLS 10
void main(){
int a[ROWS][COLS],b[ROWS][COLS],c[ROWS][COLS];
int m,n,p,q,i,j,k;
for(i=0;i<m;i++){
for(k=0;k<q;k++){
c[i][k]=0;
for(j=0;j<n;j++)
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
10
printmatrix(m,n,a);
First matrix
3 4
7 8
second matrix
10 2
1 7
#include <stdio.h>
void main(){
float a1,a2,a3,b1,b2,b3,c1,c2,c3,scalarproduct=0;
11
printf("\nEnter (3D)vector a:");
scanf("%f %f %f",&a1,&a2,&a3);
printf("\nEnter (3D)vector b:");
scanf("%f %f %f",&b1,&b2,&b3);
scalarproduct=a1*b1+a2*b2+a3*b3;
c1=a2*b3-a3*b2;
c2=a3*b1-a1*b3;
c3=a1*b2-a2*b1;
The compilation and execution in the terminal looks like the following
Scalrproduct= 8.000000
File: trapezoidal.c
#include <stdio.h>
12
void main(){
int n;
double a,b;
printf("\nEnter number of intervals( >2),initial and final points:
");
scanf("%d %lf %lf",&n,&a,&b);
if((n%2)) n++;
printf("\nTrapezoidal rule: n=%d, integral=%lf\n",n,trapz(fn,a,b,n));
}
File: simpson.c
#include <stdio.h>
#include <math.h>
void main(){
int n;
double a,b;
printf("\nEnter number of intervals(even, >2),initial and final
points: ");
scanf("%d %lf %lf",&n,&a,&b);
if((n%2)) n++;
printf("\nSimpson rule: n=%d, Integral=%lf\n",n,simpson(fn,a,b,n));
}
13
$ gcc simpson.c -o simpson -lm
$ ./simpson
File: rk2.c
#include <stdio.h>
void main(){
float y,t,yout,h,tstart,tend,k1,k2;
FILE *fp;
y=10;
h=0.1;
tstart=0;
tend=10;
fp=fopen("rk2.dat","w");
fprintf(fp,"\n%f %f",tstart,y);
for(t=tstart;t<tend;t+=h){
k1=f(y,t);
k2=f(y+h*k1,t+h);
yout=y+(k1+k2)*h/2;
y=yout;
fprintf(fp,"\n%f %f",t+h,y);
}
fclose(fp);
}
14
’solution of dy/dt=-y, y(0)=10’
15