Advertisement
tampurus

Unit 5.4 Runga Kutta 4th order

May 13th, 2022 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. algorithm
  2.  
  3. start
  4. input x0,y0,h,last point n
  5. m1 = f(xi,yi)
  6. m2 = f(xi+h/2,yi+m1*h/2)
  7. m3 = f(xi+h/2 , yi+m2*h/2);
  8. m4 = f(xi+h,yi+m3*h)
  9. y(i+1) = yi + ( (m1 + 2m2 + 2m3 + m4)/6)
  10. display Output
  11. stop
  12.  
  13. // program
  14. #include <stdio.h>
  15. #include<math.h>
  16. #define f(x,y) (x*y)
  17. int main(){
  18.     int n,i;
  19.     float x0,xn,y0,h,s,s1,s2,s3,s4;
  20.     printf("Enter the x0,y0,h & xn\n");
  21.     scanf("%f %f %f %f",&x0,&y0,&h,&xn);
  22.     while(x0<xn){
  23.         s1 = h*f(x0,y0);
  24.         s2 = h*f((x0+(h/2)),(y0+(s1/2)));
  25.         s3 = h*f((x0+(h/2)),(y0+(s2/2)));
  26.         s4 = h*f((x0+h),(y0+s3));
  27.         s = (s1 + (2*s2) + (2*s3) + s4)/6;
  28.         y0 = y0+s;
  29.         x0 = x0+h;
  30.         printf("\ny(%f) =  %f",x0,y0);
  31.     }
  32.     printf("\n\nFinal Output y(%f) =  %f",x0,y0);
  33.     return 0;
  34. }
  35.  
  36. /*
  37. Enter the x0,y0,h & xn
  38. 1 2 0.2 1.4
  39.  
  40. y(1.200000) =  2.492143
  41. y(1.400000) =  3.232107
  42.  
  43. Final Output y(1.400000) =  3.232107
  44. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement