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

program cbnst

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)
17 views18 pages

program cbnst

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/ 18

PROGRAM-01

NAME- SHIV CHARAN SHARMA


ROLL NO.-64
SECTION-G2

1. OBJECTIVE: WAP to find the roots of non-linear equation using Bisection


method.
2. METHOD
Step1: Find two values a and b such that f(a)*f(b)
Step2: Bisect (a,b) into two halves, f0=f(a), f1=f(b), f2=f(x)
Step3: Loop: Iterate using x = (a + b) / 2
if f0. f2 < 0
x1=x2
else x0= x2
Step4: if (fabs(x-a)<=0.0001 || fabs( x-b)<= 0.0001)
break;
Step5: Stop the procedure after finding the desired solution.

3. PROGRAM
#include<stdio.h>
#include<math.h>
double fun1(double a)
{
double x = pow(a,3)-(4*a)-9;
return x;
}
int main()
{

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


int n=0;
double a = 0;
double b = 3;
double x,x1;
do
{
x1=x;
x = (a + b) / 2.0;
if ((fun1(a) * fun1(x)) < 0)
{
b = x;
}
else if ((fun1(x) * fun1(b)) < 0)
{
a = x;
}
n++;
printf("The root is approximately: %lf\n", x);
}
while((fabs(x-x1))>0.0001);
printf("iteration = %d",n);
return 0;
}

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


OUTPUT:
The root is approximately: 1.500000
The root is approximately: 2.250000
The root is approximately: 2.625000
The root is approximately: 2.812500
The root is approximately: 2.718750
The root is approximately: 2.671875
The root is approximately: 2.695312
The root is approximately: 2.707031
The root is approximately: 2.701172
The root is approximately: 2.704102
The root is approximately: 2.705566
The root is approximately: 2.706299
The root is approximately: 2.706665
The root is approximately: 2.706482
The root is approximately: 2.706573
iteration = 15

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


PROGRAM-02
NAME- SHIV CHARAN SHARMA
ROLL NO.-64
SECTION-G2

1. OBJECTIVE: WAP to find the roots of non-linear equation using Regula


Falsi method.

2. METHOD
Step 1: Choose two initial values a and b such that f(a)×f(b)

Step 2: Apply the Secant Method formula to find a new approximation


x: x= b-(((b- a)/(fun1(b)-fun1(a))*(fun1(b))));
Step 3: Update the interval:
• If f(a) * f(x)
• else, set a=x;
Step 4: Repeat steps 2 and 3 until the difference between consecutive
approximations |x−xpervious| is less than the tolerance (e.g., 0.0001).

Step 5: Stop the procedure when the desired accuracy is achieved, with xxx
being the root of the function.

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


3. PROGRAM
#include<stdio.h>
#include<math.h>
double fun1(double a)
{
double x = pow(a,3)-(4*a)-9;
return x;
}
int main() {
int n=0;
double a = 0;
double b = 3;
double x,x1;
do
{
x1=x;
x = b-(((b-a)/(fun1(b)-fun1(a))*(fun1(b))));
if ((fun1(a) * fun1(x)) < 0)
{
b = x;
}
else if ((fun1(x) * fun1(b)) < 0)
{
a = x;
}
n++;
printf("The root is approximately: %lf\n", x);

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


}
while((fabs(x-x1))>0.0001);
printf("iteration = %d",n);
return 0;
}

OUTPUT:
The root is approximately: 1.800000
The root is approximately: 2.560117
The root is approximately: 2.688061
The root is approximately: 2.704286
The root is approximately: 2.706257
The root is approximately: 2.706495
The root is approximately: 2.706524
iteration = 7

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


PROGRAM-03
NAME- SHIV CHARAN SHARMA
ROLL NO.-64
SECTION-G2

1. OBJECTIVE: To find the root of given function using Iterative method in a


given program.
2. METHOD
Step 1: Initialize:
• Set x to x0 (initial guess)
• Set iteration to 0
Step 2: Iterate:
• Calculate x1 using the formula: x1 = x - f(x) / f_prime(x)
• Print the current iteration number, x1, and f(x1) values
• Update x to x1
• Increment iteration by 1
Step 3: Convergence Check:
• Check if fabs(f(x)) is less than or equal to epsilon
• If true, exit the loop
Step 4: Repeat:
• Go back to step 2 until convergence is reached
Step 5: Output:
• Print the final root x and the number of iterations iteration

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


3. PROGRAM
#include <stdio.h>
#include <math.h>
double f(double x) {
return x*x*x - 4*x - 9;
}
double f_prime(double x) {
return 3*x*x - 4;
}
int main(){
double x0;
double x1;
double epsilon = 0.00001; // tolerance for convergence
printf("Enter initial guess: ");
scanf("%lf", &x0);
int iteration = 0;
do {
iteration++;
x1 = x0 - f(x0) / f_prime(x0);
printf("%d Iteration, value of x is %.6lf, value of f(x) is %.6lf\n",
iteration, x1, f(x1));
x0 = x1;
} while (fabs(f(x0)) > epsilon);
printf("The root is: %.6lf after %d iterations\n", x0, iteration);
return 0;
}

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


OUTPUT:
Enter initial guess: 2
1 Iteration, value of x is 3.125000, value of f(x) is 9.017578
2 Iteration, value of x is 2.768530, value of f(x) is 1.145993
3 Iteration, value of x is 2.708196, value of f(x) is 0.030014
4 Iteration, value of x is 2.706529, value of f(x) is 0.000023
5 Iteration, value of x is 2.706528, value of f(x) is 0.000000
The root is: 2.706528 after 5 iterations

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


PROGRAM-04
NAME- SHIV CHARAN SHARMA
ROLL NO.-64
SECTION-G2

1. OBJECTIVE: To find the real root of an equation using Newton


Raphson Method.

2. METHOD
Algorithm
1. Define the Function and Its Derivative:
• Function f(x)=x3−4x−9f(x) = x^3 - 4x - 9f(x)=x3−4x−9
• Derivative f′(x)=3x2−4f'(x) = 3x^2 - 4f′(x)=3x2−4

2. Initialize Variables:
• Set an initial guess x0x_0x0.
• Define the tolerance level for convergence (0.0001).
• Set a maximum number of iterations to prevent infinite loops.

3. Perform Iterations:
• Compute f(x)f(x)f(x) and f′(x)f'(x)f′(x).
• Update the guess using the Newton-Raphson formula:
Xnew=x−f(x)/f′(x)
• Check for convergence: if the absolute difference between the new and
old guesses is less than the tolerance, the method has converged.
• Print each iteration's details.
• If the method does not converge within the maximum number of
iterations, output a message indicating failure.

4. Output the Result:


• Print the final approximation of the root.

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


3. PROGRAM
#include <stdio.h>
#include <math.h>
double f(double x) {
return x*x*x - 4*x - 9;
}
double f_prime(double x) {
return 3*x*x - 4;
}
void newtonRaphson(double x0, double tolerance, int maxIterations) {
double x = x0;
double x_new;
int iteration = 0;
printf("Iteration | x | f(x) | f'(x) | x_new\n");
printf(" \n");
while (iteration < maxIterations) {
double fx = f(x);
double fx_prime = f_prime(x);
if (fabs(fx_prime) < 1e-6) {
printf("Derivative too small, method fails.\n");
return;
}
x_new = x - fx / fx_prime;
printf("%d | %.6f | %.6f | %.6f | %.6f\n", iteration, x, fx, fx_prime, x_new);
if (fabs(x_new - x) < tolerance)
{
printf("Converged to root: %.6f\n", x_new);

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


return;
}
x = x_new;
iteration++;
}
printf("Method did not converge within the maximum number of iterations.\n");
}
int main() {
double initialGuess = 3.0;
double tolerance = 0.0001;
int maxIterations = 20;
newtonRaphson(initialGuess, tolerance, maxIterations);
return 0;
}

OUTPUT:
For fn x3 -4x-9

Iteration | x | f(x) | f'(x) | x_new

0 | 3.000000 | 6.000000 | 23.000000 | 2.739130


1 | 2.739130 | 0.594723 | 18.508507 | 2.706998
2 | 2.706998 | 0.008451 | 17.983514 | 2.706528
3 | 2.706528 | 0.000002 | 17.975882 | 2.706528
Converged to root: 2.706528

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


PROGRAM-05
NAME- SHIV CHARAN SHARMA
ROLL NO.-64
SECTION-G2

1.OBJECTIVE: Write a program to numerically using Newton’s forward


difference method.

2.PROGRAM:
#include<stdio.h>
#include<math.h>
int main()
{
float x,u1,u,y;
int i,j,n,fact;
printf("Enter no. of terms\n");
scanf("%d",&n);
float a[n][n+1];
printf("Enter Values of X\n");
for(i=0;i<n;i++)
scanf("%f",&a[i][0]);
printf("Enter Values of Y\n");
for(i=0;i<n;i++)
scanf("%f",&a[i][1]);
printf("Enter value of x for which you want y\n");
scanf("%f",&x);
for(j=2;j<n+1;j++)
{
for(i=0;i<n-j+1;i++)
a[i][j] = a[i+1][j-1]-a[i][j-1];
NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)
}
printf("The Difference Table is as follows:\n");
for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
printf("%f ",a[i][j]);
printf("\n");
}
u= (x - a[0][0])/(a[1][0]-a[0][0]);
y=a[0][1];
u1=u;
fact=1;
for(i=2;i<=n;i++)
{
y=y+(u1*a[0][i])/fact;
fact=fact*i;
u1=u1*(u-(i-1));
}
printf("\n\nValue at X=%g is = %f", x,y);

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


OUTPUT:

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


PROGRAM-05
NAME- SHIV CHARAN SHARMA
ROLL NO.-64
SECTION-G2

1.OBJECTIVE: Write a program to numerically using Newton’s backword


difference method.

2.PROGRAM:
#include<stdio.h>
#include<math.h>

int main()
{
float x,u1,u,y;
int i,j,n,fact;
printf("Enter no. of terms\n");
scanf("%d",&n);
float a[n][n+1];
printf("Enter Values of X \n");
for(i=0;i<n;i++)
scanf("%f",&a[i][0]);
printf("Enter Values of Y\n");
for(i=0;i<n;i++)
scanf("%f",&a[i][1]);
printf("Enter value of x for which you want y\n");
scanf("%f",&x);
for(j=2;j<n+1;j++)
{
for(i=0;i<n-j+1;i++)
NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)
a[i][j] = a[i+1][j-1]-a[i][j-1];
}
printf("The Difference Table is as follows:\n");
for(i=0;i<n;i++)
{
for(j=0;j<=n-i;j++)
printf("%f ",a[i][j]);
printf("\n");
}
u= (x - a[n-1][0])/(a[1][0]-a[0][0]);
y=a[n-1][1];
u1=u;
fact=1;
j=2;
for(i=n-2;i>=0;i--)
{
y=y+(u1*a[i][j])/fact;

fact=fact*j;
u1=u1*(u+(j-1));
j++;
}
printf("\n\nValue at X=%g is=%f",x,y);
}

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)


OUTPUT:

NAME-SHIV CHARAN SHARMA SECTION-G2 SEM.-5TH ROLL NO.-2219632(64)

You might also like