Inserting math functions from GUI


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++

So this is the main code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

#include "bisection.h"
#include "ui_bisection.h"
#include <QFile>
#include <QString>
#include <math.h>
#include <algorithm>


bisection::bisection(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::bisection)
{
    ui->setupUi(this);

}

bisection::~bisection()
{
    delete ui;
}



//void bisection::on_pushButton_clicked()
//{



//}




float f(float x);



void bisection::on_getJ_clicked()
{



    //
   QString   xla = ui->xltxt->text();
   QString   xda = ui->xdtxt->text();
   QString   itexta = ui->itext->text();


//   QString   jednadzbaa = ui->jednadzba->toPlainText();
//   f2 = jednadzbaa.toFloat();


//ui->xltxt->QString::toFloat();
//ui->xdtxt->QString::toFloat();
//ui->itext->QString::toFloat();
//ui->jednadzba->QString::toFloat();
int i = 0;
float xl = xla.toFloat();
float xd = xda.toFloat();
int imax = itexta.toFloat();
float to = 1e-12;
float x3, err;




while ((i < imax) || (err < to) || (f(x3) == 0)) {
        x3 = (xl + xd) / 2;
        err = abs(xl - xd);


        if( f(xl) * f(x3) < 0) {
            xd = x3;
        }
        else {
            xl = x3;
        }

        i++;
    }




    QString RJE = ui->rjtxt->text();

    RJE.append(QString("%1").arg(x3)); //Float pretvara u string!
    ui->rjtxt->setText(RJE);

}


float bisection::f(float x)

{

    float f2;
    QString jednadzbaa;



    jednadzbaa = ui->jednadzba->text();
    f2 = jednadzbaa.toFloat();

            return f2;

}


void bisection::on_pushButton_2_clicked()
{
    ui ->itext->setText("");
    ui ->xdtxt->setText("");
    ui ->xltxt->setText("");
    ui ->jednadzba->setText("");
    ui ->rjtxt->setText("");

}
Sooooooo, are you just trying to find a bisection of something?
Well, I don't get what you're program is about, but maybe this is what it is.

Here's a code that does the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#define PI 3.141592654
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    double x = 0;
    cout << "Equation:  f(x) = x^3 - cos(x) - x - 3" << endl;
    cout << "Enter x: ";
    cin >> x;
    cout << endl;
    double result2 = cos(x*PI/180);
    double result = x * x * x - result2 - x - 3;
    printf("The result of f(x) = x^3 - cos(x) - x - 3 with %lf as x is: %lf\n",x, result);
    system("PAUSE");
    return 0;
}
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.

I'll explain the code by commenting what is what:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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.

here is the header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#ifndef BISECTION_H
#define BISECTION_H

#include <QMainWindow>

namespace Ui {
class bisection;
}

class bisection : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit bisection(QWidget *parent = 0);
    ~bisection();

private slots:
   // void on_pushButton_clicked();

    void on_getJ_clicked();

    void on_pushButton_2_clicked();

double f(double);



private:
Ui::bisection *ui;

};

#endif // BISECTION_H



Here is the UI:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>bisection</class>
 <widget class="QMainWindow" name="bisection">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>256</width>
    <height>500</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>bisection</string>
  </property>
  <property name="statusTip">
   <string extracomment="123"/>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout_3">
    <item>
     <widget class="QLabel" name="stanje">
      <property name="text">
       <string/>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLineEdit" name="jednadzba">
      <property name="font">
       <font>
        <kerning>false</kerning>
       </font>
      </property>
     </widget>
    </item>
    <item>
     <widget class="Line" name="line_2">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_2">
        <item alignment="Qt::AlignRight">
         <widget class="QLabel" name="label_3">
          <property name="text">
           <string>Max iterations =</string>
          </property>
         </widget>
        </item>
        <item alignment="Qt::AlignRight">
         <widget class="QLabel" name="label_2">
          <property name="text">
           <string>Right side =</string>
          </property>
         </widget>
        </item>
        <item alignment="Qt::AlignRight">
         <widget class="QLabel" name="label">
          <property name="text">
           <string>Left side =</string>
          </property>
         </widget>
        </item>
        <item alignment="Qt::AlignRight">
         <widget class="QLabel" name="label_4">
          <property name="text">
           <string>Result =</string>
          </property>
         </widget>
        </item>
       </layout>
      </item>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QLineEdit" name="itext"/>
        </item>
        <item>
         <widget class="QLineEdit" name="xdtxt"/>
        </item>
        <item>
         <widget class="QLineEdit" name="xltxt"/>
        </item>
        <item>
         <widget class="QLineEdit" name="rjtxt">
          <property name="readOnly">
           <bool>true</bool>
          </property>
         </widget>
        </item>
       </layout>
      </item>
     </layout>
    </item>
    <item>
     <widget class="Line" name="line">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="getJ">
      <property name="text">
       <string>Calculate</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="pushButton_2">
      <property name="text">
       <string>Clear</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="pushButton_3">
      <property name="text">
       <string>Exit Program</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QLabel" name="label_5">
      <property name="text">
       <string>Made by Aleksandar Milacic, B.Eng.</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>256</width>
     <height>25</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuBisection">
    <property name="title">
     <string>Bisection</string>
    </property>
   </widget>
   <addaction name="menuBisection"/>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections>
  <connection>
   <sender>pushButton_3</sender>
   <signal>clicked()</signal>
   <receiver>bisection</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>246</x>
     <y>410</y>
    </hint>
    <hint type="destinationlabel">
     <x>255</x>
     <y>423</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>


:(

Those huge programs make me sad...
Can you explain to me more clearly what are you are trying to do? please
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.

Hope I was clear?

is ^ a function? I never tried that before
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 )
I guess I can make a function that '*' something how many times you wanted
try another interpreted language like Python. Use Python's eval() function.

MS VS 2010 has IronPython plugin which you can use to design GUI. Or PyQt, wxPython...
kewl
Topic archived. No new replies allowed.