Hi guys,
I'm new to C++, so go a bit easy on me. I have a problem with entering math functions in my Bisection method algorithm program. I just don't know how can I make the function that I enter in my GUI app to go from the GUI to the loop and find the root.
Any Ideas for this?
As far as I googled I only find codes that you need to pre-enter a function in the double/float.
For example:
I have a function f(x) = x^3 - cos(x) - x - 3;
and I want to enter that function trough the GUI i made in c++
Thanks for the replays guys.
This program is basically a GUI where you can enter the number of iterations, left and right start point and a function that you want to find the root with the Bisection method.
So the problem is that that I don't know how to enter the function from the GUI textbox.
float f(float x);
void bisection::on_getJ_clicked() //when this button in clicked it takes the function given by the user in the text box, number of iterations and left and right starting point of the Bisection.
{
//
QString xla = ui->xltxt->text(); //this is the left starting point taken out of the text box as string
QString xda = ui->xdtxt->text(); //this is the right starting point taken out of the text box as string
QString itexta = ui->itext->text(); //this is the number of iterations taken out of the text box as string
int i = 0; // starting iteration
float xl = xla.toFloat(); //transforms string to float from the text box
float xd = xda.toFloat();
int imax = itexta.toFloat();
float to = 1e-12;
float x3, err;
while ((i < imax) || (err < to) || (f(x3) == 0)) { //iteration loop
x3 = (xl + xd) / 2; // Bisection formula
err = abs(xl - xd); //error
if( f(xl) * f(x3) < 0) {
xd = x3;
}
else {
xl = x3;
}
i++;
}
QString RJE = ui->rjtxt->text(); //defines the result text box
RJE.append(QString("%1").arg(x3)); //Float to string!
ui->rjtxt->setText(RJE); //result output to text box prom the loop!
}
float bisection::f(float x) // defines the f(x) so the user can input his own function from GUI *** Somewhere here is the problem!
{
float f2;
QString jednadzbaa; // jednadzba is the variable that the user inputs as the function for example cos(x) - x - 3
jednadzbaa = ui->jednadzba->text(); //sends the math function to the loop
f2 = jednadzbaa.toFloat();
return f2;
}
So the thing is when I enter a function from the GUI I want it to go to the loop.
As far as I seen when I was googling around ppl always define the function in the code. I want the user to define any function they want from the GUI.
I want the user that uses this program to enter some mathematical function from the GUI and that function needs to enter the Bisection loop.
That is basically it.
Problem is, when I define that math function like this:
float f(float x) {
float fx = x^3 + x -14; // I want this part to be entered by user from a text box from GUI. That is the whole thing.
return fx;
}
I want that fx function to be called in from ui -> nameOfGui -> TextBoxMathFunction -> Text();
Problem is calling that code function in to the main function (in my case void bisection::on_getJ_clicked()) seems impossible.
C++ is a compiled language so you can't directly do that.
I see two possibilities:
- you write an interpreter (see http://root.cern.ch/ for an example of a C interpreter)
- you create the code of a C++ function from the user input and compile/link/execute it on the fly( there are lightweight compilers that can be used, for example TCC )