multi variable solving

I am Writing a program that solves some 1 and 2 dimensional motion equations.
there are five variables, three are known, two need to be evaluated.
the unknown depend on the known variables, and the known are user inputs.
is there a faster or more efficient way to do this than to check each variable with an if-statement, and write a function for each case?
i would appreciate any help in the matter.
Last edited on
That depends on the equations; in some cases you can solve for them mathematically and just hard-code the solving mechanics in your program. In other cases you would have to use algorithms to efficiently find intersects.
the equations are kinematic (some sort of drivitive, i don't know, my math teacher gave them to me).
here is one of them
Vf = Vi+ AT
where Vf is velocity final
Vi is velocity initial
A is acceleration and T is time
so, if I start at 0 meters a second and accelerate at 5 m/s2 for 5 seconds, i end up going 25 m/s.
what I am trying to write is that equation , but the user can enter any combination of known values.so the code i have right now checks each input for what is is(known/unknown),then runs the right funtion for the know values.
this approach to the problem has taken up over 200 lines of code, and i would like to find a better way to do it if there is one
Last edited on
I think I understand what you want;

1. User gives known values for a predetermined equation that can not change
2. You have to solve for the coefficients of the variables in the equation

Is this correct? If so, I'm sorry for leading you down completely the wrong path. This is much more complex than using an expression parser, and I don't actually know the maths behind it. hopefully someone who does will post here.
yes and no. sorry if i am not clear, this is my first forum posting.
the equation that runs is dependent on what the user inputs.
say i have three variables, a b and c.
a= b+c
but, the user gives me a and c
so i have to create b= a-c
the code i have is like the example, if it makes sense
the user can give any 2 of the three, and the program has to pick the right equation to run for the user to get the variable they do not know.
Last edited on
Ahhh, whoops. I got this thread confused for another similar thread - my mistake!

So, your program has to solve equations. Are they predetermined and the user just indicates which variables they are telling you about, or does the user tell you the formula as well?
the user just inputs the variables, the equations are predetermined,like how
a= b+c
is equal to
b= a-c
is equal to
c=a-b
Last edited on
Ah, so really you just need to map several sets of variables to functions to solve, given the variables, right? What does the user input look like?
vi= "string input"
vf= "string input"
D(delta)X= "string input"
a= "string input"
t= "string input"
the program checks for what inputs are null or n/a by using a few nested if-statements and a stringstream converter(see bottom)
the nested if statements take up a lot of room and i would like to downsize my code and make it more professional

the converter
1
2
3
4
5
6
7
double stringconvert (string convert)
{
       double num;
       stringstream ss(convert);
       ss >> num;
       return num;
}

i am sincerely sorry for any misunderstanding my posts are giving you, it all is perfectly clear in my head, but you are not in my head(i hope :).
and thank you for taking time to help me
So, if I understand correctly, the way you are doing it is this? (Example)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string x, y, z;
std::cout << "x = " << std::flush;
std::getline(std::cin, x):
std::cout << "y = " << std::flush;
std::getline(std::cin, y):
std::cout << "z = " << std::flush;
std::getline(std::cin, z):

if(x.empty()) //...
if(y.empty()) //...
if(z.empty()) //...

int X, Y, Z;
std::istringstream(x) >> X;
std::istringstream(y) >> Y;
std::istringstream(z) >> Z;
Something along these lines?
i am not understanding what you are writing. so i'll post my code here to see if you can understand what i am doing
just as a note, my question was of better functionality and streamlining, so this code is probably going to be the most inefficient you have seen.
the main part of compressing i want to do in in int 1d_equation

part A

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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <cstdlib>
using namespace std;
//function naming
void choose_equation_1d_or_2d ();
int _1d_equation ();
//void choose_equation_2d ();
int exit ();
void speed_angle_2d ();
double stringconvert(string convert);
void display (double dxdisp,double vidisp,double vfdisp,double adisp,double tdisp);
//equation funtions
double solve_vf_no_dx (double te,double vie,double ae);
double solve_dx_no_vf (double te,double vie,double ae);
double solve_dx_no_vi (double te,double vfe,double ae);
double solve_vi_no_dx (double te,double vfe,double ae);
double solve_dx_no_a (double te,double vie,double vfe);
double solve_a_no_dx (double te,double vie,double vfe);
double solve_dx_no_t (double vie,double vfe,double ae);
double solve_t_no_dx (double vie,double vfe,double ae);
double solve_vi_no_vf (double dxe,double ae,double te);
double solve_vf_no_vi (double dxe,double ae,double te);
double solve_vi_no_a (double dxe,double vfe,double te);
double solve_a_no_vi (double dxe,double vfe,double te);
//start
int main()
{
   choose_equation_1d_or_2d ();
   return 0;
}
void choose_equation_1d_or_2d ()
{
     string check;
     cout << "kinematics program", cout << endl;
     cout << "by stuart tveter", cout << endl;
     cout << "is the problem 1d or 2d", cout << endl;
     cin >> check;
     if (check == "1d")
     _1d_equation ();
     else if (check == "2d")
     //choose_equation_2d ();
     cout << "bla";
     else
     {
     cout << "that is not an option";
     exit ();
     }
     
}
int exit()
{
     string a;
     cout << endl,cout << "do you wish to restart? y/n", cout << endl;
     cin >> a;
     if (a == "y" || a == "yes")
     main ();
     else
     return 0;
}

int _1d_equation ()
{
     string dx,vi,vf,a,t;
     double vfd,ad,td,vid,dxd;
     cout << "enter know givens, if unknon, put a ?,";
    // cout << "if unused, put a null", cout << endl;
     cout << "dx=", cin >> dx, cout << endl;
     cout << "vi=", cin >> vi, cout << endl;
     cout << "vf=", cin >> vf, cout << endl;
     cout << "a=", cin >> a, cout << endl;
     cout << "t=", cin >> t, cout << endl;
     //begin formula sorting
     if (dx == "?")
     {
            if (vf == "?")
            {
            vid=stringconvert (vi);
            ad=stringconvert (a);
            td=stringconvert (t);
            dxd=solve_dx_no_vf (td,vid,ad);
            vfd=solve_vf_no_dx (td,vid,ad);
            display (dxd,vid,vfd,ad,td);
            }
            else if  (vi == "?")
            {
            vid=stringconvert (vf);
            ad=stringconvert (a);
            td=stringconvert (t);
            dxd=solve_dx_no_vf (td,vid,ad);
            vfd=solve_vf_no_dx (td,vid,ad);
            display (dxd,vid,vfd,ad,td);
            } 
            else if (a == "?")
            {
            vfd=stringconvert (vf);
            vid=stringconvert (vi);
            td=stringconvert (t);
            dxd=solve_dx_no_a (td,vid,vfd);
            ad=solve_a_no_dx (td,vid,vfd);
            display (dxd,vid,vfd,ad,td);
            }
            else if (t == "?")
            {
            vfd=stringconvert (vf);
            vid=stringconvert (vi);
            ad=stringconvert (a);
            dxd=solve_dx_no_t (ad,vid,vfd);
            td=solve_t_no_dx (ad,vid,vfd);
            display (dxd,vid,vfd,ad,td);
            }
            else
            cout << "exe error";
     }      
     else if (vi == "?")
     {
            if (vf == "?")
            {
            dxd=stringconvert (dx);
            ad=stringconvert (a);
            td=stringconvert (t);
            vid=solve_vi_no_vf (dxd,ad,td);
            vfd=solve_vf_no_vi (dxd,ad,td);
            display (dxd,vid,vfd,ad,td);
            }
            else if (a == "?")
            {
            dxd=stringconvert (dx);
            ad=stringconvert (a);
            td=stringconvert (t);
            vid=solve_vi_no_vf (dxd,ad,td);
            vfd=solve_vf_no_vi (dxd,ad,td);
            display (dxd,vid,vfd,ad,td);
            }
            else if (t == "?")
            {
            vfd=stringconvert (vf);
            dxd=stringconvert (dx);
            ad=stringconvert (a);
            vid=solve_dx_no_t (ad,vid,vfd);
            td=solve_t_no_dx (ad,vid,vfd);
            display (dxd,vid,vfd,ad,td);
            }
            else
            cout << "exe error";
     }
     else if (vf == "?")
     {
          if (a == "?")
          {
          dxd=stringconvert (dx);
          vid=stringconvert (vi);
          td=stringconvert (t);
          vfd=solve_dx_no_t (dxd,vid,td);
          ad=solve_t_no_dx (dxd,vid,td);
          display (dxd,vid,vfd,ad,td);
          }
          else if (t == "?")
          {
          vfd=stringconvert (vf);
          vid=stringconvert (vi);
          ad=stringconvert (a);
          dxd=solve_dx_no_t (ad,vid,vfd);
          td=solve_t_no_dx (ad,vid,vfd);
          display (dxd,vid,vfd,ad,td);
          }
          else
          cout << "exe error";
     }
     else if (a == "?")
     {
          if (t == "?")
          {
          vfd=stringconvert (vf);
          vid=stringconvert (vi);
          dxd=stringconvert (dx);
          ad=solve_dx_no_t (ad,vid,vfd);
          td=solve_t_no_dx (ad,vid,vfd);
          display (dxd,vid,vfd,ad,td);
          }
          else
          {
          cout << "exe error";
          }
     }
     else
     cout << "exe error";
 
}

//void choose_equation_2d ()
//{
//     cout << "not built yet";
//     exit ();
//}

double stringconvert (string convert)
{
       double num;
       stringstream ss(convert);
       ss >> num;
       return num;
}
void display (double dxdisp,double vidisp,double vfdisp,double adisp,double tdisp)
{
     cout << "dx = ",cout << dxdisp,cout << "m", cout << endl;
     cout << "vi = ",cout << vidisp,cout << "m/s", cout << endl;
     cout << "vf = ", cout << vfdisp ,cout << "m/s", cout << endl;
     cout << "a = ",cout << adisp,cout << "m/s" ,cout << endl;
     cout << "t = ",cout << tdisp,cout << "sec", cout << endl;
     exit ();
}



Last edited on
part B
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
/*   equations                                                              */
double solve_vf_no_dx (double te,double vie,double ae)
{
       double vfe;
       vfe= (ae*te)+vie;
       return vfe;
}
double solve_dx_no_vf (double te,double vie,double ae)
{
       double dxe;
       dxe =(vie*te)+((pow(ae*te,2))/2);
       return dxe;
}
// dx and vf
// dx     and vi
double solve_dx_no_vi (double te,double vfe,double ae)
{
       double dxe;
       dxe=(vfe*te)-(.5*ae*(te*te));
       return dxe;
}
double solve_vi_no_dx (double te,double vfe,double ae)
{
       double vie;
       vie=vfe-(ae*te);
       return vie;
}
//
double solve_dx_no_a (double te,double vie,double vfe)
{
       double dxe;
       dxe=.5*(vie=+vfe)*te;
       return dxe;        
}
double solve_a_no_dx (double te,double vie,double vfe)
{
       double ae;
       ae=(vfe-vie)/te;
       return ae;
}
// dx and a
//dx and t
double solve_dx_no_t (double vie,double vfe,double ae)
{
       double dxe;
       dxe=((pow(vfe,2))-(pow(vie,2)))/(ae*2);
       return dxe;
}
double solve_t_no_dx (double vie,double vfe,double ae)
{
       double te;
       te=(vfe-vie)/ae;
       return te;
}
//
double solve_vi_no_vf (double dxe,double ae,double te)
{
       double vie;
       vie=(dxe-(.5*ae*(pow(te,2))))/te;
       return vie;       
}
double solve_vf_no_vi (double dxe,double ae,double te)
{
       double vfe;
       vfe=(dxe+(.5*ae*(pow(te,2))))/te;
       return vfe;
}
//vi and vf
//vi and a
double solve_vi_no_a (double dxe,double vfe,double te)
{
       double vie;
       vie=(2*(dxe/te))-vfe;
       return vie;
}
double solve_a_no_vi (double dxe,double vfe,double te)
{
       double ae;
       ae=(dxe-(vfe*te)/(pow(te,2)));
       return ae;
}
//
double solve_vi_no_t (double dxe,double vfe,double ae)
{
       double vie;
       vie = (pow(vfe,2))-(2*ae*dxe)
       double sqrt(vie);
       return vie;
}
double solve_t_no_vi (double dxe,double vfe,double ae)
{
       
}
Topic archived. No new replies allowed.