1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "main.h" //contient les declarations des fonctions de ce fichier main.c
const long n=4,MAX=100,MIN=0;
int main(int argc, char *argv[])
{
double vect[n],mat[n][n],matb[n][n];
double x[n],b[n];
long c;
srand(time(NULL));
printf("calcul sur les vecteurs et matrices\n");
printf("calcul aleatoire du vecteur et de la matrice\n");
aleatvect(vect,n); //creation d un tableau de nb entiers aleatoires
affichevect(vect,n); //affichage du tableau
copievect(vect,x,n); //copie de ce tableau vect dans le tableau x
affichevect(x,n); //affichage du tableau x
aleatmat(mat,n); //creation d une matrice 4*4 de nb aleatoires
affichemat(mat,n); //affichage de la matrice
copiemat(mat,matb,n); //copie de la matrice mat dans la matrice matb
affichemat(matb,n); //affichage de matb
// printf("calcul de la norme du vecteur\t\n");
// printf("%lf\n",normvect(vect,n));
printf("calcul du produit Matrice.Vecteur=Vecteurb\n");
MX(mat,vect,b,n); //calcul du vecteur b=mat*vect
affichevect(b,n); //affichage de b
printf("resolution de M.x=b\n");
resolution(mat,x,b,n); //resolution de mat*x=b avec x inconnue
affichevect(x,n); //normalement le vecteur x doit redonner le x initial
printf("verification : \n");
MX(matb,x,b,n); //calcul de matb*x qui doit redonner b
affichevect(b,n); //le resultat est mis dans b qu'on affiche
scanf("%ld",&c);
return 0;
}
void aleatvect(double *vect, long n) //cree un vecteur de nb entiers aleatoires
{
long i;
for(i=0;i<n;i++)
{
vect[i]=rand()%(MAX-MIN+1)+MIN;
}
}
void affichevect(double *vect,long n) //affiche les composantes du vecteur
{
long i;
printf("affichage du vecteur :\n");
for(i=0;i<n;i++)
{
printf("vect[%ld]=%lf\n",i,vect[i]);
}
}
void aleatmat(double (*mat)[n],long n) // cree une matrice carree de nb entiers aleatoires
{
long i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
mat[i][j]=rand()%(MAX-MIN+1)+MIN;
}
}
}
void affichemat(double (*mat)[n], long n) //affiche la matrice
{
long i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("mat[%ld][%ld]=%lf\t",i,j,mat[i][j]);
}
printf("\n");
}
}
double normvect(double *vect, long n) //calcule la norme du vecteur
{
long i;
double norm=0.0;
for(i=0;i<n;i++)
{
norm +=pow(vect[i],2);
}
return sqrt(norm);
}
void MX(double (*mat)[n], double *vect,double *b,long n)
{
long i,k;
double v;
for(i=0;i<n;i++)
{
v=0.0;
for(k=0;k<n;k++)
{
v +=mat[i][k]*vect[k];
}
b[i]=v;
}
}
void copievect(double *vect,double *x,long n)
{
long i;
for(i=0;i<n;i++)
{
x[i]=vect[i];
}
}
void copiemat(double (*mat)[n],double (*matb)[n],long n)
{
long i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
matb[i][j]=mat[i][j];
}
}
}
void resolution(double (*mat)[n],double *x,double *b,long n)
{
long i,j,k;
double sum;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
b[j]=mat[i][i]*b[j]-mat[j][i]*b[i];
for(k=0;k<n;k++) //k peut partir de j on s en fout que les zeros soient pas affichés
{
mat[j][k]=mat[i][i]*mat[j][k]-mat[j][i]*mat[i][k];
}
}
}
affichemat(mat,n);
//affichevect(b,n);
x[n-1]=b[n-1]/mat[n-1][n-1];
for(i=n-2;i>=0;i--) //on met le resultat dans x
{
sum=0.0;
for(j=i+1;j<n;j++)
{
sum +=mat[i][j]*x[j];
}
x[i]=(b[i]-sum)/mat[i][i];
}
//affichevect(x,n);
} |
Partager