Non Linear Equation Lab Manual
Non Linear Equation Lab Manual
Lab no.:
Lab title:
Submission date:
/* PROGRAMME TO IMPLEMENT BISECTION METHOD */
Objective: to find the root of the nonlinear equation by bisection method
Theory: briefly describe about the method with figure and formula whichever applicable.
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
void main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
/* Inputs */
up:
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");
goto up;
}
/* 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);
getch();
}
1. Start
2. Take two initial guess ‘a’ and ‘b’ such that f(a)*f(b)<0
3. Define tolerable error (e).
4. Calculate c = a f(b)-b f(a) / f(b)-f(a)
5. If (f(c)*f(a)>0)
{
a=c;
f(a)=f(c);
}
Else{
b=c;
f(b)=f(c);
}
6. If |f(c)<e| goto 7
Else goto 4
7. Stop and print root ‘c’.
c- code:
#include <stdio.h>
#include <math.h>
double f(double x) {
// Define the function whose root is to be found
return pow(x, 3) - x - 1;
}
iter++;
} while (fabs(fc) > tol && iter < max_iter);
return c;
}
int main() {
// Example usage
double x,y;
printf("enter the initial guess x: \n");
scanf("%lf",&x);
printf("enter the initia guess y: \n");
scanf("%lf",&y);
if (f(x)*f(y)<0)
{
double root = false_position(x, y, 0.0001, 100);
printf("The root is: %f\n", root);
}
else{
printf("wrong guess try again");
}
return 0;
}
Objective: To find the root of the nonlinear equation by false position method
Theory: briefly describe about the method with figure and formula whichever applicable.
Algorithm:
1. Start
2. Define g(x)
3. Choose initial guess x0;
4. Find x=g(x0);
5. If |x-x0<e| goto 6
Else
x0=x and goto 4
6. Stop and print the root as ‘x’.
c-code:
#include <stdio.h>
#include <math.h>
double f(double x) {
// Define your function here
return exp(-x) - x;
}
double g(double x) {
// Define your g(x) function here
return exp(-x);
}
int main() {
double x0, x, tolerance;
int iterations;
Objective: To find the root of the nonlinear equation by false position method
Theory: briefly describe about the method with figure and formula whichever applicable.
Algorithm:
1. Choose a starting value for x, denoted by x_0.
2. Compute the value of the function f(x_0) and its derivative f'(x_0) at x_0.
3. Compute the next estimate for the root using the formula: x_1 = x_0 - f(x_0) / f'(x_0).
4. Repeat steps 2-3 until the function value is sufficiently close to zero, i.e., until |f(x_n)| < epsilon,
where epsilon is a small positive number that determines the desired accuracy of the root.
5. The final value of x is the estimated root of the function.
C code:
#include <stdio.h>
#include <math.h>
double f(double x) {
// Define your function here
return x * log10(x) - 1.2;
}
double f_prime(double x) {
// Define the derivative of your function here
return 0.43429 + log10(x);
}
double newton_raphson(double x) {
double x_next = x;
double fx, fpx;
int iter = 1;
while (1) {
fx = f(x_next);
fpx = f_prime(x_next);
return x_next;
}
int main() {
double root = newton_raphson(2); // Starting initial value for x
printf("The root is: %lf", root);
return 0;
}