NM Code

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

1.

//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;
}

3.//CURVE FITTING BY LEAST SQUARE METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i;
float a,b,sx=0,sy=0,sxy=0,sxx=0;
printf("Enter the number of data you want to input");
scanf("%d",&n);
float x[n],y[n];
printf("Enter the data");
for(i=0;i<n;i++)
{
printf("Enter x[%d] y[%d]",i,i);
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<n;i++)
{
sx=sx+x[i];
sy=sy+y[i];
sxy=sxy+x[i]*y[i];
sxx=sxx+x[i]*x[i];
}
a=((sxx*sy)-(sx*sxy))/((n*sxx)-(sx*sx));
b=((-sx*sy)+(n*sxy))/((n*sxx)-(sx*sx));
printf("The value of a is %f and b is %f",a,b);
return 0;
}

4.//EULER'S METHOD FOR THE SOLUTION OF 1ST ORDER ODE


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, float y) {
return (x+y);
}
main () {
int n,i;
printf("enter n:");
scanf("%d",&n);
float x[n],y[n],h;
printf ("enter x[n:]");
scanf("%f",&x[n]);
printf("enter y[0],x[0]:");
scanf("%f%f",&y[0],&x[0]);
h=(x[n]-x[0])/n;
printf("\nx\t\ty\n");
for (i=0;i<=n;i++) {
printf("\n%f\t%f",x[i],y[i]);
y[i+1]=y[i]+h*f(x[i],y[i]);
x[i+1]=x[i]+h;
}
return 0;
}

4.//RK2 METHOD FOR THE SOLUTION OF 1ST ORDER ODE


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, float y) {
return (x+y);
}
main () {
int n,i;
printf("enter n:");
scanf("%d",&n);
float x[n],y[n],h,k,k1,k2;
printf ("enter x[n:]");
scanf("%f",&x[n]);
printf("enter y[0],x[0]:");
scanf("%f%f",&y[0],&x[0]);
h=(x[n]-x[0])/n;
printf("\nx\t\ty\n");
for (i=0;i<=n;i++) {
printf("\n%f\t%f",x[i],y[i]);
k1=h*f(x[i],y[i]);
k2=h*f(x[i]+h,y[i]+k1);
k=(k1+k2)/2;
y[i+1]=y[i]+k;
x[i+1]=x[i]+h;
}
return 0;
}

4.//RK4 METHOD FOR THE SOLUTION OF 1ST ORDER ODE


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, float y) {
return (x+y);
}
main () {
int n,i;
printf("enter n:");
scanf("%d",&n);
float x[n],y[n],h,k,k1,k2,k3,k4;
printf ("enter x[n:]");
scanf("%f",&x[n]);
printf("enter y[0],x[0]:");
scanf("%f%f",&y[0],&x[0]);
h=(x[n]-x[0])/n;
printf("\nx\t\ty\n");
for (i=0;i<=n;i++) {
printf("\n%f\t%f",x[i],y[i]);
k1=h*f(x[i],y[i]);
k2=h*f(x[i]+h/2,y[i]+k1/2);
k3=h*f(x[i]+h/2,y[i]+k2/2);
k4=h*f(x[i]+h,y[i]+k3);
k=(k1+2*k2+2*k3+k4)/6;
y[i+1]=y[i]+k;
x[i+1]=x[i]+h;
}
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;
}

6.//GAUSS JORDAN METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,k,n;
printf("Enter the number of unknown variables\n");
scanf("%d",&n);
float a[n][n+1],temp,x[n];
printf("Enter the augmented matrix element\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("Enter a[%d][%d] element\n",i,j);
scanf("%f",&a[i][j]);
}
}
for(j=1;j<=n;j++)
{
if((fabs(a[j][j]))<=0.0005)
{
printf("Pivot element 0!!! Process terminated!!");
return 1;
}
for(i=1;i<=n;i++)
{
if(i!=j)
{
temp=a[i][j]/a[j][j];
for(k=1;k<=n+1;k++)
{
a[i][k]=a[i][k]-temp*a[j][k];
}
}
}
}
printf("The required values are\n");
for(i=1;i<=n;i++)
{
x[i]=a[i][n+1]/a[i][i];
printf("%f\n",x[i]);
}
}

7.//NEWTON RAPHSON METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (x*x*x+5*x+6);
}
float df(float x)
{
return (3*x*x+5);
}
int main()
{
int count=0;
float x0,x1,root;
printf("Enter initial value\n");
scanf("%f",&x0);
do
{
if(fabs(df(x0))<=0.0005)
{
printf("ERROR!!\n");
return 1;
}
x1=x0-f(x0)/df(x0);
x0=x1;
count++;
if(count==100)
{
printf("Max iteration reached!!\n");
return 1;
}
}while(fabs(f(x0))>=0.0005);
root=x0;
printf("The root is %f",root);
}

8.//INVERSE OF A MATRIX USING GAUSS-JORDAN METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i,j,k;
printf("Enter the number of rows of the square matrix: \t");
scanf("%d",&n);
float a[n][2*n],temp,d;
printf("Enter the matrix element\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("Enter a[%d][%d]: \t",i,j);
scanf("%f",&a[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(j=n+1;j<=(2*n);j++)
{
if(j==(i+3))
{
a[i][j]=1;
}
else
{
a[i][j]=0;
}
}
}
printf("The required augmented matrix is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=(2*n);j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
for(j=1;j<=n;j++)
{
if(fabs(a[j][j])<=0.0005)
{
printf("Pivot element 0!!!");
return 1;
}
for(i=1;i<=n;i++)
{
temp=a[i][j]/a[j][j];
if(i!=j)
{
for(k=1;k<=(2*n);k++)
{
a[i][k]=a[i][k]-temp*a[j][k];
}
}
}
}
printf("The required inverse matrix is: \n");
for(i=1;i<=n;i++)
{
d=a[i][i];
for(j=n+1;j<=(2*n);j++)
{
a[i][j]=a[i][j]/d;
printf("%f\t",a[i][j]);
}
printf("\n");
}
}

9.//GAUSS ELIMINATION METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i,j,k;
printf("Enter the number of unknown variables:\t");
scanf("%d",&n);
float a[n][n+1],temp,x[n],sum;
printf("Enter the matrix elements\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("Enter a[%d][%d]:\t",i,j);
scanf("%f",&a[i][j]);
}
}
for(j=1;j<n;j++)
{
if(fabs(a[j][j])<=0.0005)
{
printf("Pivot element 0!!!!");
return 1;
}
for(i=1;i<=n;i++)
{
if(i>j)
{
temp=a[i][j]/a[j][j];
for(k=1;k<=n+1;k++)
{
a[i][k]=a[i][k]-temp*a[j][k];
}
}
}
}
x[n]=a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
sum=0;
for(j=i+1;j<=n;j++)
{
sum=sum+a[i][j]*x[j];
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
printf("The required values are: \n");
for(i=1;i<=n;i++)
{
printf("%f\n",x[i]);
}
return 0;
}

10.//LEAST SQUARE METHOD OF CURVE FITTING FOR POLYNOMIAL EQUATION


#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int d,n,i,j,k;
printf("Enter the degree of the polynomial: \t");
scanf("%d",&d);
printf("Enter the number of data pairs: \t");
scanf("%d",&n);
float x[50],y[50],a[30][30],temp,c[50];
if(n>d)
{
printf("Enter the data: \n");
for(i=0;i<n;i++)
{
printf("Enter x[%d] y[%d]\n",i,i);
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<=d;i++)
{
for(j=0;j<=d;j++)
{
a[i][j]=0;
for(k=0;k<n;k++)
{
a[i][j]=a[i][j]+pow(x[k],i+j);
}
}
}
for(i=0;i<=d;i++)
{
a[i][d+1]=0;
for(k=0;k<n;k++)
{
a[i][d+1]=a[i][d+1]+pow(x[k],i)*y[k];
}
}
printf("The augmented matrix is: \n");
for(i=0;i<=d;i++)
{
for(j=0;j<=d+1;j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
for(j=0;j<=d;j++)
{
if(fabs(a[j][j])<=0.0005)
{
printf("Pivot element 0!!!Process terminated!!");
return 1;
}
for(i=0;i<=d;i++)
{
if(i!=j)
{
temp=a[i][j]/a[j][j];
for(k=0;k<=d+1;k++)
{
a[i][k]=a[i][k]-temp*a[j][k];
}
}
}
}
printf("The required values are\n");
for(i=0;i<=d;i++)
{
c[i]=a[i][d+1]/a[i][i];
printf("%f\n",c[i]);
}
}
else
{
printf("n<=d Process Terminated!!!");
}
return 0;
}

11.//DOMINANT EIGEN VALUE AND EIGEN VECTOR USING POWER METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 10
int main()
{
int n,i,j;
double a[N][N],x[N],y[N],d[N],z[N],l,error;
do
{
printf("Enter the row of the square matrix:\t");
scanf("%d",&n);
}while(n>N||n<=0);
printf("Enter the matrix element:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%lf",&a[i][j]);
}
}
printf("Enter the initial guess vector:\n");
for(i=0;i<n;i++)
{
scanf("%lf",&x[i]);
}
do
{
for(i=0;i<n;i++)
{
z[i]=0;
for(j=0;j<n;j++)
{
z[i]=z[i]+a[i][j]*x[j];
}
}
l=z[0];
for(i=1;i<n;i++)
{
if(fabs(z[i])>fabs(l))
{
l=z[i];
}
}
for(i=0;i<n;i++)
{
y[i]=z[i]/l;
d[i]=fabs(y[i]-x[i]);
x[i]=y[i];
}
error=d[0];
for(i=1;i<n;i++)
{
if(fabs(d[i])>error)
{
error=d[i];
}
}
}while(error>=0.0005);
printf("The eigen vector is:\n");
for(i=0;i<n;i++)
{
printf("%f\n",y[i]);
}
printf("The eigen value is %f",l);
}

12.//SOLUTION OF 2ND DEGREE ODE USING RK4 METHOD


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x, float y, float z)
{
return z;
}
float g(float x, float y, float z)
{
return (x*sin(y)+cos(z));
}
int main()
{
float x0,y0,z0,x,y,z,k1,k2,k3,k4,k,l1,l2,l3,l4,l,xn,h;
printf("Enter the initial value of x,y,z\n");
scanf("%f%f%f",&x0,&y0,&z0);
printf("Enter the step size :\t");
scanf("%f",&h);
printf("Enter the final value\n");
scanf("%f",&xn);
printf("The values in successive iterations are\n");
do
{
k1=h*f(x0,y0,z0);
l1=h*g(x0,y0,z0);

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;
}

////SIMPSON'S 1/3 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,s2=0,sum;
printf("Enter the initial value of x:\t");
scanf("%f",&x0);
printf("\nEnter the final value of x:\t");
scanf("%f",&xn);
do
{
printf("\nEnter the number of interval:\t");
scanf("%d",&n);
}while(n%2!=0||x0>xn);
h=(xn-x0)/n;
s=f(x0)+f(xn);
for(i=1;i<n;i++)
{
if(i%2==0)
{
s1=s1+2*f(x0+i*h);
}
else
{
s2=s2+4*f(x0+i*h);
}
}
sum=h/3*(s+s1+s2);
printf("The required value is %f",sum);
return 0;
}

////SIMPSON'S 3/8 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,s2=0,sum;
printf("Enter the initial value of x:\t");
scanf("%f",&x0);
printf("\nEnter the final value of x:\t");
scanf("%f",&xn);
do
{
printf("\nEnter the number of interval:\t");
scanf("%d",&n);
}while(n%3!=0||x0>xn);
h=(xn-x0)/n;
s=f(x0)+f(xn);
for(i=1;i<n;i++)
{
if(i%3==0)
{
s1=s1+2*f(x0+i*h);
}
else
{
s2=s2+3*f(x0+i*h);
}
}
sum=3*h/8*(s+s1+s2);
printf("The required value is %f",sum);
return 0;
}

You might also like