0% found this document useful (0 votes)
19 views23 pages

Ankit (CBNST)

cbnst

Uploaded by

arnavhardwar2004
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)
19 views23 pages

Ankit (CBNST)

cbnst

Uploaded by

arnavhardwar2004
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/ 23

Q1-write a program to find the root by using Bisection method.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define f(x) cos(x) - x * exp(x)

void main()

float x0, x1, x2, f0, f1, f2, e;

int step = 1;

up:

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;

/* Implementing Bisection Method */

printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");

do

x2 = (x0 + x1)/2;

Name-Aman Singh Dhek Section-F2 Roll no-10


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);

getch();

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:

Name-Aman Singh Dhek Section-F2 Roll no-10


Q2--write a program to find the root by using fixed point interation
method.

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

#define f(x) (pow(x, 3) - x - 1)

#define g(x) (cbrt(x + 1))

int main()

int count = 1, N;

float x0, x1, e;

printf("Enter initial guess: ");

scanf("%f", &x0);

printf("Enter tolerable error: ");

scanf("%f", &e);

printf("Enter maximum iterations: ");

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", count, x0, f(x0), x1, f(x1));

Name-Aman Singh Dhek Section-F2 Roll no-10


count = count + 1;

if (count > N)

printf("Not Convergent.\n");

exit(0);

x0 = x1;

} while (fabs(f(x1)) > e);

printf("\nRoot is %f\n", x1);

return 0;

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:

Name-Aman Singh Dhek Section-F2 Roll no-10


Q3--write a program to find the root by 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;

up:

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;

/* Implementing Regula Falsi or False Position Method */

printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");

do

Name-Aman Singh Dhek Section-F2 Roll no-10


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);

getch();

return 0;

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:

Name-Aman Singh Dhek Section-F2 Roll no-10


Q4--write a program to find the root by using NEWTON RAMPSON
method.
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

#define f(x) 3*x - cos(x) -1

#define g(x) 3 + sin(x)

void 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("Enter maximum 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);

Name-Aman Singh Dhek Section-F2 Roll no-10


}

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);

}while(fabs(f1)>e);

printf("\nRoot is: %f", x1);

getch();

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:(3x-cos(x)-1

Name-Aman Singh Dhek Section-F2 Roll no-10


Output: x-exp(x)

Name-Aman Singh Dhek Section-F2 Roll no-10


Output: x*x-30

Name-Aman Singh Dhek Section-F2 Roll no-10


Q5---write a program to find the root by using secant method.
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.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 initial guesses:\n");

scanf("%f%f", &x0, &x1);

printf("Enter tolerable error:\n");

scanf("%f", &e);

printf("Enter maximum iteration:\n");

scanf("%d", &N);

/* Implementing Secant Method */

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);

Name-Aman Singh Dhek Section-F2 Roll no-10


}

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);

}while(fabs(f2)>e);

printf("\nRoot is: %f", x2);

getch();

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:

Name-Aman Singh Dhek Section-F2 Roll no-10


Q6---write a program to find the root by gauss elimination method.
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

#define SIZE 10

int main()

float a[SIZE][SIZE], x[SIZE], ratio;

int i,j,k,n;

printf("Enter number of unknowns: ");

scanf("%d", &n);

for(i=1;i<=n;i++)

for(j=1;j<=n+1;j++)

printf("a[%d][%d] = ",i,j);

scanf("%f", &a[i][j]);

for(i=1;i<=n-1;i++)

if(a[i][i] == 0.0)

printf("Mathematical Error!");

exit(0);

Name-Aman Singh Dhek Section-F2 Roll no-10


}

for(j=i+1;j<=n;j++)

ratio = a[j][i]/a[i][i];

for(k=1;k<=n+1;k++)

a[j][k] = a[j][k] - ratio*a[i][k];

x[n] = a[n][n+1]/a[n][n];

for(i=n-1;i>=1;i--)

x[i] = a[i][n+1];

for(j=i+1;j<=n;j++)

x[i] = x[i] - a[i][j]*x[j];

x[i] = x[i]/a[i][i];

/* Displaying Solution */

printf("\nSolution:\n");

for(i=1;i<=n;i++)

printf("x[%d] = %0.3f\n",i, x[i]);

return(0);

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:

Name-Aman Singh Dhek Section-F2 Roll no-10


Q7---write a program to find the root by gauss jordan method.

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define SIZE 10

int main()

float a[SIZE][SIZE], x[SIZE], ratio;

int i,j,k,n;

clrscr();

printf("Enter number of unknowns: ");

scanf("%d", &n);

/* 2. Reading Augmented Matrix */

printf("Enter coefficients of Augmented Matrix:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n+1;j++)

printf("a[%d][%d] = ",i,j);

scanf("%f", &a[i][j]);

/* Applying Gauss Jordan Elimination */

for(i=1;i<=n;i++)

Name-Aman Singh Dhek Section-F2 Roll no-10


{

if(a[i][i] == 0.0)

printf("Mathematical Error!");

exit(0);

for(j=1;j<=n;j++)

if(i!=j)

ratio = a[j][i]/a[i][i];

for(k=1;k<=n+1;k++)

a[j][k] = a[j][k] - ratio*a[i][k];

/* Obtaining Solution */

for(i=1;i<=n;i++)

x[i] = a[i][n+1]/a[i][i];

/* Displaying Solution */

printf("\nSolution:\n");

for(i=1;i<=n;i++)

printf("x[%d] = %0.3f\n",i, x[i];

Name-Aman Singh Dhek Section-F2 Roll no-10


Output:

Name-Aman Singh Dhek Section-F2 Roll no-10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy