Ben Key
asked on
Need help with source code; Euler's method
The following is the beginnings of a program which I am currently attempting to write. Its purpose is to do an numerical approzimation for a linear first order differential equation using euler's method. It needs a lot of work. The problem is mainly that it only goes through one step of the analysis. Of course there may be several more problems. If anyone has an idea on how to get it to work, I would appreciate a response.
// Program -- Euleran's
// Uses Euler's meathod to approximate the solution of the differential
// equation y'=f(x,y) for a given interval.
//header files
#include <math.h>
#include <iostream.h>
#include <iomanip.h>
float euler (float);
float x,y,f,equation,h;
int n;
void main()
{
cout.setf(ios::fixed, ios::floatfield); //set up floating point
cout.setf(ios::showpoint); // output format
cout << "Enter the initial value for x: " << endl;
cin >> x;
cout << "Enter the initial value for y: " << endl;
cin >> y;
cout << "Enter the final value for x: " << endl;
cin >> f;
cout << "Enter the number of steps for which you wish the " << endl
<< "function to be analyzed: " << endl;
cin >> n;
cout << "Enter the first order differential equation which you " << endl;
cout << "wish to analyze:" << endl;
cin >> equation;
h=(f-x)/n;
euler (equation);
cout << x << " " << y << endl;
}
float euler (float equation)
{
for(int i=1;i<=n;i++);
{
y=y+h*equation;
x=x+h;
return x,y;
}
}
Thank you,
Ben Key
// Program -- Euleran's
// Uses Euler's meathod to approximate the solution of the differential
// equation y'=f(x,y) for a given interval.
//header files
#include <math.h>
#include <iostream.h>
#include <iomanip.h>
float euler (float);
float x,y,f,equation,h;
int n;
void main()
{
cout.setf(ios::fixed, ios::floatfield); //set up floating point
cout.setf(ios::showpoint);
cout << "Enter the initial value for x: " << endl;
cin >> x;
cout << "Enter the initial value for y: " << endl;
cin >> y;
cout << "Enter the final value for x: " << endl;
cin >> f;
cout << "Enter the number of steps for which you wish the " << endl
<< "function to be analyzed: " << endl;
cin >> n;
cout << "Enter the first order differential equation which you " << endl;
cout << "wish to analyze:" << endl;
cin >> equation;
h=(f-x)/n;
euler (equation);
cout << x << " " << y << endl;
}
float euler (float equation)
{
for(int i=1;i<=n;i++);
{
y=y+h*equation;
x=x+h;
return x,y;
}
}
Thank you,
Ben Key
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What is a parser ?
Suppose your equation is in a character string.
For your specific problem, a parser is a piece of program
that will analyze the string, recognize the variables and the
operator (and parenthesis and so on).
It will so create a data structure (generally a tree) that
will allow your equation to be evaluated for given value of variables.
As most language like C and C++ are compiled, once they run,
their variables are nothing more than memory adresses without name. This is the thing that prevent from doing like you've done
in the question.
If you want easy evaluations of runtime entered equations, you
will have to learn a language such as miranda or LISP.
A good book to read about data structures (not specially trees or parsing) :
E. Horowitz, S. Sahni, and D. Mehta, Fundamentals of Data Structures in C++, W.H.
Freeman & Co, 1995.
Suppose your equation is in a character string.
For your specific problem, a parser is a piece of program
that will analyze the string, recognize the variables and the
operator (and parenthesis and so on).
It will so create a data structure (generally a tree) that
will allow your equation to be evaluated for given value of variables.
As most language like C and C++ are compiled, once they run,
their variables are nothing more than memory adresses without name. This is the thing that prevent from doing like you've done
in the question.
If you want easy evaluations of runtime entered equations, you
will have to learn a language such as miranda or LISP.
A good book to read about data structures (not specially trees or parsing) :
E. Horowitz, S. Sahni, and D. Mehta, Fundamentals of Data Structures in C++, W.H.
Freeman & Co, 1995.
ASKER
my for loop. It may also be improper calling of the function
euler.