# Solve Euler Method in C++ for Beginners

• Demjan
In summary, your problem is that you need to use the slope variable in your function f in order to calculate y at x.
Demjan
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;

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

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

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

return 0;
}

Last edited by a moderator:
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;

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

h = (xn-x0)/n;

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

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

cout<<"\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:
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;
cout << 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.

Delta2

## 1. What is the Euler Method in C++?

The Euler Method is a numerical method used to approximate the solution of a differential equation. It is commonly used in scientific and engineering applications to solve problems that cannot be solved analytically.

## 2. How does the Euler Method work in C++?

The Euler Method works by dividing a continuous function into smaller segments, and then approximating the value of the function at each segment using a tangent line. This process is repeated until the desired level of accuracy is achieved.

## 3. What are the advantages of using the Euler Method in C++?

One advantage of using the Euler Method in C++ is that it is a relatively simple and straightforward method to implement. It also provides a good approximation of the solution, especially for simple differential equations.

## 4. What are the limitations of the Euler Method in C++?

One limitation of the Euler Method is that it can only provide an approximate solution, which may not be accurate enough for more complex problems. It also requires a small step size to achieve a higher level of accuracy, which can be computationally expensive.

## 5. How can beginners learn to implement the Euler Method in C++?

Beginners can learn to implement the Euler Method in C++ by first understanding the basic concepts of differential equations and numerical methods. They can then practice coding simple examples and gradually move on to more complex problems. There are also many online resources and tutorials available to help beginners learn and improve their skills.

Replies
2
Views
1K
Replies
6
Views
3K
Replies
13
Views
2K
Replies
8
Views
8K
Replies
23
Views
2K
Replies
2
Views
1K
Replies
8
Views
1K
Replies
13
Views
2K
Replies
3
Views
1K
Replies
5
Views
2K