CBNST
CBNST
Program:
#include <stdio.h>
#include <math.h>
int main() {
double x0, x1, e;
int step = 0;
printf("Enter the values of a and b (initial guesses): ");
scanf("%lf%lf", &x0, &x1);
printf("Enter the tolerance: ");
scanf("%lf", &e);
float f0 = f(x0);
float f1 = f(x1);
if(f0 * f1 >= 0.0)
{
printf("incorrect initial points\n");
return 1;
}
float x2, f2;
do
{
x2 = (x0 + x1) / 2;
f2 = f(x2);
if(f0 * f2 < 0)
{
x1 = x2;
}
else
{
x0 = x2;
}
step += 1;
} while(fabs(f2) > e);
printf("\nRoot found: %.6lf\n", x2);
printf("Iterations count: %d\n", step);
return 0;
}
Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
OUTPUT:
𝒇(𝒙) = 𝒙𝟑 − 𝟐𝟎
𝒇(𝒙) = 𝒙𝟑 − 𝟒𝒙 − 𝟗
𝒇(𝒙) = 𝒙𝟐 − 𝟑𝟎
MEHUL NAINWAL/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
Objective: 2. Write a program in c language to find the roots of given algebraic
equations using Regula Falsi method.
Program:
#include <stdio.h>
#include <math.h>
#define f(x) (x - exp(-x));
int main() {
double x0, x1, e;
int step = 0;
printf("Enter the values of a and b (initial guesses): ");
scanf("%lf%lf", &x0, &x1);
printf("Enter the tolerance: ");
scanf("%lf", &e);
float f0 = f(x0);
float f1 = f(x1);
if(f0 * f1 >= 0.0)
{
printf("incorrect initial points\n");
return 1;
}
float x2, f2;
do
{
x2 = (x0 + x1) / 2;
f2 = f(x2);
if(f0 * f2 < 0)
{
x1 = x2;
}
else
{
x0 = x2;
}
step += 1;
} while(fabs(f2) > e);
printf("Root found: %.6lf\n", x2);
printf("Iterations count: %d\n", step);
return 0;
}
Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
OUTPUT:
𝒇(𝒙) = 𝒙 – e-x
𝒇(𝒙) = 3𝒙 – cos(x) - 1
𝒇(𝒙) = 𝒙2 – 30
MEHUL NAINWAL/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
Objective: 5. Write a program in c language to find the roots of given algebraic
equations using Newton Raphson method.
Program:
#include <stdio.h>
#include <math.h>
int main()
{
double initial_guess, tol;
printf("Enter the initial guess for the root:\n");
scanf("%lf", &initial_guess);
printf("Enter the tolerance value:\n");
scanf("%lf", &tol);
newton_raphson(initial_guess, tol);
return 0;
}
Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
OUTPUT:
𝒇(x) = x2 - 30 g(x) = 2x
MEHUL NAINWAL/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
Objective:3.Write a C Program to find the roots of the Polynomial Equation
using Secant Method
Program
#include <stdio.h>
#include <math.h>
void secant(float x1, float x2, float E, float a, float b, float c, float d) {
float n = 0, xm, x0, f1, f2, f0;
f1 = f(x1, a, b, c, d);
f2 = f(x2, a, b, c, d);
if (f1 * f2 < 0) {
do {
x0 = (x1 * f2 - x2 * f1) / (f2 - f1);
f0 = f(x0, a, b, c, d);
if (f1 * f0 < 0) {
x2 = x0;
f2 = f0;
} else {
x1 = x0;
f1 = f0;
}
n++;
xm = (x1 * f2 - x2 * f1) / (f2 - f1);
} while (fabs(xm - x0) >= E);
int main() {
float x1, x2, E;
float a, b, c, d;
Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
printf("Enter coefficient b: ");
scanf("%f", &b);
return 0;
}
MEHUL NAINWAL/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
OUTPUT: 1.
OUTPUT:2.
Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
Objective: 4.Write a C Program to find the roots of the Polynomial Equation
using Iteration Method.
Program
#include <stdio.h>
#include <math.h>
do {
double f_x0 = polynomial(x0, degree, coefficients);
double f_prime_x0 = 0.0;
x1 = x0 - f_x0 / f_prime_x0;
x0 = x1;
iteration++;
} while (1);
return x1;
}
int main() {
int degree;
double tolerance;
MEHUL NAINWAL/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
printf("Enter the degree of the polynomial: ");
scanf("%d", °ree);
printf("Enter the coefficients of the polynomial (from highest degree to constant term):\n");
for (int i = 0; i <= degree; i++) {
scanf("%lf", &coefficients[i]);
}
double initial_guess;
printf("Enter the initial guess for the root: ");
scanf("%lf", &initial_guess);
return 0;
}
Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
OUTPUT: 1
OUTPUT: 2
MEHUL NAINWAL/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1