Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

NUMERICAL METHODS

AND COMPUTER
PROGRAMMING
111610145 SHAHBAZ ANIS SHOLAPURE
111610141 MAYANK PATIL
111610142 BUDDHAGOSH RAGADE
111610148 ROHAN THORAT
T.Y BTECH MECHANICAL DIVISION 2
BATCH G
Page |1

TUTORIAL 4

CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
// Use of file function to create a file by name ANS.TXT
FILE *fp;
fp = fopen("tp.txt","w");
//For loop to print the table
double arr1[8] = {0.5,2,3,4,6,8,10,11};
double arr2[8] =
{336,294.4,266.4,260.8,260.5,249.6,193.6,165.6};
Page |2

double i1, i2, i3, i4, sum;


int i;
fprintf(fp,"\nPressure(kPa) Volume(m^3)\n");
for(i=0;i<8;i++)
{
fprintf(fp,"%lf %lf\n",arr2[i],arr1[i]);
}
// Use of trapezoidal rule for first 2 points
i1 = (arr1[1] - arr1[0]) / 2.0 * (arr2[0] + arr2[1]);
fprintf(fp,"\nArea under the curve between 1st and 2nd
point = %lf kJ.\n",i1);
// Use of simpson's 1/3 rule for next 3 points
// Here simpson's 1/3 rule is used as the difference in the x
values for next three points is equal to 1
i2 = (arr1[3] - arr1[1]) / 6 * (arr2[1] + (4 * arr2[2]) + arr2[3]);
fprintf(fp,"Area under the curve between 2nd and 4th point
= %lf kJ.\n",i2);
// Use of simpson's 3/8 rule for next 4 points
// Here simpson's 3/8 rule is used as the difference in the x
values for next three points is equal to 2
i3 = (arr1[6] - arr1[3]) / 8 * (arr2[3] + 3 * (arr2[4] + arr2[5]) +
arr2[6]);
fprintf(fp,"Area under the curve between 4th and 7th point
= %lf kJ.\n",i3);
Page |3

// Agin using trapezoidal rule for last 2 points


i4 = (arr1[7] - arr1[6]) / 2.0 * (arr2[6] + arr2[7]);
fprintf(fp,"Area under the curve between 7th and 8th point
= %lf kJ.\n",i4);
sum = i1+i2+i3+i4;
fprintf(fp,"\nThe total work done by the gas is equal to %lf
kJ\n",sum);
fclose(fp);
}
Page |4

OUTPUT:
Pressure(kPa) Volume(m^3)
336.000000 0.500000
294.400000 2.000000
266.400000 3.000000
260.800000 4.000000
260.500000 6.000000
249.600000 8.000000
193.600000 10.000000
165.600000 11.000000

Area under the curve between 1st and 2nd point = 472.800000 kJ.
Area under the curve between 2nd and 4th point = 540.266667 kJ.
Area under the curve between 4th and 7th point = 1488.525000
kJ.
Area under the curve between 7th and 8th point = 179.600000 kJ.
The total work done by the gas is equal to 2681.191667 kJ
Page |5

CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) (x*pow(exp(1),(2*x))) //given function
#define av 504.5359 //analytical value
#define h(x) ((2.25+2.25*x)*(pow(exp(1),(3+3*x)))) //varaible changed
according to limits
int main()
{
float I,c0,c1,c2,c3,x1,x2,x3,x0,err;
c0=1,c1=1,x0=-0.577350269,x1=0.577350269;
I=c0*h(x0)+c1*h(x1);
printf("\nValue of integral using two point Gauss Legendre formula
is:%f",I);
err=((av-I)/av)*100;
Page |6

printf("\nValue of error using two point Gauss Legendre formula is:%f


%%\n",err);
c0=0.5555556,c1=0.8888889,c2=0.5555556,x0=-
0.774596669,x1=0,x2=0.774596669;
I=c0*h(x0)+c1*h(x1)+c2*h(x2);
printf("\nValue of integral using three point Gauss Legendre formula
is:%f",I);
err=((av-I)/av)*100;
printf("\nValue of error using three point Gauss Legendre formula is:%f
%%\n",err);
c0=0.3478548,c1=0.6521452,c2=0.6521452,c3=0.3478548,x0=-
0.861136312;
x1=-0.339981044,x2=0.339981044,x3=0.861136312;
I=c0*h(x0)+c1*h(x1)+c2*h(x2)+c3*h(x3);
printf("\nValue of integral using four point Gauss Legendre formula
is:%f",I);
err=((av-I)/av)*100;
printf("\nValue of error using four point Gauss Legendre formula is:%f
%%\n",err);
return 0;
}
Page |7

OUTPUT:
Value of integral using two point Gauss Legendre formula is:406.295044
Value of error using two point Gauss Legendre formula is:19.471529 %

Value of integral using three point Gauss Legendre formula


is:495.820435
Value of error using three point Gauss Legendre formula is:1.727422 %

Value of integral using four point Gauss Legendre formula is:504.130432


Value of error using four point Gauss Legendre formula is:0.080365 %
Page |8

CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j,x,h,t;
float vel1, vel2, vel3, acc1, acc2, acc3;
float arr[2][9]={0,2,4,6,8,10,12,14,16,0,0.7,1.8,3.4,5.1,6.3,7.3,8.0,8.4};
printf("Enter the time in sec \n");
scanf("%d",&t); //Taking the time from the user//
for(i=0;i<8;i++)
{
if(t==arr[0][i])
{
x=i;
exit;
} //Finding the position of time in the array//
}
printf("The position of the time in the array is %d \n", x);
h=(arr[0][2]-arr[0][1]); //Finding the value of h//
printf("The value of h is %d \n", h);
Page |9

vel1= ((-arr[1][x+2] + (8*arr[1][x+1]) - (8*arr[1][x-1]) + arr[1][x-


2])/(12*h)); //Finding the velocity by centered finite difference//
acc1= ((-arr[1][x+2] + (16*arr[1][x+1]) - (30*arr[1][x]) + (16*arr[1][x-1]) -
arr[1][x-2])/(12*h*h)); //Finding the accleration by centered finite
difference//
printf("The velocity at t=10sec by centered finite difference is %f \n",
vel1);
printf("The acceleration at t=10sec by centered finite difference is %f
\n", acc1);
vel2= ((-arr[1][x+2] + (4*arr[1][x+1]) - (3*arr[1][x]))/(2*h));
//Finding the velocity by forward finite difference//
acc2= ((-arr[1][x+3] + (4*arr[1][x+2]) - (5*arr[1][x+1]) +
(2*arr[1][x]))/(h*h)); //Finding the acceleration by forward finite
difference//
printf("The velocity at t=10sec by forward finite difference is %f \n",
vel2);
printf("The acceleration at t=10sec by forward finite difference is %f
\n", acc2);
vel3= (((3*arr[1][x]) - (4*arr[1][x-1]) + arr[1][x-2])/(2*h));
//Finding the velocity by backward finite difference//
acc3= (((2*arr[1][x]) - (5*arr[1][x-1]) + (4*arr[1][x-2]) - arr[1][x-
3])/(h*h));
//Finding the acceleration by backward finite difference//
printf("The velocity at t=10sec by backward finite difference is %f \n",
vel3);
printf("The acceleration at t=10sec by backward finite difference is %f
\n", acc3);
return 0;
}
P a g e | 10

OUTPUT:
Enter the time in sec
10
The position of the time in the array is 5
The value of h is 2
The velocity at t=10sec by centered finite difference is 0.541667
The acceleration at t=10sec by centered finite difference is -0.041667
The velocity at t=10sec by forward finite difference is 0.575000
The acceleration at t=10sec by forward finite difference is -0.075000
The velocity at t=10sec by backward finite difference is 0.475001
The acceleration at t=10sec by backward finite difference is -0.275000

You might also like