Life is like a box of chocolates, you never know what you're going to get..
Wednesday, October 5, 2016
প্রোগ্রামিং-এর গল্প (A tale of Programming) : Numerical Method- Coding for Newton Raphson Method
//Program for Newton-Raphson Method
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*x-4*x-10;
}
float fdx(float x)
{
return 2*x-4;
}
main()
{
float x1,x2,f1,f2,e,error,root;
int i=0;
printf("Enter the initial value x1:");
scanf("%f",&x1);
printf("Enter the error limit e:");
scanf("%f",&e);
while(1)
{
i++;//i=i+1;
f1=f(x1);
f2=fdx(x1);
x2=x1-(f1/f2);
error=fabs((x2-x1)/x2);
if(error<e)
{
root=x2;
printf("\n\n\n\nNo. of iteration:%d",i-1);
printf("\n\nFinal functional value:%f",f1);
printf("\n\nFinal root:%f",root);
break;
}
else
{
x1=x2;
printf("\n\nIteration=%d x1=%.2f x2=%.2f f1=%.2f f2=%.2f error=%.2f",i,x1,x2,f1,f2,error);
}
}
}
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*x-4*x-10;
}
float fdx(float x)
{
return 2*x-4;
}
main()
{
float x1,x2,f1,f2,e,error,root;
int i=0;
printf("Enter the initial value x1:");
scanf("%f",&x1);
printf("Enter the error limit e:");
scanf("%f",&e);
while(1)
{
i++;//i=i+1;
f1=f(x1);
f2=fdx(x1);
x2=x1-(f1/f2);
error=fabs((x2-x1)/x2);
if(error<e)
{
root=x2;
printf("\n\n\n\nNo. of iteration:%d",i-1);
printf("\n\nFinal functional value:%f",f1);
printf("\n\nFinal root:%f",root);
break;
}
else
{
x1=x2;
printf("\n\nIteration=%d x1=%.2f x2=%.2f f1=%.2f f2=%.2f error=%.2f",i,x1,x2,f1,f2,error);
}
}
}
Output for Newton Raphson Method |
প্রোগ্রামিং-এর গল্প (A tale of Programming) : Numerical Method- Coding for False Position Method
//program for false position
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*x-4*x-10;
}
main()
{
float xl,xu,x0,fxl,fxu,fx0,e,error,root;
int i=0;
printf("Enter lower limit xl:");
scanf("%f",&xl);
printf("Enter upper limit xu:");
scanf("%f",&xu);
printf("Enter limit of error:");
scanf("%f",&e);
fxl=f(xl);
fxu=f(xu);
if(fxl*fxu>0)
{
printf("\nSorry, the limits doesn't bracket any root:");
main();
}
else// if(fxl*fxu<0)
{
printf("\n\n\nIteration xl\txu\tx0\tfxl\tfxu\tfx0\terror");
while(1)
{
i++;
x0=xl-((fxl*(xl-xu))/(fxl-fxu));
fxl=f(xl);
fxu=f(xu);
fx0=f(x0);
if(fx0*fxl<0)
{
xu=x0;
}
else// if(fx0*fxu<0)
{
xl=x0;
}
error=fabs((xl-xu)/xl);
printf("\n%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f",i,xl,xu,x0,fxl,fxu,fx0,error);
if(error<=e)
{
x0=xl-((fxl*(xl-xu))/(fxl-fxu));
root=x0;
printf("\n\n\n\nThe desired root is= %f\n\n",root);
break;
}
}
}
}
#include<stdio.h>
#include<math.h>
float f(float x)
{
return x*x-4*x-10;
}
main()
{
float xl,xu,x0,fxl,fxu,fx0,e,error,root;
int i=0;
printf("Enter lower limit xl:");
scanf("%f",&xl);
printf("Enter upper limit xu:");
scanf("%f",&xu);
printf("Enter limit of error:");
scanf("%f",&e);
fxl=f(xl);
fxu=f(xu);
if(fxl*fxu>0)
{
printf("\nSorry, the limits doesn't bracket any root:");
main();
}
else// if(fxl*fxu<0)
{
printf("\n\n\nIteration xl\txu\tx0\tfxl\tfxu\tfx0\terror");
while(1)
{
i++;
x0=xl-((fxl*(xl-xu))/(fxl-fxu));
fxl=f(xl);
fxu=f(xu);
fx0=f(x0);
if(fx0*fxl<0)
{
xu=x0;
}
else// if(fx0*fxu<0)
{
xl=x0;
}
error=fabs((xl-xu)/xl);
printf("\n%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f",i,xl,xu,x0,fxl,fxu,fx0,error);
if(error<=e)
{
x0=xl-((fxl*(xl-xu))/(fxl-fxu));
root=x0;
printf("\n\n\n\nThe desired root is= %f\n\n",root);
break;
}
}
}
}
Output for False Position Method |
Numerical Method- 3: Coding for Bisection Method (with error)
#include <stdio.h>
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl); fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{ printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
while(1)
{
i++;
x0 =(xl + xu)/2; fx0 = fnct(x0);
if ( fxu* fx0 <= 0)
xl = x0;
else
xu = x0; error=fabs((xu - xl)/xl) ;
printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
if (fabs((xu - xl)/xl) < e)
{ root = (xu+xl)/2;
printf("Root= %6.4f\n", root);
break;
}
}
}
return 0;
}
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl); fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{ printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
while(1)
{
i++;
x0 =(xl + xu)/2; fx0 = fnct(x0);
if ( fxu* fx0 <= 0)
xl = x0;
else
xu = x0; error=fabs((xu - xl)/xl) ;
printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
if (fabs((xu - xl)/xl) < e)
{ root = (xu+xl)/2;
printf("Root= %6.4f\n", root);
break;
}
}
}
return 0;
}
Output for Bisection Method |
Numerical Method-2: Coding for Bisection Method
#include <stdio.h>
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl); fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{ printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
while(1)
{
i++;
x0 =(xl + xu)/2; fx0 = fnct(x0);
if ( fxu* fx0 <= 0)
xl = x0;
else
xu = x0; error=fabs((xu - xl)/xl) ;
printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
if (fabs((xu - xl)/xl) < e)
{ root = (xu+xl)/2;
printf("Root= %6.4f\n", root);
break;
}
}
}
return 0;
}
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl); fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{ printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
while(1)
{
i++;
x0 =(xl + xu)/2; fx0 = fnct(x0);
if ( fxu* fx0 <= 0)
xl = x0;
else
xu = x0; error=fabs((xu - xl)/xl) ;
printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
if (fabs((xu - xl)/xl) < e)
{ root = (xu+xl)/2;
printf("Root= %6.4f\n", root);
break;
}
}
}
return 0;
}
Output for Bisection Method |
: Numerical Method- 1: Coding for Bisection Method
#include <stdio.h>
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl); fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{ printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
while(1)
{
i++;
x0 =(xl + xu)/2; fx0 = fnct(x0);
if ( fxu* fx0 <= 0)
xl = x0;
else
xu = x0; error=fabs((xu - xl)/xl) ;
printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
if (fabs((xu - xl)/xl) < e)
{ root = (xu+xl)/2;
printf("Root= %6.4f\n", root);
break;
}
}
}
return 0;
}
#include <math.h>
float fnct(float x)
{
return (x*x - 4*x -10);}
int main()
{
int i=0;
float xl, xu, x0, e = 0.0001;
float fxl,fxu,fx0,error,root;
printf("\nEnter values for xl and xu:\n");
scanf("%f %f", &xl, &xu);
fxl= fnct(xl); fxu= fnct(xu);
if(fxl*fxu>0)
{ printf("Not in range\n");}
else
{ printf("Iter \t xl \t xu \t x0 \t fxo \t\t error \n\n ");
while(1)
{
i++;
x0 =(xl + xu)/2; fx0 = fnct(x0);
if ( fxu* fx0 <= 0)
xl = x0;
else
xu = x0; error=fabs((xu - xl)/xl) ;
printf("%d\t%.2f\t%.2f\t%.2f\t%f.2\t%.2f\n ",i,xl,xu,x0,fx0,error);
if (fabs((xu - xl)/xl) < e)
{ root = (xu+xl)/2;
printf("Root= %6.4f\n", root);
break;
}
}
}
return 0;
}
Output for Bisection Method |
Subscribe to:
Posts (Atom)