Write a program to demonstrate Euler's Modified Method in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (n*k)Where k is inner collection of iterationsSpace Complexity: O (1)Open Source: MIT
1
2
3
        /*Program to execute Euler's Modified method*/
4
	#include <stdio.h>
5
	#include <conio.h>
6
	#include <math.h>
7
	float dif(float x, float y)
8
	{
9
	    return 3 * x + (y / 2);
10
	}
11
	void main()
12
	{
13
	    float x, h, x0, x1, y0, y1, aerr, yc, yc1;
14
	    printf("Enter the values of x, h, x0, x0, y0, aerr: ");
15
	    scanf("%f%f%f%f%f", &x, &h, &x0, &y0, &aerr);
16
	    x1 = x0;
17
	    y1 = y0;
18
	    while (x1 < x)
19
	    {
20
	        yc = y1;
21
	        y1 += h * dif(x1, y1);
22
	        yc1 = yc + h * (dif(x1, yc) + dif(x1 + h, y1)) / 2;
23
	        while (fabs(yc1 - y1) > aerr)
24
	        {
25
	            y1 = yc1;
26
	            yc1 = yc + h * (dif(x1, yc) + dif(x1 + h, y1)) / 2;
27
	        }
28
	        x1 += h;
29
		printf("\n Values of y = %f, when x = %f", yc1, x1);
30
	    }
31
	    getch();
32
	}
33
            
34
        

Output

1
2
            
3
            Enter the values of x, h, x0, y0, aerr: 0.3 0.1 0 1 0.0001
4
	
5
	Values of y = 1.066666, when x = 0.100000
6
	Values of y = 1.167510, when x = 0.200000
7
	Values of y = 1.304294, when x = 0.300000
8
9
            

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run Euler's Modified Method program in C-Language?

To run Euler's Modified Method program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More About Euler’s Modified Method

Discovered by: German Mathematician Karl Heun

Discovered Year: 1900

Global Error: O(h2)

Local Truncation Error: O(h3)

Method Type: 2nd-Order Predictor-Corrector Method

To solve the biggest problem of Euler’s Method the fixed slope value during the complete steps (h) Euler’s Modified Method came into the picture. Also known as Heun’s Method.

How does Euler’s Modified Method work?

Euler’s Modified Method doesn’t rely directly on a slope. In fact, to get the average slope, the predictor-corrector approach is used.

  1. First of all, the predicted value
    Image
    (known as the predictor step) is calculated with the help of the normal Euler method.
  2. Then the average slope is calculated based on the initial and new rough values.
  3. The new point is created with the help of the average slope and a small step (h) to compute the corrected point.

    Predictor Step (Rough Estimation):

    Image
    Corrector Step (Refined Calculation using Average Slope):
    Image
    Where: h = Step size (xn+1xn)
    Image
    = Initial slope at the current point (xn,yn)
    Image
    = Predicted value of
    Image
    at xn+1(calculated via standard Euler’s Method)
    Image
    = Estimated slope at the predicted pointYn+1= Final corrected value of
    Image
    at xn+1

Why use Euler’s Modified Method?

Because Euler’s Modified Method uses the average of the slopes, this method is much more accurate than the normal Euler’s Method.

Real-life use cases of Euler’s Modified Method

  1. Thermal & Heat Transfer simulations: To solve the differential equations for the change in temperature of the engine cylinder or metal rod
  2. Circuit analysis: To get the decay and charge in RC and RL circuits in electrical engineering.
  3. Aerodynamics and Trajectory: To better estimate the speed of the missile or projectile in the air.

Limitations of Euler’s Modified Method

  1. More Calculations: Due to calculating the slope 2 times in each method, this method requires more calculations compared to the simple Euler method.
  2. Slow in Complex Multi-Step Problems: If the equations are complex, it requires more steps than Runge-Kutta, which takes more time to generate the result, so it is slower for complex equations

Alternate Methods

  • The Runge-Kutta 4th Order (RK4) method considers the average slope at 4 different points in each step, which causes a more accurate result, and it takes fewer steps to solve a problem for complex equations than Euler’s Modified method.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.