Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CBNSTfile 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

COMPUTER BASED

NUMERICAL AND STATISTICAL


TECHNIQUES
Assignment 1 and 2

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

Q9. Using Newton-Raphson method, find a real root, correct to 4 decimal


places, of the equation x sin x + cos x =0.
#include <stdio.h>
#include <math.h>
#define MAX_ITER 1000 #define
TOLERANCE 1e-8 double
f(double x) {
return x * sin(x) + cos(x);
}
double df(double x) {
return x * cos(x) - sin(x);
}
int main() { double x0, x1,
error; printf("Enter initial
guess: "); scanf("%lf", &x0);
for (int i = 1; i <= MAX_ITER; ++i) {
x1 = x0 - f(x0) / df(x0);

error = fabs(x1 - x0); if (error <


TOLERANCE) { printf("Root found at x
= %.4lf\n", x1);
printf("Number of iterations: %d\n", i);
break;
}

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>

#define MAX_ITER 100


#define EPSILON 1e-6

// Define the function f(x)


double f(double x) {
return pow(x, 3) - 7 * pow(x, 2) + 10 * x - 2;
}

// Function to calculate the derivative of f(x) at a specific root


double fprime(double *roots, int k, int len) {
double result = 1.0;
for (int i = 0; i < len; i++) {
if (i != k) {
result *= (roots[k] - roots[i]);
}
}
return result;
}

// Quotient-Difference Method implementation


void quotient_difference_method(double *roots, int len) {
double max_error;
double new_roots[len];

for (int iter = 0; iter < MAX_ITER; iter++) {


max_error = 0.0;

for (int j = 0; j < len; j++) {


new_roots[j] = roots[j] - f(roots[j]) / fprime(roots, j, len);

if (fabs(new_roots[j] - roots[j]) > max_error) {


max_error = fabs(new_roots[j] - roots[j]);
}
}

printf("Iteration %d: ", iter + 1);


for (int j = 0; j < len; j++) {
printf("Root%d = %.8f, ", j + 1, roots[j]);
}
printf("Max Error = %.8f\n", max_error);

if (max_error < EPSILON) {


break;
}

for (int j = 0; j < len; j++) {


roots[j] = new_roots[j];
}
}
}

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];
}

for (int j = 1; j < n; j++) {


for (int i = 0; i < n - j; i++) {
differences[i][j] = differences[i + 1][j - 1] - differences[i][j - 1];
}
}

double term = 1.0;


double u = (x - years[n - 1]) / (double)(years[1] - years[0]);

for (int j = 1; j < n; j++) {


term *= (u + j - 1) / j;
result += term * differences[n - j - 1][j];
}

return result;
}

int main() { // Given data int years[] =


{1891, 1901, 1911, 1921, 1931}; int
population[] = {46, 66, 81, 93, 101};
int n = sizeof(years) / sizeof(years[0]);
int x;
printf("enter target year:");
scanf("%d",&x);
double estimated_population = newton_backward_difference(x, n, years, population);

printf("Estimated Population in %d : %.2f (in thousands)\n", x,estimated_population);

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:

#include <stdio.h> double


factorial(int n) {
if (n == 0)
return 1; else
return n * factorial(n - 1);
}

double everett_coefficient(int n, int i, double target_x, double* x) {


double result = 1.0;
for (int j = 0; j <= n; j++) {
if (j != i) {
result *= (target_x - x[j]) / (x[i] - x[j]);
}
}
return result;
}

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

double target_x1, target_x2;


printf("Enter the value of x for which you want to find f(x): ");
scanf("%lf", &target_x1);
printf("Enter the second value of x for which you want to find f(x): ");
scanf("%lf", &target_x2);

double sum1 = 0.0;


double sum2 = 0.0;
for (int i = 0; i < n; i++) {
sum1 += f[i] * everett_coefficient(n - 1, i, target_x1, x);
sum2 += f[i] * everett_coefficient(n - 1, i, target_x2, x);
}

printf("f(%.2lf) = %.5lf\n", target_x1, sum1);


printf("f(%.2lf) = %.5lf\n", target_x2, sum2);
return 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

You might also like