Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- float dydx(float x, float y)
- {
- return((x - y)/(x+y));
- }
- int main()
- {
- float k1, k2, k3, k4, k5;
- float x0 = 0, y0 = 1, x = 1.4, h = 0.2,y;
- int n; int ch;
- n = (int)((x - x0) / h);
- y = y0;
- while(1)
- {
- printf("\n\n1. Euler's method\n2. RK4\n3. Exit\n");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- x0 = 0, y0 = 1, x = 1.4, h = 0.2;
- y=y0;
- printf("\nx\ty");
- for(int i=1;i<=n;i++)
- {
- y=y+h*dydx(x0,y);
- x0=x0+h;
- printf("\n%.3f\t%.3f",x0,y);
- }
- break;
- case 2:
- x0 = 0, y0 = 1, x = 1.4, h = 0.2;
- y=y0;
- printf("\nx\ty");
- for (int i=1; i<=n; i++)
- {
- k1 = h*dydx(x0, y);
- k2 = h*dydx(x0 + 0.5*h, y + 0.5*k1);
- k3 = h*dydx(x0 + 0.5*h, y + 0.5*k2);
- k4 = h*dydx(x0 + h, y + k3);
- y = y + (1.0/6.0)*(k1 + 2*k2 + 2*k3 + k4);;
- x0 = x0 + h;
- printf("\n%.3f\t%.3f",x0,y);
- }
- break;
- case 3:
- exit(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment