Solved

Need help with source code; Euler's method

Posted on 1997-04-04
3
475 Views
Last Modified: 2010-04-10
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
0
Comment
Question by:Ben Key
  • 2
3 Comments
 

Author Comment

by:Ben Key
ID: 1162565
I beleive the problem may be as simple as incorrect syntax in
my for loop.  It may also be improper calling of the function
euler.
0
 

Accepted Solution

by:
aleroy earned 50 total points
ID: 1162566
First of all, entering an equation in a float is not right way
to evaluate this equation.
For that, you must use (or program) a parser, wich is certainly
not a piece of cake.
To debug your program, I've inserted the equation (y'=x+y) in
the right place directly. If you don't intend to get a expression parse working, you can do in this way (but you will have to recompile for each different equation you plan to analyse.

All the others problems where syntaxical.
Here is the revised code :
#include <math.h>
#include <iostream.h>
#include <iomanip.h>

void euler ();
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;
h=(f-x)/n;
euler();
cout << x << " " << y << endl;
}
void euler ()
{
for(int i=0;i<n;i++) //there was a ; here, wich is bad ...
{
y+=h*(x+y);
x+=h;
}
}

That's about it.

You should avoid global variable and use parameters of function
correctly (I haven't done it because there was no real need, it's
just for the beauty of the code)






0
 

Expert Comment

by:aleroy
ID: 1162567
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.



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 107
C++ to C# code conversion issue 4 100
C++ Class Serialization, Encapsulation and Formatted Output 8 56
max float value 3 26
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now