Advertisement
tampurus

Unit 5.1 Euler's Method

May 12th, 2022 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. // program
  2. #include<stdio.h>
  3. #define f(x,y) (y-x)/(y+x)
  4. int main(){
  5.     float x0,y0,h,xn,x,y;
  6.     int n;
  7.     printf("Enter values x0, y0, h and xn \n");
  8.     scanf("%f %f %f %f",&x0,&y0,&h,&xn);
  9.     n = (xn-x0)/h;
  10.     for(int i=1 ; i<=n ; i++){
  11.         printf("\nvalues of x0 = %f & y0 = %f",x0,y0);
  12.         y = y0 + (h*f(x0,y0));
  13.         x = x0 + h;
  14.        
  15.         if(x<xn){
  16.             x0 = x;
  17.             y0 = y;
  18.         }
  19.         else
  20.             break;
  21.     }
  22.     printf("\nOutput x = %f and y = %f",x,y);
  23.     return 0;
  24. }
  25. /*
  26. Ouput
  27. Enter values x0, y0, h and xn
  28. 0 1 0.02 0.1
  29.  
  30. values of x0 = 0.000000 & y0 = 1.000000
  31. values of x0 = 0.020000 & y0 = 1.020000
  32. values of x0 = 0.040000 & y0 = 1.039231
  33. values of x0 = 0.060000 & y0 = 1.057748
  34. values of x0 = 0.080000 & y0 = 1.075601
  35.  
  36. Output x = 0.100000 and y = 1.092832
  37. */
  38.  
  39.  
  40. // Algorithm
  41.  
  42.  
  43. start
  44.  
  45. define function // define f(x,y) x*typede
  46.  
  47. Get the values of x0,y0,h and xn
  48.  
  49. *Here x0 and y0 are the initial conditions
  50. h is the interval
  51. xn is the requred value
  52. // x0= 0 y0=1 , h=0.1 , xn=0.4
  53.  
  54. n=(xn-x0)/h // n=(0.4-0)/0.1 = 4
  55.  
  56. start loop from i =1 to n
  57.  
  58. Print values of y0 and x0 // 1,0 // 1,0.1 // 1,0.1,0.2 // 1.0302,-.0302
  59.  
  60. y=y0  + h*f(x0,y0)//1.061106
  61. x=x0+h//0.4
  62.  
  63. check if x<xn // 0.4<0.4.4
  64. if yes
  65. then
  66.     x0 = x
  67.     y0 = y
  68. if no goto 10
  69.  
  70. End loop i
  71.  
  72. Print x and y
  73.  
  74. Stop
  75.  
  76.  
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement