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

Code

Uploaded by

Saurabh Bhandari
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)
5 views

Code

Uploaded by

Saurabh Bhandari
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/ 4

Code:

#include <stdio.h>
#include <math.h>
double f(double x) {
return x * x * x * x - x - 10;
}
double df(double x) {
return 4 * x * x * x - 1;
}
void newtonRaphson(double x0, double tol) {
double x1;
int itr = 0;

if (fabs(f(x0)) < tol) {


printf("Root found: %f\n", x0);
return;
}
do {
x1 = x0 - f(x0) / df(x0);
itr++;
printf("Iterations %d: x0 = %f, x1 = %f, f(x1) = %f\n", itr, x0, x1, f(x1));
x0 = x1;
} while (fabs(f(x1)) >= tol && itr < 100);

printf("Root: %f\n", x1);


printf("f(x1): %f\n", f(x1));
printf("Total iterations: %d\n", itr);
}
int main() {
double x0, tol;
printf("Enter the initial guess (x0): ");
scanf("%lf", &x0);
printf("Enter the value of tolerance: ");
scanf("%lf", &tol);

if (tol <= 0) {
printf("Tolerance must be a positive number.\n");
return 0;
}
newtonRaphson(x0, tol);
return 0;
}

NAME: RAGHAV TYAGI SEC: K2 ROLL NO: 43


CODE:

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

double f(double x) { return x * x * x * x - x - 10; }

void secant(double x0, double x1, double tol) {


double x2;
int itr = 0;

while (fabs(f(x0)) >= tol && fabs(f(x1)) >= tol && itr < 100) {
x2 = x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0));
printf("Iter %d: x0 = %.6f, x1 = %.6f, x2 = %.6f, f(x2) = %.6f\n", ++itr, x0, x1, x2, f(x2));
x0 = x1;
x1 = x2;
}

printf("Root: %.6f\nf(Root): %.6f\nIterations: %d\n", x2, f(x2), itr);


}

int main() {
double x0, x1, tol;
printf("x0: ");
scanf("%lf", &x0);
printf("x1: ");
scanf("%lf", &x1);
printf("Tolerance: ");
scanf("%lf", &tol);
if (tol <= 0) {
printf("Tolerance must be positive.\n");
return 1;
}
secant(x0, x1, tol);
return 0;
}

NAME: RAGHAV TYAGI SEC: K2 ROLL NO: 43


CODE:

#include <stdio.h>
#include <math.h>
double f(double x) {
return x * x * x - 2 * x - 5;
}
void bisection(double a, double b, double tol) {
if (f(a) * f(b) >= 0) {
printf("Bisection failed because both values should be of opposite signs.\n");
return;
}
double c;
int itr = 0;
do {
c = (a + b) / 2;
if (f(c) == 0) {
break;
} else if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
itr++;
printf("Iterations %d: a = %f, b = %f, c = %f, f(c) = %f\n", itr, a, b, c, f(c));
} while (fabs(b - a) >= tol);
printf("Root: %f\n", c);
printf("f(c): %f\n", f(c));
printf("Total iterations: %d\n", itr);
}
int main() {
double a, b, tol;
printf("Enter the value of lower bound (a): ");
scanf("%lf", &a);
printf("Enter the value of upper bound (b): ");
scanf("%lf", &b);
printf("Enter the value of tolerance: ");
scanf("%lf", &tol);
if (tol <= 0) {
printf("Tolerance must be a positive number.\n");
return 1;
}
bisection(a, b, tol);
return 0;
}

NAME: RAGHAV TYAGI SEC: K2 ROLL NO: 43


CODE:

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

int main() {
double x_true, x_approx, absolute_error, relative_error, percentage_error;

x_true = 1.855585;
x_approx = 1.855586;

absolute_error = fabs(x_true - x_approx);


printf("Absolute Error: %.10f\n", absolute_error);

if (x_true != 0) {
relative_error = absolute_error / fabs(x_true);
printf("Relative Error: %.10f\n", relative_error);

percentage_error = relative_error * 100;


printf("Percentage Error: %.10f%%\n", percentage_error);
} else {
printf("Relative Error and Percentage Error are undefined because the true value is zero.\n");
}

return 0;
}

NAME: RAGHAV TYAGI SEC: K2 ROLL NO: 43

You might also like