orneto

rugekutta-2

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