Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define H 0.000002
- #define INICIO -1
- #define FIM 1
- double fx(double x) {
- return (1+ tanh(4*x))/2;
- }
- double calculaH (int k) {
- double h0=H;
- return h0/pow(2,k);
- }
- double calculaX0(double x0, int k) {
- double h= calculaH(k);
- return ((-3.0/2.0)*fx(x0)+2.0*fx(x0+h)-(1.0/2.0)*fx(x0+2*h))*(1/h);
- }
- double calculaX1(double x1, int k) {
- double h= calculaH(k);
- return ( (-1.0/2.0) * fx(x1-h) + (1.0/2.0) * fx(x1+h)) * (1.0 / h);
- }
- double calculaX2(double x2, int k) {
- double h= calculaH(k);
- return ( (1.0/2.0) * fx(x2-2*h) - 2.0 * fx(x2-h) + (3.0/2.0) * fx(x2) ) * (1.0/h);
- }
- int main()
- {
- double x0=0, x1=0.1, x2=0.2,d;
- int k =4,i;
- double vetX0[k], vetX1[k],vetX2[k];
- for (i=0;i<=k;i++){
- vetX0[i]=calculaX0(x0,i);
- vetX1[i]=calculaX1(x1,i);
- vetX2[i]=calculaX2(x2,i);
- printf("\n\nk=%d : h=%.6lf\n",i,calculaH(i));
- printf("f'(x0)=%.20lf", vetX0[i]);
- printf("\nf'(x1)=%.20lf", vetX1[i]);
- printf("\nf'(x2)=%.20lf", vetX2[i]);
- }
- //calculando o erro relativo
- printf("\n\nErro relativo\n");
- /* for(i=1;i<k;i++){
- printf("\n\nK= %d\n",i);
- printf("dx0= %.20lf",(fabs(vetX0[i]-vetX0[i-1]))/fabs(vetX0[i]));
- // printf("\ndx1= %.6lf",(fabs(vetX1[i+1]-vetX1[i]))/fabs(vetX1[i]));
- // printf("\ndx2= %.6lf",(fabs(vetX2[i+1]-vetX2[i]))/fabs(vetX2[i]));
- }*/
- for(i=1;i<k;i++){
- printf("\n\nPara k = %d\n",i);
- printf("Er f'(x0)=%.6e",( fabs(vetX0[i]-vetX0[i-1]) ) / fabs(vetX0[i]) );
- printf("\nEr f'(x1)=%.6e",(fabs(vetX1[i]-vetX1[i-1]))/fabs(vetX1[i]));
- printf("\nEr f'(x2)=%.6e",(fabs(vetX2[i]-vetX2[i-1]))/fabs(vetX2[i]));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement