Reading a mathematical function for bisection method

Hello everyone. I am a beginner level C++ programmer having decent experience in MATLAB. I normally use MATLAB for all my programs, but recently have to learn C++. I want to write a program where the user inputs an equation in one variable along with two initial guesses. The program needs to find its roots:
What I am looking to program:
1. user first needs to input the variable in the equation
2. then he inputs the equation. The program must be able to identify the equation and wherever it has the variable, it needs to substitute the initial guesses to find the value of the equation and further apply the bisection algo. ( in MATLAB we use absolute functions to carry out this task eg: if eqn is 2x^2-4 we write '@(X) 2*X^2-4;')

My problem is basically how do you identify the equation, as in the position of the numbers( coefficients and powers) and the variable (say x).
What i do:
1. input the eqn as string, divide the string with + or - as the divider. In the divided string identify the numbers which are before the variable as co-efficients and after the variable as power. Further build the equation by replacing the variable x with the initial guesses.

Is there any better way to do it?
P.S.: I ask the user to enter the equation in this format : "2x3+4x2-5x+5"
Thank You
For a general function this is not possible at run time - you would have to code the function and then compile.

However, if you are restricting your function to certain specific types - and you seem to be restricting to polynomials - then you simply have to input what defines it. In the case of a polynomial this will simply be its degree (i.e. the highest power) and the coefficients.

So, for your cited example:
Input degree: 3
Input (3+1) coefficients, starting from the highest power: 2 4 -5 5

The name of the variable (x) is irrelevant: it is just a dummy variable; i.e. "when I evaluate this polynomial I am simply going to replace x by whatever point I choose to evaluate it at".
Topic archived. No new replies allowed.