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

CBNST

Uploaded by

Vighnesh Singhal
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)
15 views

CBNST

Uploaded by

Vighnesh Singhal
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

Objective: 1.

Write a program in c language to find the roots of given algebraic


equations using bisection method.

Program:
#include <stdio.h>
#include <math.h>

#define f(x) ((x*x*x) - 20)

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>

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


#define g(x) (1 + exp(-1))

void newton_raphson(double initial_guess, double tol)


{
double x = initial_guess;
double h = f(x) / g(x);
int iter = 0, max_iter = 100;

while (fabs(h) >= tol && iter < max_iter)


{
h = f(x) / g(x);
x = x - h;
iter++;
}
if (iter < max_iter)
{
printf("Approximate root: %.6lf\n", x);
printf("Number of iterations performed: %d\n", iter);
}
else
printf("Max iterations reached without convergence.\n");
}

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) = x – e-x g(x) = 1+e-x

𝒇(x) = 3x – cos(x) -1 g(x) = 3 + sin(x)

𝒇(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>

// Function to calculate f(x) based on user-defined coefficients


float f(float x, float a, float b, float c, float d) {
return a * pow(x, 3) + b * pow(x, 2) + c * x + d;
}

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

printf("Root of the given equation = %.6f\n", x0);


printf("No. of iterations = %.0f\n", n);
} else {
printf("Cannot find a root in the given interval\n");
}
}

int main() {
float x1, x2, E;
float a, b, c, d;

printf("Enter the coefficients of the equation ax^3 + bx^2 + cx + d = 0:\n");

printf("Enter coefficient a: ");


scanf("%f", &a);

Mehul Nainwal/ BTECH CSE/ (UNIVERSITY/CLASS) ROLL NO: (2219086/35)/ SEM-V/ SECTION- J1
printf("Enter coefficient b: ");
scanf("%f", &b);

printf("Enter coefficient c: ");


scanf("%f", &c);

printf("Enter coefficient d: ");


scanf("%f", &d);

printf("Enter the first initial guess x1: ");


scanf("%f", &x1);

printf("Enter the second initial guess x2: ");


scanf("%f", &x2);

printf("Enter the tolerance E: ");


scanf("%f", &E);

secant(x1, x2, E, a, b, c, d);

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>

double polynomial(double x, int degree, double coefficients[]) {


double result = 0.0;
for (int i = 0; i <= degree; i++) {
result += coefficients[i] * pow(x, degree - i);
}
return result;
}

double fixed_point_iteration(double initial_guess, int degree, double coefficients[], double


tolerance) {
double x0 = initial_guess;
double x1;
int iteration = 0;

do {
double f_x0 = polynomial(x0, degree, coefficients);
double f_prime_x0 = 0.0;

for (int i = 0; i < degree; i++) {


f_prime_x0 += coefficients[i] * (degree - i) * pow(x0, degree - i - 1);
}

x1 = x0 - f_x0 / f_prime_x0;

printf("Iteration %d: x = %.6f\n", iteration + 1, x1);

if (fabs(x1 - x0) < tolerance) {


break;
}

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", &degree);

double coefficients[degree + 1];

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

printf("Enter the tolerance for convergence: ");


scanf("%lf", &tolerance);

double root = fixed_point_iteration(initial_guess, degree, coefficients, tolerance);

printf("Approximate root: %.6f\n", root);

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

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