NEED HELP FOR SCHOOL PROJECT

Hi, please help me.
How can i change this program where in the user could input any equation to solve it?

//Eulers Method to solve a differential equation
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y) //function for defining dy/dx
{
double a=x+y; //dy/dx=x+y
return a;
}
int main()
{
int n;
double x0,y0,x,y,h; //for initial values, width, etc.
cout.precision(5); //for precision
cout.setf(ios::fixed);
cout<<"\nEnter the initial values of x and y respectively:\n"; //Initial values
cin>>x0>>y0;
cout<<"\nFor what value of x do you want to find the value of y\n";
cin>>x;
cout<<"\nEnter the width of the sub-interval:\n"; //input width
cin>>h;
cout<<"x"<<setw(19)<<"y"<<setw(19)<<"dy/dx"<<setw(16)<<"y_new\n";
cout<<"----------------------------------------------------------\n";
while(fabs(x-x0)>0.0000001) //I couldn't just write "while(x0<x)" as they both are floating point nos. It is dangerous to compare two floating point nos. as they are not the same in binary as they are in decimal. For instance, a computer cannot exactly represent 0.1 or 0.7 in binary just like decimal can't represent 1/3 exactly without recurring digits.
{
y=y0+(h*df(x0,y0)); //calculate new y, which is y0+h*dy/dx
cout<<x0<<setw(16)<<y0<<setw(16)<<df(x0,y0)<<setw(16)<<y<<endl;
y0=y; //pass this new y as y0 in the next iteration.
x0=x0+h; //calculate new x.
}
cout<<x0<<setw(16)<<y<<endl;
cout<<"The approximate value of y at x=0 is "<<y<<endl; //print the solution.
return 0;
}
Last edited on
input any equation to solve it?

In other words, how to parse text and create a data structure (probably a tree) that supports evaluation of the represented formula?

If the string is:
-x*x + 42*x

How to recognize that the "x" is a variable, for which a value should be substitute during evaluation, the "42" is a literal constant value and the - * + are operators that have precedence (and one of them in unary, rather than binary)?

That is not a small, trivial task.
Sorry, i dont get the idea. I'm mechanical engineering student but my prof required us to make a program. That sucks.
@Kenneth14,
I'm not sure that I understand your question.

You are solving dy/dx=f(x,y) by forward Euler. Every different f(x,y) will require you to program your function to return that value of f. You can't input an expression for f at runtime in c++ (as you could, for example, with eval in javascript).

But surely it isn't that onerous to change one line of the program:
double a=x+y;
whenever you need to code a different differential equation.
input any equation to solve it?
^^ This is huge. There is a reason things like maple have a big price tag. Even something simple, like log base 37 of x, you would have to recognize what it wants and then do a change of base log series to resolve it because c++ does not support arbitrary bases, just a few common ones.

input a small subset of all possible equations is much easier, eg maybe you support basic polynomials and the user just inputs the coefficients, that is very easy to do. Have them input in reverse and they don't need to tell you the order, and have them put in zeros where the terms are dropped as well, and you have made the user do all the work for you :)

what exactly are you required to support?
Last edited on
Topic archived. No new replies allowed.