Finding exponents in equation

I've been trying to make a program that finds the solution of an equation similar to the difference of squares equation but letting the user input the variables.
Example: (50^x)-(2^x)=12
I can find the solution in my calculator (0.666961...) but I can't find the way to put it in my cpp so the program calculates the result. If you could help me that would be great :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   const double tol = 1.0e-20;
   double x = 0, xold = 1;
   double A, B, C;

   cout << "Solves A^x - B^x = C, with A, B, C positive\n";
   cout << "Input A B C: ";
   cin >> A >> B >> C;

   double logA = log( A );
   while ( abs( x - xold ) > tol )
   {
      xold = x;
      x = log( pow( B, x ) + C ) / logA;
   }

   cout << "x = " << x << endl;
}


Solves A^x - B^x = C, with A, B, C positive
Input A B C: 50 2 12
x = 0.666961



It solves by single-point iteration.

Ax - Bx = C
Rearrange:
Ax = Bx + C
Take logs:
x log(A) = log( Bx + C )
Divide by log(A):
x = log( Bx + C ) / log(A)

Then just iterate this equation (line 19 in the code). It will converge if C > 0 and A > B > 0.

I tried Newton-Raphson first, but it didn't work very well.
Last edited on
What if A, B and C can be any number?
They can be any positive numbers (with A>B).
Just enter the values, separated by spaces, at the prompt.

Negative numbers to non-integer powers would take you outside the realm of real arithmetic.
Topic archived. No new replies allowed.