Advertisement
orneto

rugekutta-4

Jun 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. double funcao(double x, double y){
  5.     //insira aqui a função PVI
  6.     return (y);
  7. }
  8.  
  9. int main()
  10. {
  11.     double x0, y0, a, b, h, resulanterior, y, K1, K2, K3, K4, xi;
  12.     int n, i;
  13.  
  14.     printf("Entre com os valores de x0 e y0, respectivamente (Exemplo: y(0) = 1 | x0 = 0 e y0 = 1): ");
  15.     scanf(" %lf %lf",&x0,&y0);
  16.     printf("Entre com os valores de a e b, respectivamente: ");
  17.     scanf(" %lf %lf",&a,&b);
  18.     printf("Digite a quantidade de subintervalos que deseja: ");
  19.     scanf("%d",&n);
  20.     printf("\n\n");
  21.  
  22.     h = (b - a)/n;
  23.     resulanterior = y0;
  24.     xi = x0;
  25.  
  26.     for(i=0; i<n; i++){
  27.  
  28.        K1 = funcao(xi,resulanterior);
  29.        K2 = funcao(xi + (h/2), resulanterior + (h * K1)/2);
  30.        K3 = funcao(xi + (h/2), resulanterior + (h * K2)/2);
  31.        K4 = funcao(xi + h, resulanterior + (h * K3));
  32.  
  33.        y = resulanterior + (h/6) * (K1 + 2*K2 + 2*K3 + K4);
  34.  
  35.        printf("y%d = %.9lf\n",i+1,y);
  36.  
  37.        resulanterior = y;
  38.        xi += h;
  39.     }
  40.  
  41.     printf("\nResultado:\ny%d = %.9lf em x%d = %.3lf\n",n,y,n,b);
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement