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

FINAL1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

FINAL1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PROGRAM 1

AIM : Program for finding roots of f(x)=0 by bisection method.

THEORY : Bisection Method is one of the simplest, reliable, easy to implement and
convergence guaranteed method for finding real root of non-linear equations. It is also known as
Binary Search or Half Interval or Bolzano Method.

Bisection method is bracketing method and starts with two initial guesses say x0 and x1

such that x0 and x1 brackets the root i.e. f(x0)f(x1)<0

Bisection method is based on the fact that if f(x) is real and continuous function, and for two

initial guesses x0 and x1 brackets the root such that: f(x0)f(x1) <0 then there exists atleast

one root between x0 and x1.

Root is obtained in Bisection method by successive halving the interval i.e. If x0 and x1 are

two guesses then we compute new approximated root as:

x2 = (x0 + x1)/2

Now we have following three different cases:

If f(x2)=0 then the root is x2.

If f(x0)f(x2)<0 then root lies between x0 and x2. If f(x0)f(x2)> 0 then root lies between x1

and x2.

And then process is repeated until we find the root within desired accuracy.

ALGORITHM:
1. Start

2. Define function f(x)

3. Choose initial guesses x0 and x1 such that f(x0)f(x1) < 0

4. Choose pre-specified tolerable error e.

5. Calculate new approximated root as x2 = (x0 + x1)/2


6. Calculate f(x0)f(x2)

a. if f(x0)f(x2) < 0 then x0 = x0 and x1 = x2

b. if f(x0)f(x2) > 0 then x0 = x2 and x1 = x1

c. if f(x0)f(x2) = 0 then goto (8)

7. if |f(x2)| > e then goto (5) otherwise goto (8)

8. Display x2 as root.

9. Stop
CODE

#include <stdio.h>

#include <math.h>

#define e 0.001

float func(float x) {

float y = pow(x, 3) - 4*x - 9;

return y;

int main() {

float x0, x1, x2;

int i;

up:

printf("Enter x0: ");

scanf("%f", &x0);

printf("Enter x1: ");

scanf("%f", &x1);

if (func(x0) * func(x1) < 0) {

printf("The root lies between x0 and x1...\n");

} else {

printf("The root does not lie between x0 and x1...\n");

printf("Input different values...\n");

goto up;

do {

x2 = (x0 + x1) / 2;
if (func(x0) * func(x2) < 0) {

x1 = x2;

} else {

x0 = x2;

i++;

printf("No. of iterations: %d\n", i);

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

printf("Function value at current root: %f\n", func(x2));

} while (fabs(x0 - x1) >= e);

return 0;

}
GRAPH:
VIVA QUESTIONS:

a. What are Algebraic and Transcendental functions?

b. Difference between Bisection Method and Newton Raphson Method?


PROGRAM 2
AIM: Program for finding roots of f(x)=0 Newton Raphson method.

THEORY: The Newton-Raphson method which is also known as Newton’s


method, is an iterative numerical method used to find the roots of a real-
valued function. This formula is named after Sir Isaac Newton and Joseph
Raphson, as they independently contributed to its development. Newton
Raphson Method or Newton’s Method is an algorithm to approximate the
roots of zeros of the real-valued functions, using guess for the first iteration
(x0) and then approximating the next iteration(x1) which is close to roots,
using the following formula.

Newton Raphson Method is an open method and starts with one initial guess
for finding real root of non-linear equations.

In Newton Raphson method if x0 is initial guess then next approximated root


x1 is obtained

by following formula: x1 = x0 - f(x0) / g(x0) And an algorithm for Newton


Raphson method involves repetition of above process i.e. we use x1 to find
x2 and so on until we find the root within desired accuracy.

ALGORITHM:

1. Start

2. Define function as f(x)

3. Define first derivative of f(x) as g(x)

4. Input initial guess (x0), tolerable error (e) and maximum iteration (N)

5. Initialize iteration counter i=1

6. If g(x0)=0 then print "Mathematical Error" and go to (12) otherwise go to (7)

7. Calcualte x1=x0-f(x0) /g(x0)

8. Increment iteration counter i =i +1

9. If i>=N then print "NotConvergent" and go to (12) otherwise go to (10)

10. If |f(x1)|>e then set x0 =x1 and go to (6) otherwise go to (11)


11. Print root as x1

12. Stop

CODE
#include <stdio.h>

#include <math.h>

#define e 0.0001

double func(double x) {

return (pow(x, 3) - 3 * x - 5);

}
double dfunc(double x) {

return (3 * pow(x, 2) - 3);

int main() {

printf("Name: Aarush Gupta\n Class: IT \nRoll No.: 133\n");

printf("Newton Raphson Method");

double x0, x1;

printf("Enter x0: ");

scanf("%f", &x0);

int i = 1, maxitr;

printf("Enter the maximum number of iterations: ");

scanf("%d", &maxitr);

while (i <= maxitr) {

x1 = x0 - (func(x0) / dfunc(x0));

if (fabs(x1 - x0) < e) {

printf("Root found at x = %lf\n", x1);

break;

x0 = x1;

printf("Iteration %d: x = %lf, f(x) = %lf\n", i, x0, func(x0));

i++;

if (i > maxitr) {

printf("Root not found within %d iterations\n", maxitr);

return 0;
}

OUTPUT:

GRAPH:
VIVA QUESTIONS:

a. Define Newton Raphson Method.

b. What is the Advantage and Disadvantage of Newton Raphson Method?

c. State any real-life application of Newton Raphson’s Method.

d. Which theory is the Newton-Raphson Method based upon?


PROGRAM 3
AIM: Program for finding roots of f(x)=0 by secant method.

and x1 are initial guesses the next approximated root x2 is obtained by

THEORY: SecantMethod is open method and starts with two initial guesses for finding real
root of non-

linear equations.

In Secant method if x0 following formula:

x2 =x1 -(x1-x0)*f(x1)/(f(x1)-f(x0) )

And an algorithm for the Secant method involves repetition of the above process i.e. we use

to find x3 and so on until we find the root within the desired accuracy.

x1 and x2

ALGORITHM:
1. Start

2. Define function as f(x)

3. Input initial guesses (x0 and x1), tolerable error (e) and maximum iteration (N)

4. Initialize iteration counter i = 1

5. If f(x0) = f(x1) then print "Mathematical Error" and goto (11) otherwise goto (6)

6. Calculate x2 = x1 - (x1-x0) * f(x1) / ( f(x1) - f(x0) )

7. Increment iteration counter i = i + 1

8. If i>= N then print "Not Convergent" and goto (11) otherwise goto (9)

9. If |f(x2)| > e then set x0 = x1, x1 = x2 and goto (5) otherwise goto (10)

10. Print root as x2


11. Stop
CODE
#include <stdio.h>

#include <math.h>

double func(double x) {

return (pow(x, 3) - 4 * x - 9);

double secant(double x0, double x1) {

double a = (x0 * func(x1)) - (x1 * func(x0));

double b = (func(x1) - func(x0));

return a / b;

int main() {

printf("Name: Daksh \nClass: ITE \nRoll No.: 067\n");

printf("Secant Method\n");

double x0, x1, x2;

printf("Enter initial guess for x0: ");

scanf("%lf", &x0);

printf("Enter initial guess for x1: ");

scanf("%lf", &x1);

for (int i = 0; i < 20; i++) {

x2 = secant(x0, x1);

printf("Iteration: %d\n", i + 1);

printf("Value of x2: %lf, Value of f(x2): %lf\n", x2, func(x2));

if (fabs(x2 - x1) < 0.0001) {

printf("Root of the given equation using secant method is %lf\n", x2);

break;
}

x0 = x1;

x1 = x2;

return 0;

OUTPUT:
GRAPH :

VIVA QUESTIONS:

a. What is the graphical meaning of Secant method?

b. Does secant method give guarantee of convergence?

c. What is the formula for finding the root by using the secant method?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy