Aller au contenu

Mathc gnuplot/Application : Gradient

Un livre de Wikilivres.
Mathc gnuplot
Mathc gnuplot
Mathc gnuplot
Sommaire

I - Dessiner

Fichiers h partagés :

Application :

II - Animer

Application :

III - Géométrie de la tortue standard

Application :

IV - Géométrie de la tortue vectorielle

Application :

Annexe


Le gradient dans Wikipedia.

Dessiner la courbe de niveau C de f qui contient P et dessiner le gradient au point P.

N'oubliez pas les fichiers *.h partagés et ceux de ce chapitre.

Dessiner le gradient au point P

[modifier | modifier le wikicode]
c01.c
Dessiner le gradient au point P
/* ------------------------------------ */
/*  Save as :   c01.c                   */
/* ------------------------------------ */
#include "x_ahfile.h"
#include       "fa.h"
/* ------------------------------------ */
int main(void)
{
double   h  = .0001;
 point2d p  = i_point2d(2,1);
vector2d u  = grad_fxy(g,h,p);

 clrscrn();
 printf(" Sketch both the level curve C of f "
        "that contains P and grad(P)   \n\n");
 printf(" f : (x,y)-> %s\n\n", feq);
 printf(" with p(%+.3f,%+.3f) \n\n",p.x,p.y);

  printf(" In first sketch the graph of f(x,y) = 0\n\n");

     G_3d_eq(i_WsGnuplot(-10,10,-10,10,-100,100),
             i_VGnuplot( 55.,57.,1.,1.),
             feq);

 printf(" Open the file \"a_main.plt\" with gnuplot.\n");
 getchar();

 clrscrn();
 printf(" The level curves have the form f(x,y) = k \n");
 printf(" with p(%+.3f,%+.3f) k = %+.3f \n\n",
          p.x,p.y,f(p.x,p.y));

 printf(" The new function is :\n");
 printf(" g : (x,y)-> %s\n\n", geq);

  printf(" grad(g)]p = %+.3fi  %+.3fj\n",u.i,u.j);
  printf(" is a vector normal to the function\n");
  printf(" g : (x,y)-> %s\n", geq);
  printf(" at the point p(%+.3f,%+.3f)\n\n", p.x,p.y);

  G_3d_levelcurvegradfxy(i_WsGnuplot(-6,6,-6,6,
                                    g(p.x,p.y),
                                    g(p.x,p.y)+.1),
                         g,geq,
                         p);

 printf(" Open the file \"a_main.plt\" with gnuplot.\n"
        " Press return to continue.\n");

 getchar();

 return 0;
}

Le résultat.


Sketch both the level curve C of f  that contains P and grad(P)    
.
f : (x,y)->  y**2-x**2
.
with p(+2.000,+1.000) 
.
In first sketch the graph of f(x,y) = 0
.
Open the file "a_main.plt" with gnuplot.
Résultat dans gnuplot
Tangente30
The level curves have the form f(x,y) = k 
with p(+2.000,+1.000) k = -3.000 
.
The new function is :
g : (x,y)->  y**2-x**2+3
.
grad(g)]p = -4.000i  +2.000j
is a vector normal to the function
g : (x,y)->  y**2-x**2+3
at the point p(+2.000,+1.000)
.
Open the file "a_main.plt" with gnuplot.
Press return to continue.
Résultat dans gnuplot
Tangente31


Les fichiers h de ce chapitre

[modifier | modifier le wikicode]
x_ahfile.h
Appel des fichiers
/* ------------------------------------ */
/*  Save as :   x_ahfile.h              */
/* ------------------------------------ */
#include    <stdio.h>
#include   <stdlib.h>
#include    <ctype.h>
#include     <time.h>
#include     <math.h>
#include   <string.h>
/* ------------------------------------ */
#include     "xdef.h"
#include     "xspv.h"
#include     "xplt.h"
#include   "xfxy_x.h"
/* ------------------------------------ */
#include    "kgrad.h"
#include    "kg_3d.h"
#include  "kg_grad.h"


fa.h
La fonction à dessiner
/* ------------------------------------ */
/*  Save as :   fa.h                   */
/* ------------------------------------ */
double f(
double x,
double y)
{
 return( ( y*y-x*x) );
}
char  feq[] = " y**2-x**2";
/* ------------------------------------ */
double g(
double x,
double y)
{
 return( ( y*y-x*x+3) );
}
char  geq[] = " y**2-x**2+3";
/* ------------------------------------ */


kgrad.h
Le gradient
/* ------------------------------------ */
/*  Save as :   kgrad.h                 */
/* ------------------------------------ */
vector2d grad_fxy(
double (*P_f)(double x,double y),
double   h,
point2d  p
)
{
vector2d   u;

       u.i = fxy_x((*P_f),h,p);
       u.j = fxy_y((*P_f),h,p);

 return(u);
}


kg_3d.h
La fonction graphique (1)
/* ------------------------------------ */
/*  Save as :   kg_3d.h                 */
/* ------------------------------------ */
void G_3d_eq(
   Ws_Ctrl W,
View_Ctrl V,
char      feq[]
)
{
FILE   *fp = fopen("a_main.plt","w");
fprintf(fp,"reset\n"
           "set    samples 40\n"
           "set isosamples 40\n"
           "set hidden3d\n"
           "set view %0.3f, %0.3f, %0.3f, %0.3f \n"
           "splot[%0.3f:%0.3f][%0.3f:%0.3f][%0.3f:%0.3f]\\\n"
           "%s",
            V.rot_x,V.rot_z,V.scale,V.scale_z,
            W.xmini,W.xmaxi,W.ymini,W.ymaxi,W.zmini,W.zmaxi,
            feq);
 fclose(fp);
}


kg_grad.h
Dessiner la courbe de niveau qui contient P et le gradient au point P
/* ------------------------------------ */
/*  Save as :   kg_grad.h               */
/* ------------------------------------ */
void G_3d_levelcurvegradfxy(
Ws_Ctrl W,
double (*P_f)(double x, double y),
char    feq[],
point2d   p
)
{
FILE   *fp;
double   h  = .0001;

        fp = fopen("a_main.plt","w");
fprintf(fp,"reset\n"
           "set    samples 400\n"
           "set isosamples 400\n"
           "set view 1.000, 1.000, 1.000, 1.000 \n"
           "splot[%0.3f:%0.3f][%0.3f:%0.3f][%0.3f:%0.3f]\\\n"
           "%s,\\\n"
           "\"a_vect\" with linesp lt 3\n",
           W.xmini,W.xmaxi,W.ymini,W.ymaxi,W.zmini,W.zmaxi,
           feq);
 fclose(fp);

         fp = fopen("a_vect","w");
 fprintf(fp," %6.3f   %6.3f   %6.3f\n",
             p.x, p.y, ((*P_f)(p.x,p.y)) );
 fprintf(fp," %6.3f   %6.3f   %6.3f\n",
             p.x+fxy_x((*P_f),h,p),
             p.y+fxy_y((*P_f),h,p),
             (*P_f)(p.x,p.y) );
  fclose(fp);
}