0% found this document useful (0 votes)
14 views

CPP Program

The document contains the details of a student like name, course, roll number etc. and then contains the code and output for 9 different numerical integration techniques like Trapezoidal rule, Simpson's 1/3 rule, Simpson's 1/8 rule etc.

Uploaded by

Elite Gammer 03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CPP Program

The document contains the details of a student like name, course, roll number etc. and then contains the code and output for 9 different numerical integration techniques like Trapezoidal rule, Simpson's 1/3 rule, Simpson's 1/8 rule etc.

Uploaded by

Elite Gammer 03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Name :- Pankaj Bisht Subject Name:- CBNST Lab

Course:- B.C.A Subject Code:- PBC-402


Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

Objective 1: C program to find absolute error, relative error and percentage error.

#include <math.h>
int main() { float
aV, tV;
float aE, rE, pE;

printf("Enter the measured value: ");


scanf("%f", &aV);

printf("Enter the true value: ");


scanf("%f", &tV);

aE = fabs(tV - aV); rE
= (aE / tV) * 100; pE
= rE;

printf("Absolute Error: %f\n", aE);


printf("Relative Error: %f\n", rE);
printf("Percentage Error: %f%%\n", pE);
return 0; }

Output:-

1
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

Objective 2: C program to find the real root of the given equation using Bisection method
#include<stdio.h>
#include<math.h> #define F(x) x*x*x-2*x-5 int main(){ int
i=1; float a,b,c,f; printf("Enter the value of interval a and b:");
scanf("%f %f",&a,&b);
do{ c=(a+b)/2; f=F(c);
printf("\n i=%d a=%f b=%f c=%f F(c)=%f",i,a,b,c,f);
if(F(c)<0){ a=c; } else{b=c;} i++;
}
while (fabs(F(c))>0.001);
printf("\n \n approximate root = %.4f\n",c);
return 0;}

OUTPUTT:-

2
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

Objective 3: C program to find the real root of the given equation using Regula Falsi
method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*log10(x) - 1.2 int
main() { float x0, x1, x2, f0, f1, f2, e;
int step = 1; printf("\nEnter two initial
guesses:\n"); scanf("%f%f", &x0,
&x1); printf("Enter tolerable error:\n");
scanf("%f", &e); f0 = f(x0); f1 = f(x1);
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n"); goto up; }
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do{ x2 =
x0 - (x0-x1) * f0/(f0-f1); f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
if(f0*f2 < 0)
{
x1 = x2;
f1 = f2; } else{
x0 = x2; f0 =
f2;} step = step
+ 1;
} while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
return 0;
}

Output:-

3
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

4
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

Objective 4: C program to find the real root of the given equation using Secant method.
#include<stdio.h>
#include<math.h>
#define f(x) x*x*x - 2*x – 5
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
printf("\nEnter initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Entermaximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{ f0 = f(x0); f1 = f(x1); if(f0 == f1){
printf("Mathematical Error.");
exit(0);
} x2 = x1 - (x1 - x0) * f1/(f1-f0);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2, f2);
x0 = x1; f0 = f1; x1 = x2; f1 = f2;
step = step + 1;
if(step > N)
{ printf("Not Convergent.");
exit(0);
}

5
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

} while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
return 0; }
OUTPUTT:-

6
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

Objective 5: C program to find the real root of the given equation using Newton’s Raphson
method
#include<stdio.h>
#include<math.h>
#define f(x) 3*x - cos(x) -1
#define g(x) 3 + sin(x)
int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Entermaximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{ g0 = g(x0);
f0 = f(x0);
if(g0== 0.0)
{ printf("Mathematical Error.");
exit(0);
} x1 = x0 - f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
x0 = x1; step = step+1;
if(step > N)
{
printf("Not Convergent.");
exit(0); } f1 = f(x1);

7
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

}while(fabs(f1)>e);
printf("\nRoot is: %f", x1);
return 0; }
Output:-

8
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023

Objective 6: C program to find the real root of the given equation using Iterative method
#include<stdio.h>
#include<math.h>
#define f(x) cos(x)-3*x+1
#define g(x) (1+cos(x))/3
int main()
{ printf("Enter initial guess: ");
scanf("%f", &x0);
printf("Enter tolerable error: ");
scanf("%f", &e);
printf("Enter maximum iteration: ");
scanf("%d",&N);
printf("\nStep\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do{ x1 = g(x0);
printf("%d\t%f\t%f\t%f\t%f\n",step, x0, f(x0), x1, f(x1));
step = step + 1;
if(step>N){ printf("Not Convergent.");
exit(0);
} x0 = x1;
}while( fabs(f(x1)) > e);
printf("\nRoot is %f", x1);
return 0;}
Output:-

9
Name :- Priyanshu Saklani Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 40 Section:- E2 (4th -sem)
Student ID :-220411069

Objective 7: C program for Trapezoidal rule


#include<stdio.h>
#include<math.h>
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize,
k;
int i, subInterval;
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
stepSize = (upper - lower)/subInterval;
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{ k = lower + i*stepSize;
integration = integration + 2 * f(k);
}
integration =integration*stepSize/2;
printf("\nRequired value of integration is: %.3f", integration);
return 0;
}
OUTPUT:-

10
Name :- Priyanshu Saklani Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 40 Section:- E2 (4th -sem)
Student ID :-22041106

Objective 8: C program for Simpson 1/3 rule


#include<stdio.h>
#include<math.h>
#define f(x) 1/(1+x*x)
int main()
{ float lower, upper, integration=0.0, stepSize,
k;
int i, subInterval;
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval); stepSize = (upper-
lower)/subInterval;
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{ k = lower + i*stepSize;
if(i%2==0){ integration =
integration+2*f(k);} else{
integration = integration + 4 * f(k);
}
}integration = integration * stepSize/3;
printf("\nRequired value of integration is: %.3f",integration);
return 0;}
OUTPUT:-

11
Name :- Priyanshu Saklani Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 40 Section:- E2 (4th -sem)
Student ID :-220411069

Objective 9: C program for Simpson 1/8 rule


#include<stdio.h>
#include<math.h>
#define f(x) 1/(1+x*x)
int main(){
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval); stepSize =
(upper - lower)/subInterval; integration
= f(lower) + f(upper); for(i=1; i<=
subInterval-1; i++){ k = lower +
i*stepSize; if(i%3 == 0){
integration = integration + 2 * f(k);
}
else{
integration = integration + 3 * f(k); }}
integration = integration * stepSize*3/8;
printf("\nRequired value of integration is: %.3f", integration);
return 0;}
OUTPUT:-

12

You might also like