1
Numerical solutions of ordinary first order
differential equations by the Euler method
Name : Sri Bhuvaneshwari A
Register No : 34423009
2
Euler’s Method
x0 x1 x x0 x1 x
f(x)
Given a differential equation y’= f (x, y)
and an initial condition (x0,y0)
We can approximate how the values of the function change using method
f(x)
error
error
h h
3
Euler’s Method
• The derivative of a function y(x) represents the slope of the tangent line at any point (x, y):
• For small h, we approximate this as:
• This equation expresses the idea that the rate of change of y is given by the function f(x, y).
• Rearranging the equation:
• which gives us Euler’s method formula:
where:
yn​is the current value,
xn​is the current step,
h is the step size,
f(xn,yn) is the slope at that point.
=
y(x+h) = y(x) + h f(x,y)
yn+1​
= yn​
+ h f(xn​
, yn​
)
4
Example
• Given Differential Equation:
with initial condition y(0)=1, and we want to find y(0.2) using step size h=0.1.
• Solution:
So, the Euler’s method approximation for y(0.2) is 1.22.
S.No x y
1 0 1
2 0.1 1.1
3 0.2 1.22
Step-by-Step Calculation:
Step 1: At x0 = 0, y0 = 1,
f(0,1)= 0 + 1 = 1
y1​
=y0​+ h f(x0​
,y0​
) =1+0.1×1 = 1.1
Step 2: At x1 = 0.1, y1 = 1.1
f(0.1, 1.1) = 0.1+1.1 = 1.2
y2​= y1​+ h f(x1​
,y1​
) = 1.1+0.1×1.2 = 1.22
yn+1​
= yn​
+ h f(xn​
, yn​
)
f(x,y) =
x+y.
5
Improved Euler’s Method (Heun’s Method/ Euler-Cauchy Method)
x0 x1 x
x0 x1 x x0 x1 x
x0 x1 x
f(x) f(x)
f(x)
y1 y1
y0 y0
 y’= f (x, y)
 Initial condition:(x0,y0)
 x1= x0+h , y1= y0 + h f(x0,y0)
 m = f(x0+h , y0 + h f(x0,y0)
 Average of the slopes:
y1=
yn+1 ​
= yn​+ ​[f(xn​, yn​
) + f(xn+1​,ypredict ​
)]
6
Example
• Given Differential Equation:
with initial condition y(0)=1, and we want to find y(0.2) using step size h=0.1.
• Solution:
• So, the Improved Euler’s method approximation for y(0.2) is 1.2421.
• Comparison of Results:
S.No x y
1 0 1
2 0.1 1.11
3 0.2 1.2421
Step-by-Step Calculation:
Step 1: At x0 =0, y0 = 1
Predict y1
∗
​
:
y1
∗
​= y​
0 + hf(0,1) =1 + 0.1×1 = 1.1
Compute f(x1,y1
∗
):
f(0.1, 1.1) = 0.1 + 1.1 = 1.2
Corrected y1​
:
y1​
= 1 + (0.1​
(1+1.2) /2)
= 1 + 0.1×1.1
= 1.11
Step 2: At x1 = 0.1, y1 = 1.11
f(0.1, 1.11) = 1.21
Predict y2
∗
​
:
y2​
∗
= 1.11 + 0.1×1.21 = 1.231
Compute f(x2, y2
∗
):
f(0.2, 1.231) = 0.2 + 1.231 =1.431
Corrected y2:
y2​= 1.11 + (0.1​
(1.21+1.431) /2)
= 1.11 + 0.1×1.3205
= 1.2421
Method y(0.2) Approximation
Euler's Method 1.22
Improved Euler's Method 1.2421
 y1= y0 + h f(x0,y0)
 y1=
7
Algorithm for Euler’s Method and Improved Euler Method:
• Step 1: Define the Problem
=
Given:
1. Initial values: x0 , y0
2. Step size: h
3. Final value of x (denoted as xn​
)
• Step 2: Euler’s Method Algorithm
Euler’s method is a simple numerical approach that approximates the solution by taking small steps.
1. Start with initial values (x0 , y0).
2. Repeat until x reaches xn​
3. Compute the next y value using the formula: ynew = y + h f (x , y )
⋅
4. Update x as: xnew = x + h
5. Print x, y.
6. Stop when x exceeds or reaches xn​
.
8
• Step 3: Improved Euler’s Method Algorithm (Heun’s Method)
The Improved Euler's Method corrects the error in the basic Euler's Method by using an average slope.
1. Start with initial values (x0 ,y0).
2. Repeat until x reaches x n​ :
 Compute the first slope (predictor step):
k1 ​
= f (x, y)
 Compute the predicted y value:
y predict​= y + h k
⋅ 1​
 Compute the second slope using the predicted value:
k2​= f (x + h, y predict​
)
 Compute the corrected y value:
y new ​
= y + ​ (k
⋅ 1​
+k2​
)
 Update x as:
x new ​
= x + h
 Print x, y.
3. Stop when x exceeds or reaches x n ​
.
9
• Step 4: Implement User Input
Ask the user to enter
1. Initial values x0,y0
2. Step size h
3. Final value x n
4. Pass these values to both Euler’s and Improved Euler’s method functions.
• Summary of the Algorithm
1. Take user input for x0, y0, h, xn​
.
2. Compute the numerical solution using Euler’s Method
Use the formula ynew = y + h f(x, y).
⋅
3. Compute the numerical solution using Improved Euler’s Method
Use predictor-corrector steps.
4. Print results for both methods.
Taylor Series for y(x): y(xn+1​
) = y(xn​
) + h y (x
′ n​
) + ​​
y (x
′′ n​
) + ​ y (x
′′′ n​
)+…
y(xn+1​
) ​
= y(xn​
) ​
+ h f(xn​
,yn​
) + ​ (​
+ ​​
f(xn​
,yn​
)) + y (x
′′′ n​
)
Heun’s Method: y(xn+1​
) ​
= y(xn​
) + ​[f(xn​, yn​
) + f(xn+1​,ypredict ​
)]
10
C Program
1. #include <stdio.h>
2. float f(float x, float y) // Function defining the differential equation dy/dx = f(x, y)
3. { return x + y; // Example equation: dy/dx = x + y
4. }
5. void euler_method (float x, float y, float h, float xn) // Euler's Method
6. {
printf ("nEuler's Method:n");
7. printf ("xttyn");
8. while (x <= xn)
9. {
10. printf ("%ft%fn", x, y);
11. y = y + h * f(x, y); // Euler formula
12. x = x + h;
13. }
14. }
11
15. void improved_euler (float x, float y, float h, float xn) // Improved Euler's Method (Heun's Method)
16. {
17. float y_predict;
18. printf ("nImproved Euler's Method:n");
19. printf ("xttyn");
20. while (x <= xn )
21. {
22. printf ("%ft%fn", x, y);
23. float k1 = f(x, y);
24. y_predict = y + h * k1; // Predictor
25. float k2 = f(x + h, y_predict);
26. y = y + (h / 2) * (k1 + k2); // Corrector
27. x = x + h;
28. }
29. }
12
30. int main()
31. {
32. float x, y, h, xn;
33. printf ("Enter initial value of x (x0): "); // Taking user input
34. scanf ("%f", &x);
35. printf ("Enter initial value of y (y0): ");
36. scanf ("%f", &y);
37. printf ("Enter step size (h): ");
38. scanf ("%f", &h);
39. printf ("Enter final value of x (xn): ");
40. scanf ("%f", &xn);
41. euler_method (x, y, h, xn); // Calling numerical methods
42. improved_euler (x, y, h, xn);
43. return 0;
44. }
13
Output:
14
THANKYOU

Euler's method 2.pptx c program for Euler's method c program for Euler's method

  • 1.
    1 Numerical solutions ofordinary first order differential equations by the Euler method Name : Sri Bhuvaneshwari A Register No : 34423009
  • 2.
    2 Euler’s Method x0 x1x x0 x1 x f(x) Given a differential equation y’= f (x, y) and an initial condition (x0,y0) We can approximate how the values of the function change using method f(x) error error h h
  • 3.
    3 Euler’s Method • Thederivative of a function y(x) represents the slope of the tangent line at any point (x, y): • For small h, we approximate this as: • This equation expresses the idea that the rate of change of y is given by the function f(x, y). • Rearranging the equation: • which gives us Euler’s method formula: where: yn​is the current value, xn​is the current step, h is the step size, f(xn,yn) is the slope at that point. = y(x+h) = y(x) + h f(x,y) yn+1​ = yn​ + h f(xn​ , yn​ )
  • 4.
    4 Example • Given DifferentialEquation: with initial condition y(0)=1, and we want to find y(0.2) using step size h=0.1. • Solution: So, the Euler’s method approximation for y(0.2) is 1.22. S.No x y 1 0 1 2 0.1 1.1 3 0.2 1.22 Step-by-Step Calculation: Step 1: At x0 = 0, y0 = 1, f(0,1)= 0 + 1 = 1 y1​ =y0​+ h f(x0​ ,y0​ ) =1+0.1×1 = 1.1 Step 2: At x1 = 0.1, y1 = 1.1 f(0.1, 1.1) = 0.1+1.1 = 1.2 y2​= y1​+ h f(x1​ ,y1​ ) = 1.1+0.1×1.2 = 1.22 yn+1​ = yn​ + h f(xn​ , yn​ ) f(x,y) = x+y.
  • 5.
    5 Improved Euler’s Method(Heun’s Method/ Euler-Cauchy Method) x0 x1 x x0 x1 x x0 x1 x x0 x1 x f(x) f(x) f(x) y1 y1 y0 y0  y’= f (x, y)  Initial condition:(x0,y0)  x1= x0+h , y1= y0 + h f(x0,y0)  m = f(x0+h , y0 + h f(x0,y0)  Average of the slopes: y1= yn+1 ​ = yn​+ ​[f(xn​, yn​ ) + f(xn+1​,ypredict ​ )]
  • 6.
    6 Example • Given DifferentialEquation: with initial condition y(0)=1, and we want to find y(0.2) using step size h=0.1. • Solution: • So, the Improved Euler’s method approximation for y(0.2) is 1.2421. • Comparison of Results: S.No x y 1 0 1 2 0.1 1.11 3 0.2 1.2421 Step-by-Step Calculation: Step 1: At x0 =0, y0 = 1 Predict y1 ∗ ​ : y1 ∗ ​= y​ 0 + hf(0,1) =1 + 0.1×1 = 1.1 Compute f(x1,y1 ∗ ): f(0.1, 1.1) = 0.1 + 1.1 = 1.2 Corrected y1​ : y1​ = 1 + (0.1​ (1+1.2) /2) = 1 + 0.1×1.1 = 1.11 Step 2: At x1 = 0.1, y1 = 1.11 f(0.1, 1.11) = 1.21 Predict y2 ∗ ​ : y2​ ∗ = 1.11 + 0.1×1.21 = 1.231 Compute f(x2, y2 ∗ ): f(0.2, 1.231) = 0.2 + 1.231 =1.431 Corrected y2: y2​= 1.11 + (0.1​ (1.21+1.431) /2) = 1.11 + 0.1×1.3205 = 1.2421 Method y(0.2) Approximation Euler's Method 1.22 Improved Euler's Method 1.2421  y1= y0 + h f(x0,y0)  y1=
  • 7.
    7 Algorithm for Euler’sMethod and Improved Euler Method: • Step 1: Define the Problem = Given: 1. Initial values: x0 , y0 2. Step size: h 3. Final value of x (denoted as xn​ ) • Step 2: Euler’s Method Algorithm Euler’s method is a simple numerical approach that approximates the solution by taking small steps. 1. Start with initial values (x0 , y0). 2. Repeat until x reaches xn​ 3. Compute the next y value using the formula: ynew = y + h f (x , y ) ⋅ 4. Update x as: xnew = x + h 5. Print x, y. 6. Stop when x exceeds or reaches xn​ .
  • 8.
    8 • Step 3:Improved Euler’s Method Algorithm (Heun’s Method) The Improved Euler's Method corrects the error in the basic Euler's Method by using an average slope. 1. Start with initial values (x0 ,y0). 2. Repeat until x reaches x n​ :  Compute the first slope (predictor step): k1 ​ = f (x, y)  Compute the predicted y value: y predict​= y + h k ⋅ 1​  Compute the second slope using the predicted value: k2​= f (x + h, y predict​ )  Compute the corrected y value: y new ​ = y + ​ (k ⋅ 1​ +k2​ )  Update x as: x new ​ = x + h  Print x, y. 3. Stop when x exceeds or reaches x n ​ .
  • 9.
    9 • Step 4:Implement User Input Ask the user to enter 1. Initial values x0,y0 2. Step size h 3. Final value x n 4. Pass these values to both Euler’s and Improved Euler’s method functions. • Summary of the Algorithm 1. Take user input for x0, y0, h, xn​ . 2. Compute the numerical solution using Euler’s Method Use the formula ynew = y + h f(x, y). ⋅ 3. Compute the numerical solution using Improved Euler’s Method Use predictor-corrector steps. 4. Print results for both methods. Taylor Series for y(x): y(xn+1​ ) = y(xn​ ) + h y (x ′ n​ ) + ​​ y (x ′′ n​ ) + ​ y (x ′′′ n​ )+… y(xn+1​ ) ​ = y(xn​ ) ​ + h f(xn​ ,yn​ ) + ​ (​ + ​​ f(xn​ ,yn​ )) + y (x ′′′ n​ ) Heun’s Method: y(xn+1​ ) ​ = y(xn​ ) + ​[f(xn​, yn​ ) + f(xn+1​,ypredict ​ )]
  • 10.
    10 C Program 1. #include<stdio.h> 2. float f(float x, float y) // Function defining the differential equation dy/dx = f(x, y) 3. { return x + y; // Example equation: dy/dx = x + y 4. } 5. void euler_method (float x, float y, float h, float xn) // Euler's Method 6. { printf ("nEuler's Method:n"); 7. printf ("xttyn"); 8. while (x <= xn) 9. { 10. printf ("%ft%fn", x, y); 11. y = y + h * f(x, y); // Euler formula 12. x = x + h; 13. } 14. }
  • 11.
    11 15. void improved_euler(float x, float y, float h, float xn) // Improved Euler's Method (Heun's Method) 16. { 17. float y_predict; 18. printf ("nImproved Euler's Method:n"); 19. printf ("xttyn"); 20. while (x <= xn ) 21. { 22. printf ("%ft%fn", x, y); 23. float k1 = f(x, y); 24. y_predict = y + h * k1; // Predictor 25. float k2 = f(x + h, y_predict); 26. y = y + (h / 2) * (k1 + k2); // Corrector 27. x = x + h; 28. } 29. }
  • 12.
    12 30. int main() 31.{ 32. float x, y, h, xn; 33. printf ("Enter initial value of x (x0): "); // Taking user input 34. scanf ("%f", &x); 35. printf ("Enter initial value of y (y0): "); 36. scanf ("%f", &y); 37. printf ("Enter step size (h): "); 38. scanf ("%f", &h); 39. printf ("Enter final value of x (xn): "); 40. scanf ("%f", &xn); 41. euler_method (x, y, h, xn); // Calling numerical methods 42. improved_euler (x, y, h, xn); 43. return 0; 44. }
  • 13.
  • 14.