Algorithm : Euler Method Euler(h,y0,a,b) { //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(a<=b) { sum=y0+h*f(a,y0) a=a+h print(i,a,sum) y0=sum sum=0 // Initializes sum to 0 i=i+1 } } C Program For Euler Method Source Code: #include<stdio.h> #include<conio.h> #include<math.h> void euler(float,float,float,int); float f(float,float); void main() { int n; float a,b,y0; clrscr(); printf("\nSOLVE :\n"); printf("\n (dy/dx)=xy 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); euler(a,b,y0,n); getch(); } void euler(float a,float b,float y0,int n) { float h,x,y1,y2; int i=0; h=(b-a)/n; x=a; printf("\nSOLUTION :"); printf("\n\tXr\t\tYr\t"); printf("\n---------------------------------"); printf("\nr=%d\t%g\t\t%g",i,x,y0); y1=y0; for(i=1;i<=n;i++) { y2=y1+h*f(x,y1); x=a+i*h; printf("\nr=%d\t%g\t\t%g",i,x,y2); y1=y2; } } float f(float x,float y) { return x*y; } OUTPUT : SOLVE : (dy/dx)=xy 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:5 SOLUTION : Xr Yr --------------------------------- r=0 0 1 r=1 0.2 1 r=2 0.4 1.04 r=3 0.6 1.1232 r=4 0.8 1.25798 r=5 1 1.45926
Tags:
Differential Equations
1 comments
design
ReplyDelete