Hi guys i have to create a code in c to read the following sequence
x(k) = γAx(k?1) + (1 ? γ)x(k?1)
;
where A
is a matrix , x
a vector γ
a number,. The method stops when k>=100 or infinity norm >tolerance .I have created the following code, i called the function sequence creation
. It compiles but i don't know if it is correct. Can you please look at it?
#include<stdio.h>
#define N 15
typedef double vector[N];
typedef double matrix[N][N];
double modulo(double);
void copyvet(vector, vector,int );
void prin_matrix (matrix,int );
void prin_vector (vector, int );
void read_matrix (matrix, int );
void read_vector(vector, int );
double infinity_norm (vector, int );
void sequence_creation(int,double,matrix,vector,vector);
int main()
{
int k=0,n;
double c;
double toll;
printf("define a positive number n
");
scanf("%d",&n);
printf("define the value of the constant
");
scanf("%lf",&c);
printf("define a tolerance
");
scanf("%lf",&toll);
matrix A;
vector x0,xk;
read_vector(x0,n);
read_matrix(A,n);
do
{
sequence_creation(n,c,A,x0,xk);
copyvet(x0,xk,n);
k++;
}while (k<100 && infinity_norm(xk,n)>toll);
printf("iterations are %d",k);
prin_vector(xk,n);
return 0;
}
void sequence_creation(int n,double c,matrix A,vector x,vector b)
{
for(int i=0;i<n;i++)
{
b[i]=0;
for(int j=0;j<n;j++)
{
b[i]+=(c*(A[i][j]*x[j])+(1-c)*x[j]);
}
}
return;
}
double infinity_norm ( vector delta, int n)
{
double r= modulo (delta[0]);
for (int i=1; i<n; i++ )
{
if (r<modulo (delta[i]))
r=modulo(delta[i]);
}
return r;
}
double modulo(double s)
{
if (s<0)
return -s;
else return s;
}
void read_vector(vector w, int dim)
{
printf("
");
for ( int i=0; i<dim ; i++)
{
printf ("v[%d]= ",i+1);
scanf("%lf",&w[i]);
}
return ;
}
void read_matrix(matrix A, int dim)
{
int i,j;
printf("
");
for ( i=0; i<dim ; i++){
for ( j=0; j<dim ; j++)
{
printf ("v[%d][%d]= ",i+1,j+1);
scanf("%lf",&A[i][j]);
}
}
return ;
}
void prin_matrix (matrix A,int dim){
int i,j;
printf("
la matrice inserita e'la seguente:
");
for(i=0;i<dim;i++)
{
printf("|");
for (j=0;j<dim;j++){
printf("%lf ",A[i][j]);
}
printf("|
");
}
}
void prin_vector (vector b, int n)
{
printf("il vettore inserito e' il seguente");
for (int i=0 ; i<n ; i++)
{
printf (" %lf ", b[i]);
}
return;
}
void copyvet(vector a, vector b,int n)
{
for(int i=0;i<n;i++)
{
a[i]=b[i];
}
}
question from:
https://stackoverflow.com/questions/65868004/generate-a-recursive-sequence-with-matrix-and-vector