C & MATLAB Program of Euler’s Method

Euler’s method is used for approximating solutions to certain differential equations and works by approximating a solution curve with line segments. In some cases, it’s not possible to write down an equation for a curve, but we can still find approximate coordinates for points along the curve by using simple lines. These line segments have the same slope as the curve, so they stay relatively close to it. Euler’s method is useful because differential equations appear frequently in physics, chemistry, and economics, but usually cannot be solved explicitly, requiring their solutions to be approximated. For example, Euler’s method can be used to approximate the path of an object falling through a viscous fluid, the rate of a reaction over time, the flow of traffic on a busy road, etc.

While Euler’s method can seem complicated, the idea behind it is very simple.

Suppose we have a function f(x), and we know the value of f at a point x0​, and f′(x0​). If we want to find the value of f at a point close to x, say x+h, then we can approximate f(x+h) by using the tangent line to the graph at the point (x0​  , f(x0​)), as shown in the image below. The idea is that f′(x0​) estimates how much change there is in y for each change in x; we know that x increased by h, so y should have increased by approximately hf′(x0​), so the new value should be f(x0​) + hf′(x0​).

Euler's method

Of course, as h gets larger, our approximation becomes less and less accurate. The idea behind Euler’s method is to remedy this by repeatedly using tangent line approximations; so, for example, to approximate f(x+3h) by first approximating f(x+h), then f(x+2h), and then f(x+3h). At each step, we use the slope of the curve to construct the next line segment, and this allows us to “follow” the curve with line segments, as shown in the image to the right. This number h is called the step size and measures how small the approximating segments are. In general, if you use a small step size, the accuracy of approximation increases.

Euler's Method

Euler’s Method Formula

Euler's Method Formula

Euler’s Method Solved Example

Euler's Method Solved Example

C Program

//Euler's Method
/*This code is written by Souvik Ghosh 
& modified by Debmitra Chatterjee*/

#include<stdio.h>
#include<math.h>
float f(float x, float y)
{
    return x+y+x*y;
}
void main()
{
    int i;
    float y,xi,yi,xf,h;
    printf("Enter the initial condition for x: ");
    scanf("%f",&xi);
    printf("Enter the initial condition for y: ");
    scanf("%f",&yi);
    printf("Enter the value of x for which y is required: ");
    scanf("%f",&xf);
    printf("Enter the step-width h: ");
    scanf("%f",&h);
    printf("xi\t\tyi\t\tf(xi,yi)\th*f(xi,yi)\ty+hy\n");
    printf("_______________________________________________________________________________________\n");
    while(xi<xf)
    {
        y=yi+h*f(xi,yi);
        printf("%f\t%f\t%f\t%f\t%f\n",xi,yi,f(xi,yi),h*f(xi,yi),y);
        yi=y;
        xi=xi+h;
    }
    printf("%f\t%f\n",xi,yi);
    printf("_______________________________________________________________________________________\n");
    printf("The value of y is %f\n\n",y);
}

Output Terminal

MATLAB Program

prompt = "Please enter the initial condition for x: ";
xi=input(prompt);
prompt= "Please enter the initial condition for y: ";
yi=input(prompt);
prompt=("Enter the value of x for which y is required: ");
xf=input(prompt);
prompt=("Enter the step-width h:");
h=input(prompt);
 while(xi<xf)
        y=yi+h*func(xi,yi);
        yi=y;
        xi=xi+h;
    
 end
 y

function f =func(x,y)
f=x+y+x*y;
end

Output

Related Posts

Artificial Intelligence Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Artificial Intelligence Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Mixed Signal Design Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Mixed Signal Design Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Fiber Optic Communication Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Fiber Optic Communication Suggestions! Elevate Your Learning Experience and Excel with Confidence.

Cyber Security Suggestions

Unlock Success in Your MAKAUT Semester Exam with These Empowering Cyber Security Suggestions! Elevate Your Learning Experience and Excel with Confidence.

MAKAUT 7th Semester Examination Questions – 2023

Mobile Communication and Networks (PE-EC701C) Neural Network and Fuzzy Logic Control (PE-EC702C/PEROB701B) Principles of Management (HS-HU701)

CSS Cheatsheet

Transform your web design game with my CSS cheatsheet! Master the art of styling, dive into essential properties and selectors, and create visually stunning websites effortlessly. Whether…

Leave a Reply

Your email address will not be published. Required fields are marked *