Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab:01

Bisection method to find the root of x3 -4x:

#include<stdio.h>

double func(double x) {

return x*x*x-4*x-9;

double bisection(double a, double b, int max_iter) {

double c, f_a, f_c;

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

c = (a+b)/2;

f_a = func(a);

f_c = func(c);

if (f_a*f_c<0){

b=c;

else{

a=c;

printf("Root found: %lf\n", c);

return c;

int main(){

double a, b, tol;

int max_iter;

printf("Enter the left point of interval:");


scanf("%lf", &a);

printf("Enter the Right point of interval:");

scanf("%lf", &b);

printf("Enter Max. number of Iterations:");

scanf("%d", &max_iter);

double root = bisection(a,b, max_iter);

return 0;

Output :
Lab:02
Newton raphson method to find the root of x3 -4x:

#include <stdio.h>

#include <math.h>

double func(double x) {

return x*x*x-4*x-9;

double derivative(double x) {

return 3*x*x-4;

double newton_rapson(double x0, double epsilon, int max_iter){

double x = x0;

double x_prev, f_prime;

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

x_prev = x;

double f = func(x_prev);

double f_prime = derivative(x_prev);

if(f_prime== 0) {

printf("Error: Division by zero occured.\n");

return x;

x=x_prev - f/f_prime;

if(fabs(x-x_prev)<epsilon) {

printf("Convergence achived.\n");

printf("Root found: %lf\n",x);

return x;

}
}

printf("Max. no. of Iterations reacherd.\n");

printf("Best approximation: %lf\n",x);

return x;

int main(){

double x0, epsilon;

int max_iter;

printf("Enter Initial approximation:");

scanf("%lf", &x0);

printf("Enter the covergence tolotance(epsilon):");

scanf("%lf",&epsilon);

printf("Enter the Max. no. of Iterations:");

scanf("%d", &max_iter);

double root = newton_rapson(x0, epsilon, max_iter);

return 0;

Output:
Lab:03
Regulafalsi Method to find the root of x3 -4x:

#include <stdio.h>

#include <math.h>

double func(double x) {

return x*x*x-4*x-9;

double regula_falsi(double a, double b, int max_iter) {

double c, f_a, f_c;

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

f_a = func(a);

f_c = func(c);

if(f_a*f_c>0) {

a=c;

}else{

b=c;

c=(a*func(b)-b*func(a))/(func(b)-func(a));

printf("Root found: %lf\n",c);

return c;

int main() {

double a,b;

int max_iter;

printf("Enter left point: ");

scanf("%lf", &a);

printf("Enter Right point: ");


scanf("%lf", &b);

printf("Enter Max. no. of Iterations:");

scanf("%d", &max_iter);

double root = regula_falsi(a, b, max_iter);

return 0;

Output:

You might also like