Advertisement
tampurus

Unit 5.3 Runga Kutta 2nd Order Method

May 12th, 2022 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. start
  2. input x,y,h,xf,  initial values, size and required values
  3.  
  4. while loop until (x<xf)
  5.     s1 = f(x,y);
  6.     s2 = f(x+h,y+(h*s1));
  7.     s = (s1+s2)/2;
  8.     y = y+ (h*s);
  9.     x = x+h;   
  10. end while loop
  11.  
  12. display output y
  13. stop
  14.  
  15.  
  16. // Online C compiler to run C program online
  17. #include <stdio.h>
  18. #include<math.h>
  19. #define f(x,y) (x+y)
  20. int main() {
  21.     int n,i;
  22.     float x0,y0,xn,h,s,s1,s2;
  23.      printf("Enter the x0,y0,h & xn\n");
  24.     scanf("%f %f %f %f",&x0,&y0,&h,&xn);
  25.    
  26.     while(x0<xn){
  27.         s1 = f(x0,y0);
  28.         s2 = f(x0+h,y0+(h*s1));
  29.         s = (s1+s2)/2;
  30.         y0 = y0+ (h*s);
  31.         x0 = x0 + h;
  32.         printf("\ny(%f) =  %f",x0,y0);
  33.     }
  34.     printf("\n\nFinal Output y(%f) =  %f",x0,y0);
  35. }
  36.  
  37. Enter the x0,y0,h & xn
  38. 0 1 0.1 0.4
  39.  
  40. y(0.100000) =  1.110000
  41. y(0.200000) =  1.242050
  42. y(0.300000) =  1.398465
  43. y(0.400000) =  1.581804
  44.  
  45. Final Output y(0.400000) =  1.581804
  46. /*
  47. Enter the first and second value
  48. 1 2
  49. Enter the length and required result
  50. 0.2 1.4
  51.     x0             y0              s1              s2              s
  52. 1.200000        2.680000        3.000000        3.800000        3.400000
  53. 1.400000        3.553600        3.880000        4.856000        4.368000
  54.  
  55. Output is 3.553600
  56. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement