Sie sind auf Seite 1von 18

EIGEN VALUE

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[5][5],x[5],y[5],temp;
int n,i,j,l;
int count,no;
clrscr();
cout<<"Enter the order of matrix:";
cin>>n;
cout<<"\nThe element of matrix:";
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
cout<<"\nThe initialised of matrix x:";
for(i=1;i<=n;i++)
cin>>x[i];
for(l=1;l<=10;l++)
{
for(i=1;i<=n;i++)
{
y[i]=0;
for(j=1;j<=n;j++)
y[i]+=a[i][j]*x[j];
}
temp=(y[1]);
for(i=2;i<=n;i++)
{
if(fabs(y[i])>fabs(temp))
{
temp=y[i];
}
}
for(i=1;i<=n;i++)
{
x[i]=y[i]/temp;
}
}
cout<<"\nThe eigen value: "<<temp;
cout<<"\n\nThe eigen vector: ";
for(i=1;i<=n;i++)
{
cout<<endl;
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

cout<<x[i];
}
getch();
}

GAUSS ELIMINATION
#include<stdio.h>
#include<conio.h>
# define S 10
//----------matrix display function---------void dispmatrix(float A[S][S],int n)
{
int p,q;
for(p=1;p<=n;p++)
{
for(q=1;q<=n+1;q++)
{
if(q==n+1)
printf(": %7.3f ",A[p][q]);
else
printf(" %7.3f ",A[p][q]);
}
printf("\n");
}
}
//------------------------------------------void main()
{
int n,i,j,k;
float A[S][S],mul[S],X[S],temp;
clrscr();
printf("Enter no of variables in eqn in the form a1x1+a2x2+...+anxn=b1:");
scanf("%d",&n);
//-----------reading of coefts of equations-----------for(i=1;i<=n;i++)
{
printf("\nEnter coefts of eqn %d :",i);
for(j=1;j<=n+1;j++)
{
scanf("%f",&A[i][j]);
}
}
//-------------------------------------------------------//---------------display of augmented matrix-----------Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

printf("\nThe augmented matrix is:\n\n");


dispmatrix(A,n);
//----------------------------------------------------------//--------------converting in to upper triangular matrix-----for(k=1;k<=n-1;k++) //for1
{
for(i=k+1;i<=n;i++) //for2
{
mul[i]=A[i][k]/A[k][k]; //as first m should be A21/A11,second A31/A11..
for(j=k;j<=n+1;j++) //for3
{
A[i][j]=A[i][j]-mul[i]*A[k][j];
}
//end of for3
}
//end of for2
//---------display intermediate matrices-----printf("\nAugmented matrix after operation:\n");
dispmatrix(A,n);
printf("\n\n");
//------------------------------------------} //end of for3
//--------------------------------------------//----Back wards substitution-------------------X[n]=A[n][n+1]/A[n][n];
for(j=n-1;j>=1;j--)
{
X[j]=A[j][n+1];
for(k=j+1;k<=n;k++)
{
X[j]=X[j]-X[k]*A[j][k];
}
X[j]=X[j]/A[j][j];
}
//-------------------------------------------------//-----------display of solution----------------printf("\nThe solution set is:\n");
for(i=1;i<=n;i++)
{
printf("\nX%d=%.3f",i,X[i]);
}
getch();
}
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

HORNERS METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define S 10
#define E 0.0001
void main()
{
float A[S]={0},B[S]={0},C[S]={0},x0,e,r;
int ind=1,i,j,n;
clrscr();
printf("\n\"The output is here\"\n");
printf("Enter the degree of polynomial eqn:");
scanf("%d",&n);
printf("\nEnter coefts of eqn (in the form a0x^n+a1x^n-1+a2x^n-2+...+an-1x+an):");
for(i=0;i<=n;i++)
{
scanf("%f",&A[i]);
}
printf("\nEnter initial value:");
scanf("%f",&x0);
B[0]=A[0];
C[0]=A[0];
do
{
do
{
for(j=1;j<=n;j++)
B[j]=A[j]+B[j-1]*x0;
for(j=1;j<=n;j++)
C[j]=B[j]+B[j-1]*x0;
e=fabs(B[n]);
r=x0;
x0=(x0-(B[n]/C[n-1]));
}
while(e>=0.0001);
printf("\nRoot%d is=%.2f",ind,r);
for(i=1;i<=n;i++)
A[i]=B[i];
n=n-1;
ind++;
}
while(n>=1);
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

getch();
}

BISECTION METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.0001
float fun(float a)
{
float f;
//f=(exp(a)-4*sin(a));
f=(a*a*a-4*a+1);
return(f);
}
void main()
{
float x1,x2,x0,f1,f2,f0,e,root;
int i=1;
clrscr();
printf("Enter initial values x1 and x2:");
scanf("%f%f",&x1,&x2);
e=(fabs(x2-x1));
f1=fun(x1);
f2=fun(x2);
if((f1*f2)>0)
{
printf("initial values are not valid, press any key to exit.");
getch();
exit(0);
}
printf("itern x1 x2 f(x1) f(x2) xm f(xm) error\n\n");
do
{
x0=(x1+x2)/2;
f0=fun(x0);
printf("%3d: %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f",i,x1,x2,f1,f2,x0,f0,e);
if(f1*f0<0) x2=x0;
else x1=x0;
e=fabs(x2-x1);
f1=fun(x1);
f2=fun(x2);
i++;
printf("\n\n");
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

}
while(e>=E);
printf("\nThe number of iteration=%d",i-1);
root=x0;
printf("\n\nthe root of polynomial is:%.4f",root);
getch();
}
EIGEN VALUE
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define SZ 10
#define error 0.1
void readmat(float mat[SZ][SZ],int m,int n)
{
int i,j;
for(i=1;i<=m;i++)
{
printf("\nEnter elements of %dth row:",i);
for(j=1;j<=n;j++)
{
scanf("%f",&mat[i][j]);
}
}
}
void readm(float CM[SZ],int m)
{
int i;
for(i=1;i<=m;i++)
{
printf("\nEnter element of %dth row :",i);
scanf("%f",&CM[i]);
}
}

void dispm(float CM[SZ],int m)


{
int i;
for(i=1;i<=m;i++)
{
printf(" %5.3f \n",CM[i]);
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

}
}

void dispmat(float mat[SZ][SZ],int m,int n)


{
int i,j;
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf(" %5.3f ",mat[i][j]);
}
printf("\n");
}
}
void mul(float A[SZ][SZ],float B[SZ],float C[SZ],int m,int n)
{
float temp;
int i,j,k;
for(i=1;i<=m;i++)
{
C[i]=0;
for(k=1;k<=n;k++)
{
C[i]=C[i]+A[i][k]*B[k];
}
}
}

void main()
{
int m,n,i,j;
float A[SZ][SZ],X[SZ],X1[SZ],Y[SZ],e,e1,lar;//mat A for given matrix ,X for initial
matrix
clrscr();
printf("\nEnter the order of matrix A m n:");
scanf("%d%d",&m,&n);
if(m!=n)
{
printf("You should enter square matrix.");
getch();
exit(0);
}
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

readmat(A,m,n);
printf("\nThe matrix A is:\n");
dispmat(A,m,n);
printf("\nReading for initial column matrix:");
readm(X,m);
printf("\nThe initial matrix is:\n");
dispm(X,m);
//-----calculation----//lar=X[1];
e=error;
do
{
mul(A,X,Y,m,n);
printf("After multiplication,y is:\n");
dispm(Y,m);
lar=Y[1];
printf("\nlargest is %f",lar);
for(i=2;i<=m;i++)
{
if(fabs(Y[i])>fabs(lar))
lar=Y[i];
}
for(i=1;i<=m;i++)
{
X1[i]=Y[i]/lar;
}
for(i=1;i<=m;i++)
{
e1=fabs(X[i]-X1[i]);
if(e1>e)
e=e1;
}
for(i=1;i<=m;i++)
{
X[i]=X1[i];
}
printf("\nThe matrix X after new itteration is:\n ");
dispm(X,m);
dispm(X1,m);
}
while(e>error);
printf("\nThe eigen value is=%f",lar);
printf("\nThe eigen vector is:\n");
dispm(X,m);
getch();
}
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

/*
void main()
{
int m=3,n=3;
float A[SZ][SZ],B[SZ],C[SZ];
clrscr();
readmat(A,m,n);
printf("\nThe matrix A is:\n");
dispmat(A,m,n);
readm(B,m);
printf("\nThe matrix B is:\n");
dispm(B,m);
mul(A,B,C,m,n);
printf("\nThe matrix C is:\n");
dispm(C,m);
getch();
}
*/
SIMPSON 1/3
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (1-exp(-x/2))
void main()
{
float a,b,h,x,sum=0.0;
int n,i;
clrscr();
cout<<"\tComposite Simpson's 1/3 Rule\n\n";
cout<<"\nEnter the initial point(a):";
cin>>a;
cout<<"\nEnter the final point(b):";
cin>>b;
cout<<"\nEnter the no of intervals(n):";
cin>>n;
h=(b-a)/n;
for(i=1;i<n-1;i++)
{
x=a+h*i;
if((i%2)!=0)
sum+=(4*f(x));
else
sum+=2*f(x);
}
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

sum+=f(a)+f(b);
sum=sum*h/3;
cout<<"\n\nIntergral value from "<<a<<" to "<<b<<" for f(x)=1-exp(-x/2) is: "<<sum;
getch();
}
POWER METHOD
//-------------POWER METHOD----------------#include<stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
void display(float x[][4], float y[], float z[], int n);
void multiply(float x[][4], float y[], float z[], int n);
int main()
{
float x[4][4];
float y[4]={1,1,1,1};
float z[4]={1,1,1,1};
float temp[4]={0,0,0,0};
float max;
int n;
int ok;
float pivot, factor;
int i, j, k;
clrscr();
do
{
cout<<"Size of square matrix (MAX 4) =? "; cin>>n;
}while(n<1 || n>4);
cout<<"Enter data below rowwise"<<endl;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
cout<<"x["<<i+1<<"]["<<j+1<<"] = ";
cin>>x[i][j];
}
while(1)
{
display(x, z,y, n);
multiply(x, y, z, n);
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

max = z[0];
for(i=1; i<n; i++)
if(max<z[i])
max = z[i];
for(i=0; i<n; i++)
y[i] = z[i]/max;
for(i=0; i<n; i++)
{
if(fabs(temp[i]-y[i]) <= 0.00001)
ok = 1;
else
{
ok = 0;
break;
}
}
if(ok)
{
cout<<"Final result:"<<endl;
display(x, z,y, n);
getch();
return 0;
}
for(i=0; i<n; i++)
temp[i] = y[i];
cout<<endl;
if(getch() == 27)
break;
}
display(x, z,y, n);
return 0;
}
void multiply(float x[][4], float y[], float z[], int n)
{
for(int i=0; i<n; i++)
{
z[i] = 0;
for(int j=0; j<n; j++)
{
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

z[i] += x[i][j]*y[j];
}
}
}
void display(float x[][4], float y[], float z[], int n)
{
cout.setf(ios::showpos);
cout.setf(ios::fixed);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout.precision(3);
cout.width(5);
cout<<x[i][j]<<" ";
}
cout.width(5);
cout<<" --- ";
cout.width(5);
cout<<y[i]<<" ";
cout.width(5);
cout<<" --- ";
cout.width(5);
cout<<z[i]<<" ";
cout<<endl;
}
}
GAUSS ELIMINATION WITH PIVOTING
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define S 10
void dispmatrix(float A[S][S],int n)
{
int p,q;
for(p=1;p<=n;p++)
{
for(q=1;q<=n+1;q++)
{
if(q==n+1)
cout<<": %7.3f "<<A[p][q];
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

else
cout<<" %7.3f "<<A[p][q];
}
cout<<endl<<endl;
}
}

void main()
{
float x[10][10];
float y[10],X[10];
float result[10],mul[10];
float temp,temp1,temp2,temp3;
int n;
float pivot, factor;
int i, j, k;
clrscr();
do
{
cout<<"No. of unknowns(MAX 4) =? ";
cin>>n;
}while(n<1 || n>4);
cout<<"Enter data below rowwise"<<endl;
cout<<"Format: x11, x12, x13, a"<<endl;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cout<<"x["<<i+1<<"]["<<j+1<<"] = ";
cin>>x[i][j];
}
cout<<"y["<<i+1<<"] = ";
cin>>y[i];
}
cout<<"\nThe augmented matrix is:\n\n";
dispmatrix(x,n);
//--------------end of display------------------------------------//--------------converting in to upper triangular matrix-----for(k=1;k<=n-1;k++) //for1
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

{
//-----------------pivoting--------------------------// printf("\nAugmented matrix before pevoting:\n");
// dispmatrix(A,n);
temp1=fabs(x[k][k]);
temp2=k;
temp3=k;
for(i=k+1;i<=n;i++)
{
if(x[i][k]>temp1)
{
temp1=fabs(x[i][k]);
temp3=i;
}
}
if(temp2!=temp3)
{
for(i=1;i<n+1;i++)
{
temp1=x[temp2][i];
x[temp2][i]=x[temp3][i];
x[temp3][i]=temp1;
}
}
if(temp2!=temp3)
{
cout<<"\nAugmented matrix after pevoting:\n";
dispmatrix(x,n);
}
//---------end of pivoting---------------------------for(i=k+1;i<=n;i++) //for2
{
mul[i]=x[i][k]/x[k][k]; //as first m should be A21/A11,second A31/A11..
for(j=k;j<=n+1;j++) //for3
{
x[i][j]=x[i][j]-mul[i]*x[k][j];
}
//end of for3
}
//end of for2
//---------display intermediate matrices-----cout<<"\nAugmented matrix after operation:\n";
dispmatrix(x,n);
cout<<endl<<endl;
//-----------------end of display----------------------------Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

} //end of for3
//------------end of conversion to U.T.M.-------------------------//----Back wards substitution-------------------X[n]=x[n][n+1]/x[n][n];
for(j=n-1;j>=1;j--)
{
X[j]=x[j][n+1];
for(k=j+1;k<=n;k++)
{
X[j]=X[j]-X[k]*x[j][k];
}
X[j]=X[j]/x[j][j];
}
//-------------------------------------------------//-----------display of solution----------------cout<<"\nThe solution set is:\n";
for(i=1;i<=n;i++)
{
cout<<"\nX"<<i<<"= "<<X[i];
}
getch();
}
NEWTON RAPHSON
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.0001
//#define f(a) pow(a,3)-4*a+1
//#define df(a) 3*a*a-4
#define f(a) (a*sin(a)+cos(a))
#define df(a) (a*cos(a))
void main()
{
float x0,x1,f1,df1,e,root;
int i=1;
clrscr();
printf("Enter initial values x0 :");
scanf("%f",&x0);
e=(fabs(f(x0)));
f1=f(x0);
df1=df(x0);
if(f1==0)
{
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

printf("initial values are not valid, press any key to exit.");


getch();
exit(0);
}
printf("itern x0 x1
f(x1) df(x1) error\n\n");
do
{
x1=x0-f1/df1;
f1=f(x1);
df1=df(x1);
x0=x1;
printf("%3d: %8.4f %8.4f %8.4f %8.4f %8.4f",i,x0,x1,f1,df1,e);
e=fabs(f1);
i++;
printf("\n\n");
}
while(e>=E);
root=x1;
printf("\nroot is:%8.4f",root);
getch();
}
SECANT METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define E 0.0001
#define f1(a) (a*a*a-4*a+1)
//#define df1(a) 3*a*a-4
//#define f1(a) a*sin(a)+cos(a)
//#define df1(a) a*cos(a)
void main()
{
float x0,x1,x2,f0,f1,e;
int i=1;
clrscr();
printf("Enter initial values x0 & x1:");
scanf("%f%f",&x0,&x1);
f0=f1(x0);
f1=f1(x1);
if((f1-f0)==0)
{
printf("initial values are not valid, press any key to exit.");
getch();
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

exit(0);
}
printf("itern x0
x1 f(x0) f(x1) x2 error\n\n");
do
{
f0=f1(x0);
f1=f1(x1);
x2=(x1-f1*(x1-x0)/(f1-f0));
e=fabs(f1(x2));
printf("%3d: %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f",i,x0,x1,f0,f1,x2,e);
x0=x1;
x1=x2;
i++;
printf("\n\n");
}
while(e>=E);
printf("\nroot is:%8.4f",x1);
getch();
}
LINEAR REGRESSION
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define N 10
void main()
{
int n,i,j;
float x[N],y[N];
float a,b,r,sumlogx=0.0,sumlogy=0.0,sumlogxy=0.0,sumlogx2=0.0;
clrscr();
cout<<"Enter the no of data items: "; cin>>n;
cout<<"\nThe data points are: \n";
for(i=0;i<n;i++) cin>>x[i];
cout<<"\nThe corresponding functional values are: \n";
for(i=0;i<n;i++) cin>>y[i];
for(i=0;i<n;i++)
{
sumlogx+=log(x[i]);
sumlogy+=log(y[i]);
sumlogxy+=log(x[i])*log(y[i]);
sumlogx2+=log(x[i])*log(x[i]);
}
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

b=(n*sumlogxy-sumlogx*sumlogy)/(n*sumlogx2-(sumlogx*sumlogx)) ;
r=(sumlogy-b*sumlogx)/n;
a=exp(r);
cout<<"\nThe values of a and b are: ";
cout<<"\na:"<<a<<"\nb:"<<b;
cout<<"\nRequired equation is :\ty= "<<a<<"x^"<<b;
getch();
}
LAGRANGE INTERPOLATION
#include<iostream.h>
#include<conio.h>
#define N 10
void main()
{
int i,j,n;
float c,x[N],f[N],l[N],p=0;
clrscr();
cout<<"Enter the no of the data points: "; cin>>n;
cout<<"\nEnter the given points and corresponding functional value :\n";
cout<<"\tx\t\tf(x)\n";
for(i=0;i<n;i++)
{
cout<<"\t";
cin>>x[i];
cout<<"\t\t\t";
cin>>f[i];
}
cout<<"\nEnter the point to be interpolated: "; cin>>c;
for(i=0;i<n;i++)
{
l[i]=1;
for(j=0;j<n;j++)
{
if(j!=i)
{
l[i]*=((c-x[j])/(x[i]-x[j]));
}
}
p+=(f[i]*l[i]);
}
cout<<"\nThe required interpolated value is: "<<p;
getch();
}
Samrat Subedi
samratsubedi@kec.edu.np
samratsubedi@gmail.com

Das könnte Ihnen auch gefallen