Solve Euler Method in C++ for Beginners

Click For Summary
SUMMARY

The discussion focuses on implementing the Euler Method in C++ for solving differential equations. A user sought guidance on creating a version that utilizes void and float functions to accept user input for calculations. The provided code demonstrates the Euler Method with a function definition for the slope, specifically using the equation dy/dx = 2x. Key corrections included properly utilizing the slope variable and adjusting the for loop to accurately compute the next y-value based on the initial conditions.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with differential equations and numerical methods
  • Knowledge of function definitions and variable scopes in C++
  • Basic input/output operations in C++ using cin and cout
NEXT STEPS
  • Implement void functions in C++ to encapsulate the Euler Method logic
  • Explore different numerical methods for solving differential equations, such as Runge-Kutta
  • Learn about error analysis in numerical methods to assess the accuracy of results
  • Investigate advanced C++ features like templates for generic programming in numerical computations
USEFUL FOR

Beginner programmers, students studying numerical methods, and anyone interested in implementing mathematical algorithms in C++. This discussion is particularly beneficial for those looking to understand the Euler Method in a practical coding context.

Demjan
Messages
1
Reaction score
0
Summary: Problem with Euler Method in C++

Hello, I have a very difficult problem for me (a beginner in programming) how to make the version of the euler method presented in c ++ with the void, float functions, so that the program will calculate from the data that I enter during the program.
Code:
#include<iostream>
#include <string>
using namespace std;

double f(double x, double y) { return x + y; }

int main()
{
 double x0, y0, xn, h, yn, slope;
 int i, n;

 count<<"Warunki początkowe: "<< endl;
 count<<"x0 = ";
 cin>> x0;
 count<<"y0 = ";
 cin >> y0;
 count<<"Enter calculation point: xn = ";
 cin>>xn;
 count<<"Enter number of steps: ";
 cin>> n; h = (xn-x0)/n;

 count<<"\nx0\ty0\tslope\tyn\n";
 count<<"------------------------------\n";

 for(i=0; i < n; i++)
 {
  nachyl = f(x0, y0);
  yn = y0 + h * slope;
  count<<  x0 <<"\t"<<  y0 <<"\t"<<  slope <<"\t"<<  yn << endl;
  y0 = yn;
  x0 = x0+h;
 } count<<"\nValue of y at x = "<< xn<< " is " << yn;

 return 0;
}
 
Last edited by a moderator:
Physics news on Phys.org
Demjan said:
Summary: Problem with Euler Method in C++

Hello, I have a very difficult problem for me (a beginner in programming) how to make the version of the euler method presented in c ++ with the void, float functions, so that the program will calculate from the data that I enter during the program.
Code:
#include<iostream>
#include <string>

using namespace std;

double f(double x, double y) { return x + y; }

int main()
{
 double x0, y0, xn, h, yn, slope;
 int i, n;

 count<<"Warunki początkowe: "<< endl;
 count<<"x0 = ";
 cin>> x0;
 count<<"y0 = ";
 cin >> y0;
 count<<"Enter calculation point: xn = ";
 cin>>xn;
 count<<"Enter number of steps: ";
 cin>> n;

 h = (xn-x0)/n;

 count<<"\nx0\ty0\tslope\tyn\n";
 count<<"------------------------------\n";

 for(i=0; i < n; i++)
 {
  nachyl = f(x0, y0);
  yn = y0 + h * slope;
  count<<  x0 <<"\t"<<  y0 <<"\t"<<  slope <<"\t"<<  yn << endl;
  y0 = yn;
  x0 = x0+h;
 }

 count<<"\nValue of y at x = "<< xn<< " is " << yn;

 return 0;
}
What is your question? You said "how to make the version of the euler method presented in c ++ with the void, float functions" -- what do you mean by that?

In the Euler method, you are given a differential equation of the form dy/dx = f(x, y), with initial condition ##y(x_0) = y_0##.

Try your code out on this differential equation: ##dy/dx = 2x##, with initial condition y(1) = 1. Here the function to use is ##f(x, y) = 2x + 0y##.
 
Last edited:
  • Like
Likes   Reactions: Delta2
With a minor change in the for loop in your code I was able to get it to work.

You have a variable named slope that you didn't use. Your function f is the slope. The example differential equation I'm solving is dy/dx = 2x + 0y, so f(x, y) = 2x + 0y, or f(x, 0).
Here is my definition for f:
Code:
double f(double x, double y) { return 2 * x ; }
Since y doesn't appear in the return value, this function is called using f(x, 0).

Here is my for loop:
Code:
for (i = 0; i < n; i++)
{
    slope = f(x0, 0);         
    yn = y0 + h * slope;
    count << x0 << "\t" << y0 << "\t" << slope << "\t" << yn << endl;
    y0 = yn;
    x0 = x0 + h;
}

Using dy/dx = 2x and a starting point of (1, 1), and an ending x-value of 1.1 and 10 steps, I get a y-value of 1.209, which is pretty close to the exact value of 1.21.
Euler's method is probably the least accurate of methods for solving differential equations, so my result isn't so far off.
 
  • Like
Likes   Reactions: Delta2

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 8 ·
Replies
8
Views
8K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
28K