INDEX
S. No. Title Page Teacher’s
No. Signature
1. BISECTION METHOD. 1-2
2. NEWTON RAPHSON METHOD 3
3. GAUSS ELIMINATION METHOD 4-5
4. GAUSS SEIDEL METHOD 6-7
5. NEWTON’S FORWARD
DIFFERENCES METHOD 8-9
6. NEWTON’S BACKWARD 10-11
DIFFERENCES METHOD
7 NEWTON’S FORWARD 12-13
DIFFERENCES
INTERPOLATION POLYNOMIAL
METHOD.
8. TRAPEZOIDAL METHOD 14
9. SIMPSON’S 1/3 RULE 15
10. SIMPSON’S 3/8 RULE 16-17
11 EULER’S METHOD. 18
12 RUNGE KUTTA METHOD. 19
13 SECANT METHOD. 20-21
14 BIRGE VIETA METHOD. 22-23
15 JACOBI’S METHOD. 24-25
1
#1: Program in C To Solve Non-Linear Equation Using The
Bisection Method.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#define f(x) (x * x * x - x - 4)
int main() {
int i = 0;
float f1, f2, f3, x1, x2, x3, epsilon, delta;
printf("\nEnter two initial approximations x1,x2: ");
scanf("%f %f", &x1, &x2);
printf("\nEnter a very small number epsilon; ");
scanf("%f", &epsilon);
f1 = f(x1);
f2 = f(x2);
if (f1 * f2 > 0) {
printf("\nInitial approximations x1=%f, x2=%f are
not proper", x1, x2);
exit(0);
}
do {
x3 = (x1 + x2) / 2;
i++;
printf("\nNext approximation after %d iteration is
%f", i, x3);
f3 = f(x3);
if (f1 * f3 < 0) {
x2 = x3;
f2 = f3;
} else {
x1 = x3;
f1 = f3;
2
}
} while (
(fabs((x2 - x1) / x2) > epsilon) && (f3 != 0)
);
printf("\nRoot= %f", x3);
getch();
return 0;
}
3
#2: Program in C To Solve Non-linear Equation Using The
Newton Raphson Method
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x)(x * x * x - x - 4)
#define df(x)(3 * x * x - 1)
int main() {
int k, n;
float x1, x2, epsilon, delta, err;
printf("\nEnter the value of x1:");
scanf("%f", &x1);
printf("\nEnter the value of epsilon and delta:");
scanf("%f%f", &epsilon, &delta);
printf("\nEnter the number of iteration:");
scanf("%d", &n);
for (k = 1; k < n; k++) {
if (fabs(df(x1)) < delta) {
printf("\nSlope of curve is too small.");
exit(0);
}
x2 = x1 - f(x1) / df(x1);
err = fabs((x2 - x1) / x2);
if (err < epsilon) {
printf("Root=%f", x2);
exit(0);
}
x1 = x2;
}
printf("Solution does not converge after %d iterations", n);
getch();
return 0;
}
4
#3: Program In C to solve System of Linear Equation Using
Gauss Elimination Method.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#define max 50
int main() {
int i, n, j, k;
float a[max][max], x[max], u, sum;
printf("\nEnter the number of equation:");
scanf("%d", &n);
for (i = 0; i< n; i++) {
printf("\nEnter the coefficients of unknowns and RHS value of
equation %d \n", i + 1);
for (j = 0; j < n + 1; j++) {
scanf("%f", &a[i][j]);
}
for (k = 0; k < n - 1; k++) {
if (a[k][k] == 0) {
printf("\ndivision by zero");
exit(0);
}
for (i = k + 1; i< n; i++) {
u = a[i][k] / a[k][k];
for (j = 0; j < n + 1; j++) {
a[i][j] = a[i][j] - u * a[k][j];
}
}
for (i = n - 1; i>= 0; i--) {
sum = 0;
for (j = i + 1; j < n; j++) {
sum += a[i][j] * x[j];
x[i] = (a[i][n] - sum) / a[i][i];
}
5
printf("\nSolution of system linear equations is\n");
for (i = 0; i< n; i++)
printf("\n x[%d]=%.3f", i + 1, x[i]);
return 0;
}
6
#4: Program in C to solve Linear Equation Using Gauss
Seidel Method
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n, maxiter, i, j, k;
float a[10][11], x[10], sum, temp, error, e, bigerro
printf("\nEnter number of equation:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter the coefficients of unknowns and
value of equation
%d \n", i + 1);
for (j = 0; j < n + 1; j++)
scanf("%f", &a[i][j]);
}
printf("\nEnter the maximum number of iterations:");
scanf("%d", &maxiter);
printf("\nEnter a very small number epsilon:");
scanf("%f", &e);
for (i = 0; i < n; i++)
{
x[i] = 0;
for (k = 1; k <= maxiter; k++)
{
bigerror = 0;
for (i = 0; i < n; i++)
{
sum = 0;
for (j = 0; j < n; j++)
7
{
if (i != j)
sum += a[i][j] * x[j];
}
temp = (a[i][n] - sum) / a[i][i];
error = fabs((temp - x[i]) / temp);
if (error > bigerror)
{
bigerror = error;
}
x[i] = temp;
}
if (bigerror <= e)
{
printf("\nSolution converge after %d
iterations", k);
for (i = 0; i < n; i++)
{
printf("\nx[%d]=%f", i + 1, x[i]);
exit(0);
}
}
printf("\nSolution does not converge in %d
iteration", maxiter, bigerror);
return 0;
}
}
}
8
#5: Program in C to Generate Newton Forward
Differences Table.
#include <stdio.h>
#include <conio.h>
#include<math.h>
#define gotoxy(x, y) printf("%d;%dH", y, x)
int main() {
int i, j, n, m, l;
float d[20][20], x[20], y[20];
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Enter %d pairs of (x,y)\n ", n);
for (i = 0; i < n; i++) {
scanf("%f %f", &x[i], &y[i]);
}
for (j = 0; j < n - 1; j++) {
for (i = 0; i < n - j; i++) {
if (j == 0) {
d[i][j] = y[i + 1] - y[i];
}
else {
d[i][j] = d[i + 1][j - 1] - d[i][j - 1];
}
}
}
m = 14;
l = 6;
gotoxy(m, l);
printf("X");
l = l + 2;
gotoxy(m, l);
for (i = 0; i < n; i++) {
9
printf("%.2f", x[i]);
l = l + 2;
gotoxy(m, l);
}
m = m + 8;
l = 6;
gotoxy(m, l);
printf("Y");
l = l + 2;
gotoxy(m, l);
for (i = 0; i < n; i++) {
printf("%.2f", y[i]);
l = l + 2;
gotoxy(m, l);
}
for (j = 0; j < n - 1; j++) {
m = m + 8;
l = 6;
gotoxy(m, l);
printf("D%d", j + 1);
l = l + 2;
gotoxy(m, l);
for (i = 0; i < n - j - 1; i++) {
printf("%.2f", d[i][j]);
l = l + 2;
gotoxy(m, l);
}
}
getch();
return 0;
}
10
#6:Program in C to Generate Newton Backward
Differences Table.
#include <stdio.h>
#include <conio.h>
#define gotoxy(x, y) printf("%d %d", y, x)
void main()
{
int i, j, n, m, l, o = 4;
float d[20][20], x[20], y[20];
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Enter %d pairs of (x,y)\n ", n);
for (i = 0; i < n; i++)
{
scanf("%f %f", &x[i], &y[i]);
for (j = 0; j < n - 1; j++)
{
for (i = j + 1; i < n; i++)
{
if (j == 0)
{
d[i][j] = y[i] - y[i - 1];
}
else
{
d[i][j] = d[i][j - 1] - d[i - 1][j - 1];
}
}
}
}
m = 6;
l = 6;
gotoxy(m, l);
11
printf("X");
l = l + 2;
gotoxy(m, l);
for (i = 0; i < n; i++)
{
printf("%f", x[i]);
l = l + 2;
gotoxy(m, l);
}
m = m + 8;
l = 6;
gotoxy(m, l);
printf("Y");
l = l + 2;
gotoxy(m, l);
for (i = 0; i < n; i++)
{
printf("%f", y[i]);
l = l + 2;
gotoxy(m, l);
for (j = 0; j < n - 1; j++)
{
m = m + 8;
l = 6;
gotoxy(m, l);
printf("%d", j);
l = l + o;
gotoxy(m, l);
for (i = j + 1; i < n; i++) {
printf("%f", d[i][j]);
l = l + 2;
gotoxy(m, l);
o = o + 2;
}
}
}
12
#7: Program in C to implement Newton forward
differences interpolation polynomial
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int i, j, n, m, l, k;
float a, d[20][20], x[20], y[20], u, prod, sum;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Enter %d pairs of (x,y)\n ", n);
for (i = 0; i < n; i++){
scanf("%f, %f", &x[i], &y[i]);
}
printf("Enter the value of X to interpolate the value of Y: ");
scanf("%f", &a);
if ((a < x[0]) || (a > x[n - 1]))
{
printf("\nThe value lies outside the tabulated range ");
exit(0);
}
i = 1;
while (a < x[i])
{
i++;
13
}
k = i - 1;
u = (a - x[k]) / (x[k + 1] - x[k]);
for (j = 0; j < n - 1; j++)
{
for (i = 0; i < n - j; i++)
{
if (j == 0) {
d[i][j] = y[i + 1] - y[i];
}
else {
d[i][j] = d[i + 1][j - 1] - d[i][j - 1];
}
}
}
sum = y[k];
for (i = 0; i < n - k; i++)
{
prod = 1.0;
for (j = 0; j <= i; j++) {
prod = prod * (u - j) / (j + 1);
}
sum += d[k][i] * prod;
}
printf("\n Interpolated value of Y=%f", sum);
getch();
return 0;
}
14
#8: Program in C to Implement Trapezoidal Rule
for a known function.
#include <stdio.h>
#include <stdlib.h>
float f(float x) {
return (1.0 / (1 + x * x * x * x));
}
int main() {
int n, i;
float a, b, h, sum, intg;
printf("\n Enter values of a and b: ");
scanf("%f,%f", &a, &b);
printf("Enter Number of sub-intervals: ");
scanf("%d", &n);
if (a > b) {
printf("Invalid Input");
exit(0);
}
h = (b - a) / n;
sum = f(a) + f(b);
for (i = 1; i <= n - 1; i++) {
sum = sum + 2 * f(a + (i * h));
}
intg = (h / 2) * sum;
printf("Integral : %.4f", intg);
return 0;
}
15
#9: Program in C to Implement Simpsons 1/3 for a
known function.
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
float f(float x) {
return 1 / (1 + x * x);
}
void main() {
int i, n;
float a, b, h, sum, intg;
printf("\nEnter values of a and b: ");
scanf("%f %f", &a, &b);
printf("\nEnter no of sub-intervals :");
scanf("%d", &n);
if (a > b) {
printf("\nInvalid Input....");
getch();
exit(0);
}
h = (b - a) / n;
sum = f(a) + f(b);
for (i = 1; i <= n - 1; i++) {
if (i % 2 == 0)
sum += 2 * f(a + i * h);
else
sum += 4 * f(a + i * h);
}
intg = sum * (h / 3);
printf("\n INTEGRAL : %f", intg);
getch();
}
16
#10: Program in C to Implement Simpsons 3/8 for a
known function.
#include <stdio.h>
#include <stdlib.h>
#define Max 50
float f(float x) {
return 1 / (1 + x * x);
}
int main() {
int i, n;
float a, b, h, x[Max], y[Max], sum, intg;
printf("\nEnter values of a and b: ");
scanf("%f, %f", &a, &b);
printf("\nEnter no of sub-intervals :");
scanf("%d", &n);
if (a > b) {
printf("\nInvalid Input....");
exit(0);
}
h = (b - a) / n;
for (i = 0; i <= n; i++) {
x[i] = a + i * h;
y[i] = f(x[i]);
}
sum = y[0] + y[n];
for (i = 1; i <= n - 1; i++) {
if (i % 3 != 0) {
sum += 3 * y[i];
}
else {
17
sum += 2 * y[i];
}
}
intg = 3.0 * sum * (h / 8.0);
printf("integral : %f", intg);
getch();
return 0;
}
18
#11: Program in C To Solve ordinary Differential
Equation using Euler's Method.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float fn(float x, float y) {
return 3 * x + y;
}
int main() {
int i;
float x1, y1, h, xf, y, x;
printf("\nEnter starting point of the solution curve
(x, y)");
scanf("%f, %f", &x1, &y1);
printf("\nEnter the value of x for last solution
curve:");
scanf("%f", &xf);
printf("\nEnter step size h:");
scanf("%f", &h);
x = x1;
y = y1;
i = 1;
printf("\n i\t x \t y");
printf("\n %d\t %3f\t %f\t", i, x, y);
while (x < xf)
{
y = y + h * fn(x, y);
x = x + h;
i++;
printf("\n %d\t %f\t %f\t", i, x, y);
}
return 0;
}
19
#12: Program in C To Solve ordinary Differential Equation
using Runge Kutta Method for Second Order.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float fn(float x, float y) {
return x * x + y;
}
int main() {
int n, i;
float s1, s2, s3, s4, s, x1, x, y1, h, xf, y, f;
printf("\nEnter starting point of the solution curve
(x, y)");
scanf("%f, %f", &x1, &y1);
printf("\nEnter the value of x for last solution
curve:");
scanf("%f", &xf);
printf("\nEnter step size h:");
scanf("%f", &h);
x = x1;
y = y1;
i = 1;
printf("\ni\t x\t y");
printf("\n %d\t %f\t %f", i, x, y);
while (x <= xf)
{
s1 = fn(x, y);
x = x + h;
s2 = fn(x, y + h * s1);
y = y + h * (s1 + s2) / 2.0;
i = i + 1;
printf("\n %d\t %3f\t %3f", i, x, y);
}
return 0;
}
20
#13: Program in C to find root of equation using Secant
Method
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define f(x) x*x*x - 2*x - 5
void main() {
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
printf("\nEnter initital guesses: \n");
scanf("%f %f", &x0, &x1);
printf("Enter tolerance value: \n");
scanf("%f", &e);
printf("Enter max iterations value: \n");
scanf("%d", &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,
x0 = x1;
x1 = x2;
f0 = f1;
f1 = f2;
step = step + 1;
if(step > N) {
21
printf("Not Convergent");
exit(0);
}
}while(fabs(f2) > e);
printf("\nRoot is %f", x2);
getch();
}
22
#14: Program in C to find root of equation using
Birge-Vieta Method
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main() {
float p[20], a[20], b[20], c[20], epsilon;
int n, maxiter;
printf("Enter degree of polynomial:\n");;
scanf("%d", &n);
printf("Enter initial approximation:\n");
scanf("%f", &p[0]);
printf("enter prescribes tolerance:\n");
scanf("%f", &epsilon);
printf("Enter max number of iterations:\n");
scanf("%d", &maxiter);
for(int i = 0; i<=n; i++){
scanf("%f", &a[i]);
}
for(int k=1; k<=maxiter; k++) {
b[0]=a[0];
for(int i = 1; i<=n; i++) {
b[i]=a[i]+p[k]*b[i-1];
}
c[0]=b[0];
for(int i=1; i<=(n-1); i++) {
c[i]=b[i] + p[k] * c[i-1];
}
p[k+1] = p[k] - b[n] / c[n-1];
if(fabs((p[k-1] - p[k]) / p[k+1]) <= epsilon) {
printf("Root is %f", p[k+1]);
getch();
exit(0);
}
23
}
printf("Root not found");
getch();
}
24
#15: Program in C to find root of system of equations
using Jacobi’s Method
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main() {
int i,j,k,n,maxiter;
float a[10][10], x[10], y[10], maxerr, s, e;
printf("Enter number of equations:\n");
scanf("%d", &n);
printf("Enter number of iterations:\n");
scanf("%d", &maxiter);
printf("Enter the prescribed tolerance:\n");
scanf("%f", &e);
for(i = 1; i<=n; i++) {
for(j = 1; j<=(n+1); j++) {
scanf("%f", &a[i][j]);
}
}
for(i=1; i<=n; i++) {
y[i] = 0;
}
for(k=1; k <= maxiter; k++) {
maxerr = 0;
for(i=1; i<=n; i++) {
s=0;
for(j=1; j<=n;j++) {
int err;
if(i!=j) {
s=s+a[i][j]*y[j];
}
x[i]=(a[i][n+1]-s)/a[i][j];
25
err = fabs(x[i] = y[i]);
if (err>maxerr) {
maxerr = err;
}
}
}
if(maxerr <= e) {
printf("Solutionconverges in %d iterations", k);
for (i=1; i <=n; i++) {
printf("%f\n", x[i]);
}
exit(0);
}
}
printf("Solution does not converge in %d iterations", maxiter);
getch();
return;
}