TP I20, semaine 5

Coordonnées polaires

Exemple de programme à réaliser :

/* ... */

struct complexe polaire (double r, double t){
  struct complexe z;
  double pi;
  
  pi = acos (-1);
  t = t * pi / 180;
  z.reel = r * cos (t);
  z.imaginaire = r * sin (t);
  return z;
}
  
struct complexe puissance (struct complexe u, int n) {
  struct complexe v = {1, 0};
  int i; 
  
  for (i = 0; i < n; i++)
    v = produit (u, v);
  return v;
}

void afficher (struct complexe u) {
  double y = u.imaginaire;
  
  if (y >= 0)
    printf ("%8.4f + %7.4f i\n", u.reel, y);
  else
    printf ("%8.4f - %7.4f i\n", u.reel, - y);
}

void main () {
  struct complexe u;
  int i;
  
  u = polaire (2, 30);  
  for (i = 1; i <= 12; i++)
    afficher (puissance (u, i));
}

Exécution sur Sparc :

    1.73 +    1.00 i
    2.00 +    3.46 i
    0.00 +    8.00 i
   -8.00 +   13.86 i
  -27.71 +   16.00 i
  -64.00 +    0.00 i
 -110.85 -   64.00 i
 -128.00 -  221.70 i
   -0.00 -  512.00 i
  512.00 -  886.81 i
 1773.62 - 1024.00 i
 4096.00 -    0.00 i

Exponentielle complexe

Exemple de programme à réaliser :

#define EPSILON 1e-15

/* ... */

double module (struct complexe z) {
  double x = z.reel, y = z.imaginaire;
  return sqrt (x * x + y * y);
}

struct complexe exponentielle (struct complexe z) {
  int n;
  struct complexe u = {1, 0}, s = {1,0};
  
  for (n = 1; module (u) > EPSILON; n++) {
    u = produit (u, z);
    u.reel = u.reel / n;
    u.imaginaire = u.imaginaire / n;
    s = somme (s, u);
  }
  return s;
}

void main () {
  double phi;
  struct complexe u;
  
  printf ("Entrer un réel : ");
  scanf ("%lf", &phi);
  u.reel = 0;
  u.imaginaire = phi;
  afficher (exponentielle (u));
  printf ("%8.4f  %8.4f\n", cos (phi), sin (phi) );
}

Exécution sur Sparc :

Entrer un réel : 0.12345
  0.9924 +  0.1231 i
  0.9924    0.1231

Equations numériques et structures

#define SIGNE 1
#define DIVERGE 2
#define MAXITER 50

struct solution {
  double valeur;
  int erreur, iteration;
};

typedef double fonction (double);

struct solution dichotomie (fonction f, double a, double b, double epsilon) {
  int k;
  double c;
  struct solution s;
  
  if (f (a) > 0) {
    c = a;
    a = b;
    b = c;
  }
  if (f (a) > 0 || f (b) < 0) {
    s.erreur = SIGNE;
    return s;
  }

  for (k = 0; fabs (b - a) > epsilon; k++) {
    c = (a + b) / 2;
    if (f(c) < 0)
      a = c;
    else
      b = c;
  }
  s.erreur = 0;
  s.valeur = a;
  s.iteration = k;
  return s;
}

struct solution lagrange (fonction f, double a, double b, double epsilon) {
  double c, ya, yb;
  int k;
  struct solution s;
  
  ya = f(a);
  for (k = 0; k < MAXITER && fabs (b - a) > epsilon; k++ ) {
    yb = f(b);
    c = (a * yb - b * ya) / (yb - ya);
    a = b;
    b = c;
    ya = yb;
  }
  
  if (k < MAXITER)
    s.erreur = 0;
  else
    s.erreur = DIVERGE;
  s.valeur = b;
  s.iteration = k;
  return s;
}

struct solution newton (fonction f, fonction fprime, double a, double epsilon) {
  double t;
  int k = 0;
  struct solution s;
  
  do {
    t = a;
    a = a - f(a) / fprime (a);
    k++;
  }
  while (k < MAXITER &&  fabs (a - t) > epsilon );
  
  if (k < MAXITER)
    s.erreur = 0;
  else
    s.erreur = DIVERGE;
  s.valeur = a;
  s.iteration = k;
  return s;
}

void afficher (struct solution x) {
  switch (x.erreur) {
    case 0 :	/* tout va bien */
      printf ("%20.15lf (%3d itérations)\n", x.valeur, x.iteration);
      break;
      
    case SIGNE :
      printf ("Méthode inapplicable (erreur de signe)\n");
      break;
      
    case DIVERGE :
      printf ("Méthode inapplicable (suite divergente)\n");
      break;
      
    default :
      printf ("Erreur imprévue\n");
      break;
  }
}