Power Method & Runge Kutta Method

Power Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define er 0.001
void main()
{
	int i,j,n;
	float a[10][10],y[10],b[10],max,max1=0,e;
	clrscr();
	printf("enter the order of matrix :");
	scanf("%d",&n);
	printf("enter the matrix: n");
	for(i=0;i<n ;i++)
		for(j=0;j<n;j++)
			scanf("%f",&a[i][j]);
	for(i=0;i<n;i++)
		b[i]=1;
	do
	{
		for(i=0;i<n;i++)
		{
			y[i]=0;
			for(j=0;j<n;j++)
				y[i]+=a[i][j]*b[j];
		}
		max=0;
		for(i=0;i<n;i++)
		{
			if(fabs(y[i])>max)
				max=y[i];
		}
		for(i=0;i</n><n ;i++)
		b[i]=y[i]/max;
		e=fabs(max-max1);
		max1=max;
	} while(e>er);
	printf(" nEigen value =  %f", max);
	printf("Eigen vector = n");
	for(i=0;i</n><n ;i++)
		printf("n%f",b[i]);
	getch();
}

Runge Kutta Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x,y) pow(x,2)+pow(y,2)
void main()
{
	float i,x0,y0,h,x,m1,m2,m3,m4;
	clrscr();
	printf("Enter x0, y0,a & h: ");
	scanf("%f %f %f %f",&x0,&y0,&x,&h);
	for(i=0;i< (x-x0)/h;i++)
	{
		m1=f(x0+i*h,y0);
		m2=f(x0+i*h+h/2, y0+ m1*h/2);
		m3=f(x0+i*h+h/2, y0 +m2*h/2);
		m4=f(x0+i*h+h, y0+m3*h);
		y0+=(m1+2*m2+2*m3+m4)*h/6;
		printf("n y(%f) = %f",x0+i*h,y0);
	}
	getch();
}

Linear Algebraic Equation(Gauss Elimination & Gauss Jordan)

Gauss Elimination

#include<stdio .h>
#include<conio .h>

void main()
{
	clrscr();
	int n,i,j,k;
	float a[10][10],b[10],x[10],p,f,sum;
	printf("Enter the no. of equations");
	scanf("%d",&n);
	for(i=0;i<n ;i++)
	{
		printf("Enter the coefficients of equation %d:n",i+1);
		for(j=0;j<n;j++)
			scanf("%f",&a[i][j]);
		printf("Enter the constant");
		scanf("%f",&b[i]);
	}
	for(i=0;i<n;i++)
	{
		p=a[i][i];
		for(j=i+1;j<n;j++)
		{
			f=a[j][i]/p;
			for(k=i;k<n;k++)
				a[j][k]-=f*a[i][k];
			b[j]-=f*b[i];
		}
	}
	for(i=n-1;i>=0;i--)
	{
		sum=0;
		for(j=i+1;j</n><n ;j++)
			sum+=a[i][j]*x[j];
		x[i]=(b[i]-sum)/a[i][i];
		printf("x[%d]=%fn",i,x[i]);
	}
	getch();
}

Gauss Jordan

#include<stdio .h>
#include<conio .h>

void main()
{
	clrscr();
	int n,i,j,k;
	float a[10][10],b[10],x[10],p,f,sum;
	printf("Enter the no. of equations");
	scanf("%d",&n);
	for(i=0;i<n ;i++)
	{
		printf("Enter the coefficients of equation %d:n",i+1);
		for(j=0;j<=n;j++)
			scanf("%f",&a[i][j]);
	}
	for(i=0;i<n;i++)
	{
		p=a[i][i];
		for(k=0;k<=n;k++)
			a[i][k]/=p;
		for(j=0;j<n;j++)
		{
			if(j!=i)
			{
				f=a[j][i]/a[i][i];
				for(k=0;k<=n;k++)
					a[j][k]-=f*a[i][k];
			}
		}
		printf("x[%d]=%fn",i,a[i][n]);
	}
	getch();
}

Numerical Integration

Trapezoidal

#include<stdio .h>
#include<conio .h>
#include<math .h>
#define f(x) pow(x,3)+1

void main()
{
	int j;
	float n,a,b,h,i,i1,i2=0;
	clrscr();
	printf("Enter a and b::");
	scanf("%f%f",&a,&b);
	printf("enter the value of n");
	scanf("%f",&n);
	h=(b-a)/n;
	printf("%f",h);
	i1=f(a)+f(b);
	for(j=1; j<n ; j++)
	{
		i2+=f(a+j*h);
	}
	i=(i1+2*i2)*h/2;
	printf("nnI=%f",i);
	getch();
}

Simson’s 1 by 3

#include<stdio .h>
#include<conio .h>
#include<math .h>
#define f(x) pow(x,3)+1

void main()
{
	int j;
	float a,b,n,h,i,i1,i2=0,i3=0;
	clrscr();
	printf("Enter a and b::");
	scanf("%f%f",&a,&b);
	printf("enter the value of n");
	scanf("%f",&n);
	h=(b-a)/n;
	i1=f(a)+f(b);
	for(j=1; j<n ; j++)
	{
		if (j%2==0) i2+=f(a+j*h);
		else i3+=f(a+j*h);
	}
	i=(i1+4*i3+2*i2)*h/3;
	printf("nnI=%f",i);
	getch();
}

Simpson’s 3 by 8

#include<stdio .h>
#include<conio .h>
#include<math .h>
#define f(x) pow(x,-1)

void main()
{
	int j;
	float a,b,n,h,i,i1,i2=0,i3=0;
	clrscr();
	printf("Enter a and b::");
	scanf("%f%f",&a,&b);
	printf("enter the value of n");
	scanf("%f",&n);
	h=(b-a)/n;
	i1=f(a)+f(b);
	for(j=1; j<n ; j++)
	{
		if (j%3==0) i2+=f(a+j*h);
		else i3+=f(a+j*h);
	}
	i=(i1+3*i3+2*i2)*(3*h)/8;
	printf("nnI=%f",i);
	getch();
}

Least Square Regression

y=ax+b

#include <stdio .h>
#include <conio .h>


void main()
{
	clrscr();
	float x[10],y[10],x2=0, x1y1=0, y1=0, x1=0;
	int i,n;
	printf("Enter the number of data::");
	scanf("%d",&n);
	printf("nnEnter the data::");
	for(i=0; i<n ; i++)
	{
		printf("nnx[i]=");
		scanf("%f",&x[i]);
		printf("ty[i]=");
		scanf("%f",&y[i]);
	}
	for(i=0; i<n; i++)
	{
		x2+=(x[i]*x[i]);
		x1y1+=(x[i]*y[i]);
		y1+=y[i];
		x1+=x[i];
	}
	float b=(n*x1y1-x1*y1)/(n*x2-(x1*x1));
	float a=(y1-b*x1)/n;
	printf("nna=%fttb=%f",a,b);
	getch();
}

y=axb

#include <stdio .h>
#include <conio .h>
#include<math .h>


void main()
{
	clrscr();
	float x[10],y[10],x2=0, x1y1=0, y1=0, x1=0;
	int i,n;
	printf("Enter the number of data::");
	scanf("%d",&n);
	printf("nnEnter the data::");
	for(i=0; i<n ; i++)
	{
		printf("nnx[i]=");
		scanf("%f",&x[i]);
		x[i]=log(x[i]);
		printf("ty[i]=");
		scanf("%f",&y[i]);
		y[i]=log(y[i]);
	}
	for(i=0; i<n; i++)
	{
		x2+=(x[i]*x[i]);
		x1y1+=(x[i]*y[i]);
		y1+=y[i];
		x1+=x[i];
	}
	float b=(n*x1y1-x1*y1)/(n*x2-(x1*x1));
	float a=(y1-b*x1)/n;
	a=exp (a);
	printf("nna=%fttb=%f",a,b);
	getch();
}