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

Leave a Reply

Your email address will not be published. Required fields are marked *