Search
×

OR

Create a Shvoong account from scratch

×

OR

×

OR

Shvoong Home>Science>Mathematics>C Programming for Numerical Analysis of Some Methods Summary

# C Programming for Numerical Analysis of Some Methods

Academic Paper Summary   by:Ashraful     Original Author: Md. Ashraful Babu

1. Program in C Language for Bisection Method to find a real root of x^3-3x-5=0 in the interval (2,3) to five places of decimal.

#include<stdio.h>
#include<math.h>
float fx(float x);
void main()
{ int i=0;
float x[30],a,b;
scanf("%f%f",&a,&b);
x[0]=a;
if((fx(a)*fx(b))<0.0)
{start:
i=i+1;
x[i]=(a+b)/2.0;
if(fabs(x[i]-x[i-1])<=0.00001)
goto end;
else if((fx(x[i])*fx(b))<0.0)
{ a=x[i];
goto start;}
else
{ b=x[i];
goto start; }
end: printf("x[%d]=%.5f",i,x[i]);
}
else
}

float fx(float x)
{ float value;
value=pow(x,3)-(3.0*x)-5.0;
return(value);
}

2. Program in C Language for Regula falsi Method to find a real root of 2x-log10(x)-7=0 in the interval (3,4) to four places of decimal.

#include<stdio.h>
#include<math.h>
float fx(float x);
void main()
{ int i=0;
float x[30],a,b;
scanf("%f %f",&a,&b);

if((fx(a)*fx(b))<0)
{start:
i=i+1;
x[i]=((a*fx(b))-(b*fx(a)))/(fx(b)-fx(a));
if(fabs(x[i]-x[i-1])<=0.0001)
goto end;
else if((fx(x[i])*fx(b))<0)
{ a=x[i];
goto start;}
else
{ b=x[i];
goto start; }
end: printf("x[%d]=%.4f",i,x[i]); }
else
}

float fx(float x)
{ float value;
value=2*x-log10(x)-7;
return(value);
}
}

3. Program in C Language for Secant Method to find a real root of 2x-log10(x)-7=0 in the interval (3,4) to four places of decimal.

#include<stdio.h>
#include<math.h>
float fx(float x);
void main()
{ int i=0;
float x[30],a,b;
scanf("%f %f",&a,&b);

start:
i=i+1;
x[i]=((a*fx(b))-(b*fx(a)))/(fx(b)-fx(a));
if(fabs(x[i]-x[i-1])<=0.0001)
goto end;
else
{ a=x[i];
goto start;}

end: printf("x[%d]=%.4f",i,x[i]); }

float fx(float x)
{ float value;
value=2*x-log10(x)-7;
return(value);
}

4. Program in C Language for Fixed Point Method to find a real root of 2x-log10(x)-7=0 which is near to x=2,to four places of decimal.

#include<stdio.h>
#include<math.h>
float fx(float x);
void main()
{ int i=0;
float x[30],a;
scanf("%f",&a);

x[0]=a;
start:
i=i+1;
x[i]=fx(x[0]);
if(fabs(x[i]-x[i-1])<=0.0001)
printf("x[%d]=%.4f",i,x[i]);
else
goto start;
}
float fx(float x)
{ float value;
value=(log10(x)+7.0)/2.0;
return(value);
}

5. Program for Newton-Raphson Method to find a real root of x^4-x-10=0 which is near to x=2, to four places of decimal.

#include<stdio.h>
#include<math.h>
float fx(float x);
float gx(float x);

void main()
{ int i=0;
float x[30],a;
printf("Please enter the approximate nalue of 'a':");
scanf("%f",&a);
x[0]=a;

start:
i=i+1;
x[i]=x[i-1]-(fx(x[i-1])/gx(x[i-1]));
if(fabs(x[i]-x[i-1])<=0.001)
printf("The required root is: x[%d]=%f",i,x[i]);
else goto start;
}

float fx(float x)
{ float value;
value=pow(x,4)-x-10.0;
return(value);
}

float gx(float x)
{ float valu;
valu=(4*pow(x,3))-1.0;
return(valu);}

6. Program in C Language for Newton-Raphson Method for polynomial equation to find a real root to four places of decimal.

#include<stdio.h>
#include<math.h>
void main()
{ int i,j,k,n;
float x[30],a[50],b[50],c,prod,gx[30],fx[30];
printf("Please enter the highest degree (n) of the polynomial:");
scanf("%d",&n);
printf("Please enter coefficients 'a[i]' of the polynomial:");
for(i=n;i>=0;i--)
scanf("%f",&a[i]);
printf("Please enter the approximate nalue of 'x[0]':");
scanf("%f",&c);
x[0]=c;
b[n-1]=a[n];
prod=b[n-1];
k=0;

start:
k=k+1;
for(j=n;j>=2;j--)
{b[j-2]=a[j-1]+(b[j-1]*x[k-1]);
prod=b[j-2]+(prod*x[k-1]);}

gx[k-1]=prod;
fx[k-1]=a[0]+(b[0]*x[k-1]);
x[k]=x[k-1]-(fx[k-1]/gx[k-1]);
if(fabs(x[k]-x[k-1])<=0.0001)
printf("The required root is: x[%d]=%.5f",k,x[k]);
else
goto start;
}

7. Program for Newton Forward Interpolation.

#include<stdio.h>
void main()
{ int i,j,n,k;
float f[30],x[30],Df[30][30],xp,h,p,prod,sum;
printf("Please enter the number of input value n:");
scanf("%d",&n);

n=n-1;
printf("Please enter the value of x[i]:");
for(i=0;i<=n;i++)
scanf("%f",&x[i]);
printf("Please enter the value of f[x]:");
for(i=0;i<=n;i++)
{ scanf("%f",&f[i]);
Df[0][i]=f[i];
}

for(j=1;j<=n;j++)
{ for(i=0;i<=n-j;i++)
Df[j][i]=Df[j-1][i+1]-Df[j-1][i];
}

printf("\t\t\tDifference Table:\n\n");
for(k=0;k<=n;k++)
printf("Df[%d][i]\t",k);
printf("\n_________________________\n\n");
for(j=0;j<=n-1;j++)
{ for(i=0;i<=n-j;i++)
printf("%f\t",Df[i][j]);
printf("\n");
}

printf("\n Please enter the target value xp:");
scanf("%f",&xp);
h=x[1]-x[0];
p=(xp-x[0])/h;
prod=1.0;
sum=f[0];

for(i=1;i<=n;i++)
{j=i-1;
prod=prod*(p-j)/(float)i;
sum=sum+(prod*Df[i][0]);
}
printf("\n The required value = %f",sum); }

8. Program for Newton Backward Interpolation.

#include<stdio.h>
void main()
{ int i,j,n,k;
float f[30],x[30],Df[30][30],xp,h,p,prod,sum;
printf("Please enter the number of input value n:");
scanf("%d",&n);

n=n-1;
printf("Please enter the value of x[i]:");
for(i=0;i<=n;i++)
scanf("%f",&x[i]);
printf("Please enter the value of f[x]:");
for(i=0;i<=n;i++)
{ scanf("%f",&f[i]);
Df[0][i]=f[i];
}

for(j=1;j<=n;j++)
{ for(i=j;i<=n;i++)
Df[j][i]=Df[j-1][i]-Df[j-1][i-1];
}

printf("\t\t\tDifference Table:\n\n");
for(k=0;k<=n;k++)
printf("Df[%d][i]\t",k);
printf("\n________________________\n\n");
for(j=0;j<=n;j++)
{ for(i=0;i<=j;i++)
printf("%f\t",Df[i][j]);
printf("\n");
}

printf("\n Please enter the target value xp:");
scanf("%f",&xp);
h=x[n]-x[n-1];
p=(xp-x[n])/h;
prod=1.0;
sum=f[n];

for(i=1;i<=n;i++)
{j=i-1;
prod=prod*(p+j)/(float)i;
sum=sum+(prod*Df[i][n]);
}
printf("\n The required value = %f",sum);
}

Published: May 29, 2010
 Please Rate this Summary : 1 2 3 4 5
1. Answer   Question  :    pls post c coding for ADI for laplace equation in a unitb square domain with boundary conditions as 1 at top nd 0 on remaining sides.
1. Answer   Question  :    write a program to approximate the real root to 6 decimal places , using Newtons method of the function f(x)=1,000,000x^2-111,000x-1 for -2<=x<=2
1. Answer   Question  :    newton's backward interpolation polynomial using c programming
1. Answer   Question  :    Program in C Language for Bisection Method to find a real root of x^3-x+1
1. Answer   Question  :    how to write a program to multiply two floating points without * operator?
1. Answer   Question  :    how to write a program in c to find the roots of the quadratic eqations.
1. Answer   Question  :    please, help me Obtain the local minimize of the function f(x) = X5-5X3-20X+5 on [-3,O] and [0,3] by use newton method and bisection method in C Language.
1. Answer  :    ans Saturday, May 11, 2013
1. Answer   Question  :    C program for 2 dimensional heat equation by ADE method
1. Answer   Question  :    proamme to find the realroot for the given equation inbijection method
Tags:
 Use our Content Translate Send Link Print Share

#### More Summaries & Reviews by Ashraful

More
1. 1. yyyyy

331

write a program to approximate the real root to 6 decimal places , using Newtons method of the function f(x)=1,000,000x^2-111,000x-1 for -2<=x<=2

0 Rating Wednesday, April 17, 2013
X

.

•