CBNSTfile 1
CBNSTfile 1
CBNSTfile 1
AAYUSHI SINGH
REG.NO. 20236002
Ser.no. 3, G1 ME
Q7. Use the method of iteration to find a real root of the equation sin x =10(x-
1). Give your answer correct to three decimal places.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define f(x) sin(x)-10*x+10
#define g(x) (sin(x)+10)/10;
int main()
{
int step=1, N; float
x0, x1, e;
printf("Enter initial guess: "); scanf("%f",
&x0);
printf("Enter tolerable error: "); scanf("%f",
&e);
printf("Enter maximum iteration: "); 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",step, x0, f(x0), x1, f(x1));
step = step + 1;
if(step>N)
{
printf("Not Convergent.");
exit(0);
}
x0 = x1;
}while( fabs(f(x1)) > e);
printf("\nRoot is %f", x1); return(0);
}
Output:
Enter initial guess: 0
Enter tolerable error: 0.001
Enter maximum iteration: 5
Step x0 f(x0) x1 f(x1)
1 0.000000 10.000000 1.000000 0.841471
2 1.000000 0.841471 1.084147 0.042434
3 1.084147 0.042434 1.088390 0.001977
4 1.088390 0.001977 1.088588 0.000092
Root is 1.088588
x0 = x1;
}
if (error >= TOLERANCE) {
printf("Method did not converge within %d iterations\n", MAX_ITER);
}
return 0;
}
Output:
Enter initial guess: 1.5
Root found at x = 2.7984
Number of iterations: 10
Question 13
#include <stdio.h>
#include <math.h>
int main() {
// Initial guesses for the roots
double roots[] = {-1.0, 0.0, 1.0};
int len = sizeof(roots) / sizeof(roots[0]);
printf("Initial roots: Root1 = %.2f, Root2 = %.2f, Root3 = %.2f\n", roots[0], roots[1],
roots[2]);
quotient_difference_method(roots, len);
return 0;
}
Output
Initial roots: Root1 = -1.00, Root2 = 0.00, Root3 = 1.00
Iteration 1: Root1 = -1.00000000, Root2 = 0.00000000, Root3 = 1.00000000, Max Error =
10.00000000
Iteration 2: Root1 = 9.00000000, Root2 = -2.00000000, Root3 = 0.00000000, Max Error =
2.63636364
Iteration 3: Root1 = 6.47474747, Root2 = 0.63636364, Root3 = -0.11111111, Max Error =
1.05921550
Iteration 4: Root1 = 5.41553197, Root2 = 1.04576061, Root3 = 0.53870742, Max Error =
0.87825670
Iteration 5: Root1 = 5.14871356, Root2 = 1.92401731, Root3 = -0.07273086, Max Error =
0.26517800
Iteration 6: Root1 = 5.12430873, Root2 = 1.68324414, Root3 = 0.19244713, Max Error =
0.04514304
Iteration 7: Root1 = 5.12488777, Root2 = 1.63810110, Root3 = 0.23701113, Max Error =
0.00143023
Iteration 8: Root1 = 5.12488542, Root2 = 1.63667322, Root3 = 0.23844136, Max Error =
0.00000146
Iteration 9: Root1 = 5.12488542, Root2 = 1.63667176, Root3 = 0.23844282, Max Error =
0.00000000
Q2. Find a root of the equation f(x)=x3-x-1=0 by Bisection method correct to
three decimal places.
#include<stdio.h>
#include<math.h>
/*
Defining equation to be solved.
Change this equation to solve another problem.
*/
#define f(x) x*x*x-x-1;
int main()
{
float x0, x1, x2, f0, f1, f2, e; int step = 1; /* Inputs */
printf("\nEnter two initial guesses:\n"); scanf("%f%f", &x0, &x1); printf("Enter tolerable
error:\n"); scanf("%f", &e);
/* Calculating Functional Value */ f0 = f(x0); f1 = f(x1);
/* Checking whether given guesses brackets the root or not. */ if( f0 * f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
}
else if(f0*f1==0)
{
if(f0==0)
printf("The root is %f\n",x0); else
printf("The root is %f\n",x1);
}
else
{
/* Implementing Bisection Method */ printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do
{
x2 = (x0 + x1)/2; 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);
}
return 0;
}
Output:
Enter two initial guesses:
1
2
Enter tolerable error:
0.0001
Step x0 x1 x2 f(x2)
1
1.000000 2.000000 1.500000 0.875000
2
1.000000 1.500000 1.250000 -0.296875
3
1.250000 1.500000 1.375000 0.224609
4
1.250000 1.375000 1.312500 -0.051514
5
1.312500 1.375000 1.343750 0.082611
6
1.312500 1.343750 1.328125 0.014576
7
1.312500 1.328125 1.320312 -0.018711
8
1.320312 1.328125 1.324219 -0.002128
9
1.324219 1.328125 1.326172 0.006209
10
1.324219 1.326172 1.325195 0.002037
11
1.324219 1.325195 1.324707 -0.000046
Root is: 1.324707
Q4. The population of a town was as given below. Using Newton’s backward
difference, estimate the population for the year 1925
include <stdio.h>
double newton_backward_difference(int x, int n, int years[], int population[]) {
double result = population[n - 1]; // Start with the last term
double differences[n][n];
for (int i = 0; i < n; i++) {
differences[i][0] = population[i];
}
return result;
}
return 0;
}
Output:
enter target year:1925 Estimated Population in 1925 : 96.84 (in thousands)
Q9. Apply everett’s interpolation formula to find f(26) and f(27) from the given
table:
int main() {
double x[] = {15, 20, 25, 30, 35, 40};
double f[] = {12.849, 16.351, 19.524, 22.396, 24.999, 27.356};
int n = sizeof(x) / sizeof(x[0]);
Output:
Enter the value of x for which you want to find f(x): 26 Enter
the second value of x for which you want to find f(x): 27
f(26.00) = 20.12143 f(27.00) = 20.70708