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

Tested on: Turbo C++ Version 4.0Time Complexity: O (n)Space Complexity: O (1)Open Source: MIT
1
2
          
3
        /* Program to execte Euler's Method*/
4
	#include<stdio.h>
5
	#include<conio.h>
6
	#include<math.h>
7
	float dif ( float x,float y )
8
	{
9
	    return x+y;
10
	}
11
	void main()
12
	{
13
	    float x  ,h,x0,x1,y0,y1;
14
	    printf("Enter the values of x,h,x0,y0: ");
15
	    scanf("%f%f%f%f", &x, &h, &x0,&y0);
16
	    x1=x0;
17
	    y1=y0;
18
	    while(x1 < x) {
19
	        y1 += h*dif(x1,y1);
20
	        x1 += h;
21
	        printf("\n Values of y=%f, when x=%f",y1,x1);
22
	    }
23
	    getch();
24
	}
25
            
26
            

Output

1
2
            
3
            Enter the values of x,h,x0,y0: 1 0.1 0 1
4
	Values of y=1.100000, when x=0.100000
5
	 Values of y=1.220000, when x=0.200000
6
	 Values of y=1.362000, when x=0.300000
7
	 Values of y=1.528200, when x=0.400000
8
	 Values of y=1.721020, when x=0.500000
9
	 Values of y=1.943122, when x=0.600000
10
	 Values of y=2.197434, when x=0.700000
11
	 Values of y=2.487178, when x=0.800000
12
	 Values of y=2.815895, when x=0.900000
13
	 Values of y=3.187485, when x=1.000000
14
15
            

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 Method program in C-Language?

To run Euler's 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 Method

Discovered by: Swiss mathematician Leonhard Euler

Discovered year: 1768

Euler’s Method is used to solve ordinary differential equations (ODEs) when the initial values are given. These initial values are known as an initial value problem.

How does Euler’s Method work?

  1. First of all, the derivative (slope) of the curve is calculated.
  2. The small steps(h) are taken using the calculated derivatives or tangent line.
  3. Calculate the next point value based on the tangent line.

    Below is the formula

    Yn+1= yn+ h⋅f(xn,yn)

    Where h is the step size and f(xn, yn) is the slope.

Why is Euler’s Method used?

  • Physics & Game Physics (Simple motion): To calculate the position and velocity of an object when the applied force continuously changes
  • Estimate the growth Rate: To calculate the growth rate of any asset, like in the economy or population of a city or country; then this method is used.
  • Chemical Kinetics: To track the concentration of reactants in a chemical reaction.

Limitations of Euler’s Method

  • Lack of Accuracy (High error): Due to a straight line (tangent line) being drawn, the actual curve points are not covered; that is why there is a high error.
  • More calculations for accuracy: To get a more accurate result, the size of the step (h) should be small, which causes more calculations that will be time-consuming (slow speed).
  • Local Truncation Error: In each step, Euler's method uses a straight line (tangent) to approximate a curve, which introduces a local truncation error.
  • Global Truncation Error: As these step-by-step errors accumulate over the entire interval, the global truncation error becomes, making it less accurate compared to higher-order methods like RK4.

Alternate Methods

  1. Improved Euler’s Method (Heun’s Method): In this method, the slope is not just calculated at the beginning but also predicts the next point and averages the slopes at the start and end points, which is why it gives more accuracy.
  2. Runge-Kutta 4th-Order (RK4) method : It is the most popular and reliable method to solve numerical differential equations. It checks 4 different points in each step; that is why it gives a very accurate result.
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.