NM Code
NM Code
NM Code
//BISECTION METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
float y;
y=(1/sin(x)+pow(x,5));
return y;
}
int main()
{
int count=0;
float a,b,c;
printf("Enter the range (a,b) ");
scanf("%f%f",&a,&b);
printf("Your entered range is %f,%f",a,b);
if(f(a)*f(b)>0)
{
printf("Error!!!");
return 0;
}
do
{
c=((a+b)/2);
if(f(a)*f(c)<0)
{
b=c;
}
else{
a=c;
}
count=count+1;
if(count>100)
{
printf("Function discontinuous!!!");
return 0;
}
}while (fabs(f(c))>=0.0005);
printf("Your root is %f ",c);
}
2.//SECANT METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
float y;
y=(x*sin(x)+cos(x));
return y;
}
int main()
{
int count=0;
float a,b,c;
printf("Enter the value of a and b ");
scanf("%f%f",&a,&b);
do
{
if(f(a)==f(b))
{
printf("ERROR!!!");
return 1;
}
else
{
c=(((a*f(b))-b*f(a))/(f(b)-f(a)));
a=b;
b=c;
}
count++;
if(count>100)
{
printf("Function discontinuous!!!");
return 1;
}
}while(fabs(f(c)>=0.0005));
printf("The root of the equation is %f",c);
return 0;
}
5.//LAGRANGE'S INTERPOLATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int count=0,n,i,j;
float p,s=0,xp;
printf("Enter the number of data you want to input ");
scanf("%d",&n);
float x[n],y[n];
printf("Enter your data ");
for(i=0;i<n;i++)
{
printf("Enter x[%d] y[%d] ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
printf("Enter the interpolating data ");
scanf("%f",&xp);
for(i=0;i<n;i++)
{
p=1;
for(j=0;j<n;j++)
{
if(i!=j)
{
p=(p*((xp-x[j])/(x[i]-x[j])));
}
}
s=s+(p*y[i]);
}
printf("The required answer is %f ",s);
return 0;
}
k2=h*f(x0+h/2,y0+k1/2,z0+l1/2);
l2=h*g(x0+h/2,y0+k1/2,z0+l1/2);
k3=h*f(x0+h/2,y0+k2/2,z0+l2/2);
l3=h*g(x0+h/2,y0+k2/2,z0+l2/2);
k4=h*f(x0+h,y0+k3,z0+l3);
l4=h*g(x0+h,y0+k3,z0+l3);
k=(k1+2*k2+2*k3+k4)/6;
l=(l1+2*l2+2*l3+l4)/6;
y=y0+k;
z=z0+l;
x=x0+h;
x0=x;
y0=y;
z0=z;
printf("%f\n%f\n%f\n",x0,y0,z0);
printf("\n\n");
}while(x0<xn);
printf("The final value is : %f\t%f\t%f",x0,y0,z0);
return 0;
}
13.//NUMERICAL INTEGRATION
/////TRAPEZOIDAL RULE
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (1/(1+pow(x,2)));
}
int main()
{
int i,n;
float x0,xn,h,s=0,s1=0,sum;
printf("Enter the initial value of x:\t");
scanf("%f",&x0);
printf("\nEnter the final value of x:\t");
scanf("%f",&xn);
printf("\nEnter the number of interval:\t");
scanf("%d",&n);
h=(xn-x0)/n;
s=f(x0)+f(xn);
for(i=1;i<n;i++)
{
s1=s1+2*f(x0+i*h);
}
sum=h/2*(s+s1);
printf("The required value is %f",sum);
return 0;
OR
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 10
int main()
{
int i,n;
float x0,xn,h,s=1,s1=0,sum,y[N];
printf("Enter lower limit:\t");
scanf("%f",&x0);
printf("\nEnter upper limit:\t");
scanf("%f",&xn);
do
{
printf("Enter the number of interval:\t");
scanf("%d",&n);
}while(n>N||x0>xn);
h=(xn-x0)/n;
printf("\nEnter the values of:\n");
for(i=0;i<=n;i++)
{
printf("Enter y[%d]:\t",i);
scanf("%f",&y[i]);
}
s=y[0]+y[n];
for(i=1;i<n;i++)
{
s1=s1+2*y[i];
}
sum=h/2*(s+s1);
printf("\nThe required value is %f",sum);
return 0;
}