Guest User

Untitled

a guest
Apr 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define PI 3.1415926
  6.  
  7. /* Programa de demonstracao que implementa o metodo de resolucao de equacao */
  8. /* de conveccao  por um metodo explicito */
  9.  
  10. float f(float x);
  11. float h(float x);
  12. float gx0(float t);
  13. float gxfim(float t);
  14.    
  15. int main(void)
  16. {  
  17.     FILE *outf;
  18.    
  19.     float t, t0, tfim, x, x0, xfim,
  20.       deltat, deltax, alfa, c, v;
  21.  
  22.     float **solucao;
  23.      
  24.     int i, j, nx, nt;
  25.  
  26.    
  27.     outf = fopen("questao2.dat", "w");
  28.  
  29.     deltat = 0.00004; /* Discretizacao do tempo */
  30.     deltax = 0.01;   /* Discretizacao do espaco */
  31.  
  32.     t0   = 0.0;
  33.     tfim = 5.0;
  34.  
  35.     x0   = 0.0;
  36.     xfim = 5.0;
  37.  
  38.     nt = (int) ((tfim - t0)/deltat) +2;
  39.     nx = (int) ((xfim - x0)/deltax) +2;
  40.  
  41.  
  42.     solucao = (float **) malloc(nt*sizeof(float*));
  43.     for (i = 0; i < nt; i++)
  44.     {
  45.        solucao[i] = (float*) malloc (nx*sizeof(float));
  46.     }
  47.  
  48.     fprintf(stdout, " Numero de intervalos temporais %d e espaciais %d", nt, nx);
  49.  
  50.     /* Parametros fisicos e variavel auxiliar */
  51.  
  52.     v = 1;
  53.  
  54.     c = v * deltat/deltax;
  55.  
  56.     /* Condicao inicial */
  57.  
  58.     x = x0;
  59.  
  60.     for (j = 0; j < nx; j++)
  61.     {
  62.         solucao[0][j] = f(x);
  63.         x += deltax;
  64.     }
  65.  
  66.     /* Contorno */
  67.     t = t0;
  68.     for (i = 0; i < nt-1; i++)
  69.     {
  70.         t += deltat;
  71.         solucao[i][0]      = gx0(t);
  72.         solucao[i][nx - 1] = gxfim(t);
  73.         /* Meio */
  74.         x = x0;
  75.         for (j = 1; j < nx - 1; j++)
  76.         {
  77.             x += deltax;
  78.            
  79.             if (i == 0)
  80.                 solucao[1][j] = (1 - c*c)*f(x) + (c*c/2)*(f(x+deltax)+f(x-deltax)) + deltat*h(x);
  81.             else    
  82.                 solucao[i+1][j] = 2*(1 - c*c)*solucao[i][j] + (c*c)*(solucao[i][j+1]+solucao[i][j-1]) - solucao[i-1][j];                
  83.            
  84.         }
  85.     }      
  86.  
  87.     x = x0;
  88.  
  89.     for (j = 0; j < nx; j++)
  90.     {
  91.         fprintf(outf, "%f ", x);
  92.         for (i = 0; i < nt; i++)
  93.         {
  94.             fprintf(outf, "%f ", solucao[i][j]);
  95.         }
  96.         fprintf(outf, "\n");
  97.         x += deltax;
  98.     }
  99.    
  100.     for (i = 0; i < nt; i++)
  101.     {
  102.        free(solucao[i]);
  103.     }
  104.     free(solucao);
  105.    
  106. }
  107.  
  108. float f(float x)
  109. /* Funcao que da' a condicao inicial */
  110. {
  111.     if (x <= 0 || x >= 5.0)
  112.         return 0;
  113.     return exp(-((x-2.5)*(x-2.5)/0.04));
  114. }
  115.  
  116. float h(float x)
  117. /* dfi/dt em t=0 */
  118. {
  119.     return(0.0);
  120. }
  121.  
  122. float gx0(float t)
  123. {
  124.    return(0.0);
  125. }
  126.    
  127. float gxfim(float t)
  128. {
  129.    return(0.0);
  130. }
Add Comment
Please, Sign In to add comment