Mathc gnuplot/Application : Tangente
Préambule
modifierLa tangente dans Wikipedia.
Présentation
modifierN'oubliez pas les fichiers *.h partagés et ceux de ce chapitre.
Dessiner la tangente
modifierc01.c Dessiner la tangente |
---|
/* ------------------------------------ */
/* Save as : c01.c */
/* ------------------------------------ */
#include "x_ahfile.h"
#include "f2.h"
/* ------------------------------------ */
int main(void)
{
double c = 2;
printf(" f : x-> %s\n\n", feq);
printf(" f': x-> %s\n\n\n",Dfeq);
printf(" With c = %0.3f, the equation of the tangent is :",c);
printf("\n\n y = f'(c) (x-c) + f(c) =");
eq_Tan(c,f,Df);
G_Tan(i_WGnuplot(-7, 7,-2,2),
c,
feq,
f,
Df);
printf(" load \"a_main.plt\" with gnuplot.\n\n"
" Press return to continue");
getchar();
return 0;
}
Le résultat.
f : x-> cos(x) f': x-> (-sin(x)) . With c = 2.000, the equation of the tangent is : . y = f'(c) (x-c) + f(c) = -0.909*x +1.402 . load "a_main.plt" with gnuplot.
Résultat dans gnuplot |
---|
Les fichiers h de ce chapitre
modifierx_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 "xplt.h"
/* ------------------------------------ */
#include "kg_tan.h"
#include "k_tan.h"
f2.h La fonction à dessiner |
---|
/* ------------------------------------ */
/* Save as : f2.h */
/* ------------ f --------------------- */
double f(
double x)
{
return( cos(x));
}
char feq[] = " cos(x)";
/* ------------ f' ------------------- */
double Df(
double x)
{
return( (-sin(x)) );
}
char Dfeq[] = " (-sin(x)) ";
k_tan.h Equation de la tangente |
---|
/* ------------------------------------ */
/* Save as : k_tan.h */
/* ------------------------------------
y = ax + b [P(xp,yp);(y-yp)=a(x-xp)]
a = f'(x)
b = y - ax
b = y - f'(x)x
b = f(x) - f'(x)x
x=c
a = f'(c)
b = f(c) - f'(c)c
------------------------------------ */
void eq_Tan(
double c,
double (*P_f)(double x),
double (*PDf)(double x)
)
{
printf(" %0.3f*x %+0.3f\n\n\n",
(*PDf)(c), (*P_f)(c) - (*PDf)(c)*c );
}
/* ------------------------------------ */
void eq_Tanf(
FILE *fp,
double c,
double (*P_f)(double x),
double (*PDf)(double x)
)
{
fprintf(fp," %0.3f*x %+0.3f\n\n\n",
(*PDf)(c), (*P_f)(c) - (*PDf)(c)*c );
}
kg_tan.h La fonction graphique |
---|
/* ------------------------------------ */
/* Save as : kg_tan.h */
/* ------------------------------------ */
void G_Tan(
W_Ctrl w,
double c,
char fEQ[],
double (*P_f)(double x),
double (*PDf)(double x)
)
{
FILE *fp = fopen("a_main.plt","w");
fprintf(fp,"# Gnuplot file : load \"a_main.plt\" \n\n"
" set zeroaxis\n\n"
" plot [%0.3f:%0.3f] [%0.3f:%0.3f] \\\n"
" %s, \\\n"
" %0.6f*x %+0.6f lw 3\n"
" reset",
w.xmini,w.xmaxi,w.ymini,w.ymaxi,
fEQ,
(*PDf)(c),
(-(*PDf)(c)* c + (*P_f)(c)));
fclose(fp);
}
Exemple avec la sortie dans le fichier "a_out.txt"
modifierc01f.c Sortie dans un fichier |
---|
/* ------------------------------------ */
/* Save as : c01f.c */
/* ------------------------------------ */
#include "x_ahfile.h"
#include "f2.h"
/* ------------------------------------ */
int main(void)
{
FILE *fp = fopen("a_out.txt","w");
double c = 0;
fprintf(fp," f : x-> %s\n\n", feq);
fprintf(fp," f': x-> %s\n\n\n",Dfeq);
fprintf(fp," With c = %0.3f, the equation of the tangent is :",c);
fprintf(fp,"\n\n y = f'(c) (x-c) + f(c) =");
eq_Tanf(fp,c,f,Df);
G_Tan(i_WGnuplot(-7, 7,-2,2),c,feq,f,Df);
fprintf(fp," load \"a_main.plt\" with gnuplot.");
fclose(fp);
printf(" Read \"a_out.txt\".\n"
" Press return to continue");
getchar();
return 0;
}
Le résultat dans le fichiers "a_out.txt"
f : x-> cos(x) f': x-> (-sin(x)) . With c = 0.000, the equation of the tangent is : . y = f'(c) (x-c) + f(c) = -0.000*x +1.000 . load "a_main.plt" with gnuplot.
Résultat dans gnuplot |
---|