Runge Kutta Method
Algorithm Of Runge Kutta Method Order2 Runge_Of_order2(a,b,h,y0) { This algorithm takes the following inputs where h is the width of each division y0 is the value of y when x=0 and a&b are the limits while(x<=b) { x1=x+h k1=h*(f(x,y0)) k2=h*f(x+h,y0+h) k=(k1+k2)/2 y1=y0+k print(i,x1,y1) i=i+1 x=x1 y0=y1 } } Algorithm Of Runge Kutta Method Order4 Runge_Of_order4(a,b,h,y0) { This algorithm takes the following inputs where h is the width of each division y0 is the value of y when x=0 and a & b are the limits while(x<=b) { x1=x+h k1=h*(f(x,y0)) k2=h*f(x+h/2,y0+k1/2) k3=h*f(x+h/2,y0+k2/2) k4=h*f(x+h,y0+k3) k=(k1+2*k2+2*k3+k4)/2 y1=y0+k print(i,x1,y1) i=i+1 x=x1 y0=y1 } } C Program For Runge Kutta Method Source Code: #include<stdio.h> #include<conio.h> #include<math.h> void R_K(float,float,float,int,int); float f(float,float); void main() { int n,order; float a,b,y0; clrscr(); printf("\nSOLVE :\n"); printf("\n (dy/dx)=x-y using the given limits.\n"); printf("\nEnter the upper limit:"); scanf("%f",&b); printf("\nEnter the lower:"); scanf("%f",&a); printf("\nEnter the value of y for the first interval:"); scanf("%f",&y0); printf("\nENTER THE NUMBE3R OF SUB-DIVISIONS:"); scanf("%d",&n); printf("\nEnter order(2 or 4):"); scanf("%d",&order); R_K(a,b,y0,n,order); getch(); } void R_K(float a,float b,float y0,int n,int order) { float h,x,y1,y2,k=0,k1,k2,k3,k4; int i=0; h=(b-a)/n; x=a; printf("\nSOLUTION :"); printf("\n\tXr\tK\t\tYr\t"); printf("\n------------------------------------------------"); printf("\nr=%d\t%g\t%g\t\t%g",i,x,k,y0); y1=y0; for(i=1;i<=n;i++) { if(order==4) { k1=h*f(x,y1); k2=h*f((x+(h/2)),(y1+(k1/2))); k3=h*f((x+(h/2)),(y1+(k2/2))); k4=h*f((x+h),(y1+k3)); k=(k1+2*k2+2*k3+k4)/6; } else if(order==2) { k1=h*f(x,y1); k2=h*f((x+h),(y1+h)); k=(k1+k2)/2; } y2=y1+k; x=a+i*h; printf("\nr=%d\t%g\t%f\t%g",i,x,k,y2); y1=y2; } } float f(float x,float y) { return x-y; } OUTPUT(order 2): SOLVE : (dy/dx)=x-y using the given limits. Enter the upper limit:1 Enter the lower:0 Enter the value of y for the first interval:1 ENTER THE NUMBE3R OF SUB-DIVISIONS:10 Enter order(2 or 4):2 SOLUTION : Xr K Yr ------------------------------------------------ r=0 0 0 1 r=1 0.1 -0.100000 0.9 r=2 0.2 -0.080000 0.82 r=3 0.3 -0.062000 0.758 r=4 0.4 -0.045800 0.7122 r=5 0.5 -0.031220 0.68098 r=6 0.6 -0.018098 0.662882 r=7 0.7 -0.006288 0.656594 r=8 0.8 0.004341 0.660934 r=9 0.9 0.013907 0.674841 r=10 1 0.022516 0.697357 OUTPUT(order 4): SOLVE : (dy/dx)=x-y using the given limits. Enter the upper limit:1 Enter the lower:0 Enter the value of y for the first interval:1 ENTER THE NUMBE3R OF SUB-DIVISIONS:10 Enter order(2 or 4):4 SOLUTION : Xr K Yr ------------------------------------------------ r=0 0 0 1 r=1 0.1 -0.090325 0.909675 r=2 0.2 -0.0722132 0.837462 r=3 0.3 -0.055825 0.781637 r=4 0.4 -0.0409963 0.740641 r=5 0.5 -0.0275787 0.713062 r=6 0.6 -0.015438 0.697624 r=7 0.7 -0.00445263 0.693171 r=8 0.8 0.00548734 0.698659 r=9 0.9 0.0144814 0.71314 r=10 1 0.0226196 0.73576
Tags:
Differential Equations
0 comments