0% found this document useful (0 votes)
17 views2 pages

CBNSTF

The document contains C code that implements numerical methods for finding roots of functions, including the bisection method and Newton's method. It also includes a section for solving systems of equations using Gaussian elimination and interpolation using divided differences. The code prompts the user for inputs such as bounds, error tolerance, and function values to perform calculations.

Uploaded by

mittalayush062
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)
17 views2 pages

CBNSTF

The document contains C code that implements numerical methods for finding roots of functions, including the bisection method and Newton's method. It also includes a section for solving systems of equations using Gaussian elimination and interpolation using divided differences. The code prompts the user for inputs such as bounds, error tolerance, and function values to perform calculations.

Uploaded by

mittalayush062
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/ 2

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

h>
#include <math.h> #include <math.h> #include <math.h>
double f(double x) { double f(double x) { double f(double x) {
return x * x - 4; } return x * x - 4; return x * x - 4;
int main() { } }
double a, b, c, error_tolerance; double f_derivative(double x) {
printf("Enter lower bound (a): "); int main() { return 2 * x;
scanf("%lf", &a); double a, b, c, error_tolerance; }
printf("Enter upper bound (b): ");
scanf("%lf", &b); int main() {
printf("Enter error tolerance: "); printf("Enter lower bound (a): "); double x0, x1, error_tolerance;
scanf("%lf", &error_tolerance); scanf("%lf", &a);
if (f(a) * f(b) >= 0) { printf("Enter upper bound (b): "); printf("Enter initial guess (x0): ");
printf("The function must have different signs at a and b.\n"); scanf("%lf", &b); scanf("%lf", &x0);
return 1; printf("Enter error tolerance: "); printf("Enter error tolerance: ");
} scanf("%lf", &error_tolerance); scanf("%lf", &error_tolerance);
do { do {
c = (a + b) / 2.0; if (f(a) * f(b) >= 0) { x1 = x0 - f(x0) / f_derivative(x0);
printf("c = %.6lf, f(c) = %.6lf\n", c, f(c)); printf("The function must have different signs at a and b.\n"); if (fabs(f(x1)) < error_tolerance) {
if (fabs(f(c)) < error_tolerance) { return 1; break;
break; } }
} x0 = x1;
if (f(c) * f(a) < 0) { do {
b = c; } while (fabs(f(x1)) >= error_tolerance);
} else { c = (a * f(b) - b * f(a)) / (f(b) - f(a)); printf("Root: x = %.6lf\n", x1);
a = c; printf("f(x) = %.6lf\n", f(x1));
}
} while (fabs(b - a) >= error_tolerance); if (fabs(f(c)) < error_tolerance) { return 0;
printf("Root: c = %.6lf\n", c); break; }
printf("f(c) = %.6lf\n", f(c)); return 0; }
}
if (f(c) * f(a) < 0) {
b = c;
} else {
a = c;
}
} while (fabs(b - a) >= error_tolerance);
printf("Root: c = %.6lf\n", c);
printf("f(c) = %.6lf\n", f(c));

return 0;
}

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


#include <math.h> #include <math.h> #define SIZE 10
double g(double x) { double fact(double x) void gauss(float matrix[SIZE][SIZE],int n)
return (x * x + 4) / 2; { {
} double f = 1; int i,j,k;
for (int i = 1; i <= x; i++) float factor;
int main() { { for(int i=0;i<n-1;i++)
double x0, x1, error_tolerance; f = f * i; {
} for(int j=i+1;j<n;j++)
printf("Enter initial guess (x0): "); return f;} {
scanf("%lf", &x0); int main(){ factor=matrix[j][i]/matrix[i][i];
printf("Enter error tolerance: "); int n;
scanf("%lf", &error_tolerance); printf("Enter the number of arguments: "); for(int k=0;k<=n;k++)
scanf("%d", &n); {
do { double arr[n][n + 1]; matrix[j][k]=matrix[j][k]-factor*matrix[i][k];
x1 = g(x0); printf("Enter values of x:\n"); }
printf("x1 = %.6lf\n", x1); for (int i = 0; i < n; i++){ }
if (fabs(x1 - x0) < error_tolerance) { scanf("%lf", &arr[i][0]); }
break; } //back substitution
} printf("Enter the values of f(x) or Y:\n"); float x[SIZE];
x0 = x1; for (int i = 0; i < n; i++){ for(int i=n-1;i<=0;i++)
scanf("%lf", &arr[i][1]); {
} while (fabs(x1 - x0) >= error_tolerance); } x[i]=matrix[i][n];
printf("Root: x = %.6lf\n", x1); for (int i = 2; i < n + 1; i++) for(int j=i+1;j<n;j++)
{ {
return 0; for (int j = 0; j < n - i + 1; j++) x[i]=x[i]-matrix[i][j]*x[j];
} { }
arr[j][i] = arr[j + 1][i - 1] - arr[j][i - 1]; x[i]=x[i]/matrix[i][i];
} }
} printf("The solution is:\n");
printf("\nThe forward difference table is shown as:\n"); for (i = 0; i < n; i++) {
for (int i = 0; i < n; i++) printf("x%d = %0.3f\n", i + 1, x[i]);
{ }
for (int j = 0; j < n - i + 1; j++) }
{ int main()
printf("%0.2lf\t", arr[i][j]); {
} float matrix[SIZE][SIZE];
printf("\n"); int i,j,n;
} printf("enter the number of equation\n");
double x; scanf("%d",&n);
printf("\nEnter a point of interpolation: "); printf("Enter the augmented matrix (coefficients and
scanf("%lf", &x); constants):\n");
double h = arr[1][0] - arr[0][0]; for (i = 0; i < n; i++) {
double u = (x - arr[0][0]) / h; for (j = 0; j <= n; j++) {
scanf("%f", &matrix[i][j]);
double result = arr[0][1]; }
double t = 1.0; }
gauss(matrix,n);
for (int i = 1; i < n; i++) }
{
t = t * (u - (i - 1));
result += (t * arr[0][i + 1]) / fact(i);
}

printf("The value of y at x = %0.2lf is %0.4lf\n", x, result);

return 0;
}

You might also like