-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4c634c
commit b8c7cea
Showing
3 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// Jacobi eigenvalue algorithm: | ||
// ============================ | ||
// | ||
// Jacobi eigenvalue algorithm is an iterative method for calculation of the | ||
// eigenvalues and eigenvectors of a symmetric matrix. | ||
// This program finds all the eigenvalues and the corresponding eigenvectors | ||
// of a real symmetric matrix. | ||
// Assume that the given matrix is real symmetric. | ||
// | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
void main() { | ||
int n, i, j, p, q, flag; | ||
double a[10][10], d[10][10], s[10][10], s1[10][10], s1t[10][10]; | ||
double temp[10][10], theta, zero=1e-5, max, pi=3.141592654; | ||
|
||
printf("Enter the size of the matrix "); | ||
scanf("%d",&n); | ||
|
||
printf("Enter the elements row wise \n"); | ||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) scanf ("%lf", &a[i][j]); | ||
} | ||
|
||
printf("The given matrix is\n"); | ||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) printf ("%8.4f ", a[i][j]); | ||
printf ("\n"); | ||
} | ||
printf ("\n"); | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
d[i][j]= a[i][j]; | ||
if(i==j) | ||
s[i][j]= 1; | ||
else | ||
s[i][j]= 0; | ||
} | ||
} | ||
|
||
do { | ||
flag= 0; | ||
p=0; q=1; | ||
max= fabs(d[p][q]); | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
if(i!=j) { | ||
if (max < fabs(d[i][j])) { | ||
max= fabs(d[i][j]); | ||
p= i; | ||
q= j; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if(d[p][p]==d[q][q]) { | ||
if (d[p][q] > 0) | ||
theta= pi/4; | ||
else | ||
theta= -pi/4; | ||
} | ||
else { | ||
theta=0.5*atan(2*d[p][q]/(d[p][p]-d[q][q])); | ||
} | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
if(i==j) { | ||
s1[i][j]= 1; | ||
s1t[i][j]= 1; | ||
} | ||
else { | ||
s1[i][j]= 0; | ||
s1t[i][j]= 0; | ||
} | ||
} | ||
} | ||
|
||
s1[p][p]= cos(theta); | ||
s1t[p][p]= s1[p][p]; | ||
|
||
s1[q][q]= cos(theta); | ||
s1t[q][q]= s1[q][q]; | ||
|
||
s1[p][q]= -sin(theta); | ||
s1[q][p]= sin(theta); | ||
|
||
s1t[p][q]= s1[q][p]; | ||
s1t[q][p]= s1[p][q]; | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
temp[i][j]= 0; | ||
for(p=0; p<n; p++) temp[i][j]+= s1t[i][p]*d[p][j]; | ||
} | ||
} | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
d[i][j]= 0; | ||
for(p=0; p<n; p++) d[i][j]+= temp[i][p]*s1[p][j]; | ||
} | ||
} | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
temp[i][j]= 0; | ||
for(p=0; p<n; p++) temp[i][j]+= s[i][p]*s1[p][j]; | ||
} | ||
} | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) s[i][j]= temp[i][j]; | ||
} | ||
|
||
for(i=0; i<n; i++) { | ||
for(j=0; j<n; j++) { | ||
if(i!=j) | ||
if(fabs(d[i][j]) > zero) flag= 1; | ||
} | ||
} | ||
} while(flag==1); | ||
|
||
printf("The eigenvalues are \n"); | ||
for(i=0; i<n; i++) | ||
printf("%8.4lf ",d[i][i]); | ||
printf("\nThe corresponding eigenvectors are \n"); | ||
|
||
for(j=0; j<n; j++) { | ||
printf("("); | ||
for(i=0; i<n-1; i++) | ||
printf("% 8.4lf,",s[i][j]); | ||
printf("%8.4lf )^T\n",s[n-1][j]); | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
int main() | ||
{ | ||
double x[4]={0,1,2,3},b[4][1]={1,0,1,2}; | ||
double a[4][3],at[3][4],A[3][3],B[3][1],mat[3][4]; | ||
|
||
for(int i=0;i<4;i++) | ||
{ | ||
a[i][0]=1; | ||
a[i][1]=x[i]; | ||
a[i][2]=x[i]*x[i]; | ||
//a[i][3]=x[i]*x[i]*x[i]; | ||
} | ||
|
||
|
||
/** making transpose matrix **/ | ||
for(int i=0;i<3;i++) | ||
{ | ||
for(int j=0;j<4;j++) | ||
{ | ||
at[i][j]=a[j][i]; | ||
} | ||
} | ||
|
||
|
||
/* multilication of transpose matrix X matrix */ | ||
for(int i=0;i<3;i++) | ||
{ | ||
for(int j=0;j<4;j++) | ||
{ | ||
A[i][j]=0; | ||
} | ||
} | ||
|
||
for(int i=0;i<3;i++) | ||
{ | ||
for(int j=0;j<3;j++) | ||
{ | ||
for(int k=0;k<4;k++) | ||
{ | ||
A[i][j]+=(at[i][k]*a[k][j]); | ||
} | ||
} | ||
} | ||
|
||
|
||
/* updating B */ | ||
for(int i=0;i<3;i++) | ||
{ | ||
for(int j=0;j<1;j++) | ||
{ | ||
B[i][j]=0; | ||
} | ||
} | ||
|
||
for(int i=0;i<3;i++) | ||
{ | ||
for(int j=0;j<1;j++) | ||
{ | ||
for(int k=0;k<4;k++) | ||
{ | ||
B[i][j]+=(at[i][k]*b[k][j]); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** using gauss elimination process to find the least square fitted values **/ | ||
for(int i=0;i<3;i++) | ||
{ | ||
for(int j=0;j<3;j++) | ||
{ | ||
mat[i][j]=A[i][j]; | ||
} | ||
mat[i][3]=B[i][0]; | ||
} | ||
|
||
|
||
/** upper triangular matrix **/ | ||
for(int j=0;j<3;j++) | ||
{ | ||
for(int i=j+1;i<3;i++) | ||
{ | ||
int m=mat[j][j]; | ||
int n=mat[i][j]; | ||
|
||
for(int k=0;k<=3;k++) | ||
{ | ||
mat[i][k]=(m*mat[i][k])-(n*mat[j][k]); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** back substitution **/ | ||
double ans[3]; | ||
ans[3-1]=(mat[3-1][3]/mat[3-1][3-1]); | ||
|
||
for(int j=3-2;j>-1;j--) | ||
{ | ||
double sum=0; | ||
for(int i=j+1;i<3;i++) | ||
{ | ||
sum+=(mat[j][i]*ans[i]); | ||
//printf("sum %9.3lf\n",sum); | ||
} | ||
|
||
ans[j]=(mat[j][3]-sum)/(mat[j][j]); | ||
} | ||
|
||
|
||
/** answer **/ | ||
for(int i=0;i<3;i++) | ||
{ | ||
printf("c%d = %9.3lf\n",i+1,ans[i]); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include<math.h> | ||
#define MAXN 200 | ||
#define F(X) ((X*X*X) + (4*X*X)-10) | ||
|
||
int main() | ||
{ | ||
double a,b,p,tol,fa,fb,fp; | ||
a=1.25,b=1.5,tol=10.e-6; | ||
int i=0; | ||
fa=F(a),fb=F(b); | ||
|
||
if(fa*fb>=0) | ||
{ | ||
puts("no root"); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
puts("a b c f(a) f(b) f(c)"); | ||
|
||
while(i<MAXN) | ||
{ | ||
|
||
i++; | ||
p=((a*fb)-(b*fa))/(fb-fa); | ||
fp=F(p); | ||
printf("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",a,b,p,fa,fb,fp); | ||
if(fabs(fp)<=tol) | ||
{ | ||
printf("the approaximate solution is = %lf\n",p); | ||
printf("iteration number =%d\n",i); | ||
return EXIT_SUCCESS; | ||
} | ||
else | ||
{ | ||
if(fp*fa<0) | ||
{ | ||
b=p; | ||
fb=fp; | ||
} | ||
else | ||
{ | ||
a=p; | ||
fa=fp; | ||
} | ||
} | ||
|
||
} | ||
printf("iteration overflow . . . "); | ||
} |