So I think this is a beginner question, either that or I am aiming too high for a beginner.
I am writing a code, and I need for part of it to be able to solve an equation for x. I am told I can not use a simple SOLVE command. So my question is, how do I go about solving f(x) = g(x) for x, in a general manner (i.e. the functions f() and g() are changeable, so I need something that will solve for any [feasible] functions) preferably fairly simply and without adding copious amounts to my code.
A general equation solver is probably a bit too high for a beginner in C++, I am afraid. It's really not simple. If you google "equation parsing" you'll get an idea of what's needed to break down the equation into bits.
There's no way to type in an equation and have it suddenly become part of the code; you would have to write an engine for analysing the equation, breaking it down into parts, and then solving it. It's really not simple.
If you know what f(x) is going to be in advance, or even the general form of it (e.g. it will always be a polynomial of order 3, or it will always be of the form "y = mx+c"), you can simplify it, but a general equation solver for all possible equations is not an easy task.
What I am actually trying to do is find the points of intersection of 3 lines in 3D space. I have put the functions into parametric form, I just now need to find the intersection points. The lines are all, more or less, orthogonal. By that I mean, they are not actually orthogonal, but one runs from [0<x<100] with z constant, one from [0<y<50] with z constant, and the last one from [0<z<50] with x and y constant. [i.e. the parametric forms are (I think):
1. ( t , f(t) , c1 )
2. ( g(u), u , c2 )
3. ( c3 , c4 , s )
where the 'c's are constants and t, u, s are the parameters.]
Ok, so f(t) is - 21*EXP[ - (t-50)^2 * LOG[3] / 2500] + 7 when t = 0
21* EXP[ - (t-50)^2 * LOG[3] / 2500] - 7 when t = 14
and for 0 < t < 14 the function is interpolated between these two.
Equation 3. obviously has no function, those lines are parallel to the y axis (I hope)
And the g(u) functions are unclear as yet (we are waiting for date for them) but for the mean time I am trying to just put in
g(u) = 0 when u = 0
g(u) = u/2 when u = 50
g(u) = 100 when u = 100
So I am basically trying to find where t = g(u) and f(t) = u (Or, alternatively, f(t) = g^(-1) (t) )
Well, if two of their z's are constant, then it must mean that if 3 of them cross, they cross at that z point. So you'd just have to figure out where the last one goes and see if it hits that z point. If it does, then you can find the x/y and see if they fit into the top 2 equations. Not sure how much that simplifies it but...
Since you have x/y (from c3/c4 above), you can use those to find t/u (from the equations) if I am reading it correctly. Then you just sub them in and see if they are consistent.
All right, i have to say that I personally think C++ is not well-suited to the task of solving equations. There are better language options out there, but annoyingly I can't think of any off hand...
I don't really know how your structure/class looks so I can only really offer vague advice...if t = 3x for example, then you would get the 3 out somehow and find t by doing 3 * value of x from the other equation.
All right, i have to say that I personally think C++ is not well-suited to the task of solving equations. There are better language options out there, but annoyingly I can't think of any off hand...
I am learning this! I need it to be in C++ though, because it is part of a larger code to be linked to another C++ code. Things are never easy!
firedraco, is there a section of my code I could send that would help? I don't really know what I can tell you that might help because my programming is not great to begin with, and without trying to explain to you what it should be doing it is complicated.
Have you tried implementing gaussian elimination? You'd have to transform your functions into matrices gaussian elimination works with first, but other than that it should work.
Ok, so f(t) is - 21*EXP[ - (t-50)^2 * LOG[3] / 2500] + 7 when t = 0
21* EXP[ - (t-50)^2 * LOG[3] / 2500] - 7 when t = 14
and for 0 < t < 14 the function is interpolated between these two.
This is probably obvious to you, but just in case: t=0 and t=14 are also interpolations of the function, so there is no need to differentiate.