Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

c - generate a recursive sequence with matrix and vector

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...