[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here

Numerical Analysis

Created: Wednesday, 05 March 2014

Lectures

Google drive mirror: Click here

 

Programs

These Programs are written in C .. Not written by Ehab Eldeeb
If you have any improvements on the code supplied, you are more than welcome to modify and re-post it  down here in the comments.

Programs Available:

  • Bisection Method
  • Secant Method
  • False Position Method
  • Muller's Method

 

Bisection Method

#include<stdio.h>
#include <math.h>
#include<conio.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + (x)*(x) + (x) + 7
void main(){
  int i = 1;
  float x0,x1,x2;
  double f1,f2,f0,t;
  printf("\nEnter the value of x0: ");
  scanf("%f",&x0);

  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\n__________________________________________________________________\n");
  printf("\niteration\t x0\t       x1\t x2\t   f0\t   f1\t   f2");
  printf("\n___________________________________________________________________\n");
  do{
  x2=(x0+x1)/2;
  f0=F(x0);
  f1=F(x1);
  f2=F(x2);
  printf("\n%d %f %f %f %lf %lf %lf", i, x0,x1,x2,f0,f1,f2);
  if(f0*f2<0)
   {
    x1=x2;
   }
   else
   {
    x0=x2;
   }
   i++;
  }while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}

Secant Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) (x)*(x) - 4*(x) - 10
void main(){
  float x1,x2,x3,f1,f2,t;
  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\nEnter the value of x2: ");
  scanf("%f",&x2);
  printf("\n______________________________________________\n");
  printf("\n    x1\t  x2\t  x3\t     f(x1)\t f(x2)");
  printf("\n______________________________________________\n");
  do {
  f1=F(x1);
  f2=F(x2);
  x3=x2-((f2*(x2-x1))/(f2-f1));
  printf("\n%f   %f   %f   %f   %f",x1,x2,x3,f1,f2);
  x1=x2;
  x2=x3;
  if(f2<0)
    t=fabs(f2);
  else
    t=f2;
  }while(t>ESP);
printf("\n______________________________________________\n");
printf("\n\nApp.root = %f",x3);
getch();
}

False Position Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x){
return (x*x*x-5*x+1);
}
int main(){
int itr=0, maxitr;
float x1,x2,x3,x4,aerr;
printf("\nProgram to find Root of an Equation by Regula falsi Method\n\n");
printf("\nEnter value of x0, x1, allowed error and maximum iteration\n");
scanf("%f %f %f %d", &x1, &x2, &aerr, &maxitr);
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
printf("\n\nIn iteration %d, Value of x3 =\t%f",itr+1,x3);
do{
if (f(x1)*f(x3)<0)
x2=x3;
else
x1=x3;
x4=x3;
x3=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
itr++;
printf("\nIn iteration %d Value of x=\t%f", itr+1,x3);
if (fabs(x4-x3)<aerr){
printf("\n\nAfter %d iteration, Root= %f", itr,x4);
getch();
return 0;
}
}
while (itr<maxitr);
printf("\n\nSolution does not converge Iteration not sufficient");
getch();
return 1;
}

Muller's Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + 2*(x)*(x) + 10*(x) - 20

void main(){
  double x1,x2,x3,x4_1,x4_2,fx1,fx2,fx3,
     h1,h2,h3_1,h3_2,h4,D,d1,d2,a1,a2,a0;
  int i=1;
  printf("\nEnter the value of x1: ");
  scanf("%lf",&x1);
  printf("\nEnter the value of x2: ");
  scanf("%lf",&x2);
  printf("\nEnter the value of x3: ");
  scanf("%lf",&x3);
  fx1 = F(x1);
  printf("\n\n f(x1) = %lf",fx1);
  getch();
  fx2 = F(x2);
  printf("\n\n f(x2) = %lf",fx2);
  getch();
  fx3 = a0 = F(x3);
  printf("\n\n f(x3) = %lf",fx3);
  getch();
  h1 = x1-x3;
  h2 = x2-x3;

  d1 = fx1-fx3;
  d2 = fx2-fx3;

  D = h1*h2*(h1-h2);

  a1 = (d2*h1*h1 - d1*h2*h2)/D;
  a2 = (d1*h2 - d2*h1)/D;

  h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
  h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));

  if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
         ((a1 - sqrt(fabs(a1*a1 - (4*a2*a0))))) )
  {
   h4 = h3_1;
  }
  else
  {
   h4 = h3_2;
  }
  x4_1 = x3 + h4;
  printf("\n\n\n x4 = %lf \n",x4_1);

  x1=x2;
  x2=x3;
  x3=x4_1;
  printf("\n\nx1 = %lf",x1);
  printf("\n\nx2 = %lf",x2);
  printf("\n\nx3 = %lf",x3);
  getch();

  do
  {
   fx1 = F(x1);
   fx2 = F(x2);
   fx3 = a0 = F(x3);

   h1 = x1-x3;
   h2 = x2-x3;

   d1 = fx1-fx3;
   d2 = fx2-fx3;

   D = h1*h2*(h1-h2);

   a1 = (d2*h1*h1 - d1*h2*h2)/D;
   a2 = (d1*h2 - d2*h1)/D;

   h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
   h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));

   if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
       &n