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 :)
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
constdouble 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.