You are on page 1of 3

Computational Physics Lab Test 2011

Prahalad Kanti Barman. 11PH40018 06.02. 2012

Assignment 2 : Problem 1:
Given the three data points (x,y)= (1.0,8.0), (2.1,20.6) and (5.0,13.7), write a program to return the value of y for any arbitrary x in the range [1.0,5.0]. The program should exit if x is outside this range.

A ) Program :
#include<stdio.h> #include<math.h> main() { float x[]={1.0,2.1,5.0},y[]={8.0,20.6,13.7}; float y1,y2,c1,c2,p,q; y1=(y[1]-y[0])/(x[1]-x[0]); c1=-y1*x[0]+y[0]; y2=(y[2]-y[1])/(x[2]-x[1]); c2=-y2*x[1]+y[1]; printf("\nEnter the value of x with in range [1.0,5.0] = "); scanf("%f",&p); if(p>=1.0 && p<=2.1) q=y1*p+c1; else if(p>2.1 && p<=5.0) q=y2*p+c2; else printf("\nentered value is out of range\n"); if(p>=1 && p<=5) printf("\nThe interpolated value of y = %f\n",q); }

B ) Inputs and Outputs from Linux Terminal :


subha@subha-Inspiron-N5010:~$ ./inter Enter the value of x with in range [1.0,5.0] = 3.7 The interpolated value of y = 16.793104 subha@subha-Inspiron-N5010:~$ ./inter Enter the value of x with in range [1.0,5.0] = 6.3 entered value is out of range subha@subha-Inspiron-N5010:~$ ./inter

Enter the value of x with in range [1.0,5.0] = 1.6 The interpolated value of y = 14.872728

C ) Results :
Here we get the liner interpolated values of Y for a given X in the range.

D ) Discussions and Conclusions :


For the two points we calculated the slope and intercept .Then we plugged them back into the straight line equation and calculated the value of y for a given x.

Linear Interpolation for data points from a DAT file Problem 2 :


Generalize the above program to input data points from a data file points.dat (a sample program to read data from a data file is given here).

A)Program :
Code
#include<stdio.h> #include<math.h> main() { float x[100],y[100],m[100],c[100]; int N,test,i; float p,q; FILE* fp; test=1; N=0; fp=fopen("points.dat","r"); while(test>0) { test=fscanf(fp,"%f%f",&x[N],&y[N]); ++N; } N=N-1; printf("No. of point =%d\n",N); for(i=0;i<N;++i) { printf("%f %f\n",x[i],y[i]); }

for(i=0;i<N;i++) { m[i+1]=(y[i+1]-y[i])/(x[i+1]-x[i]); c[i+1]=-m[i+1]*x[i+1]+y[i+1]; } printf("\nenter the value of x ="); scanf("%f",&p); for(i=0;i<N;i++) { if(p>=x[i] && p<=x[i+1]) q=m[i+1]*p+c[i+1]; } if(p>=x[0] && p<=N-1) printf("\n interpolated value for y = %f",q); else printf("\n entered value is out of range"); }

B)Input and output from Linux Terminal :


enter the value of x =2.35 interpolated value for y = 2.210000 Enter the value of x = 67 entered value is out of range

C)Result :
Here we get the liner interpolated values of Y for a given X in the range.Here we take the inputs from a data file and calculated the interpolated values for user given values of x.

D)Discussion :
For the two points we calculated the slope and intercept .Then we plugged them back into the straight line equation and calculated the value of y for a given x.All the xs,ys,ms and cs have been put in arrays for convinience.Later they have been use element wise.
OS : Ubuntu 11.10 Text Editor : Emacs 23

You might also like