Solved

Solving exponential equations in C++

Posted on 2009-05-07
8
1,156 Views
Last Modified: 2012-06-21
Is there a function or a library that can solve equations like: exp(constant*x) + exp(constant*x) = constant ?
If not , what is the best way to solve this equation in c++ ?

Thanks
0
Comment
Question by:Chrysaor
8 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24326232
Depends on what you mean by "exp"--what is this function supposed to do? Also, you can't assign a constant to anything but a constant, as far as I know. The entire expression must be a constant (unless x is a constant as well). Can you be a  bit more specific as to what exactly you're trying to accomplish?
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 24346957
He is trying to solve the equation "e^mx + e^nx = k" for x; m, n & k are known.
0
 

Author Comment

by:Chrysaor
ID: 24350226
Excactly as jhshukla said.. Any ideas?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Assisted Solution

by:MatrixDud
MatrixDud earned 83 total points
ID: 24496538
If all values are known, as you mentioned jhshukla was right about, then I don't see what you are trying to do (function is already solved).

The standard library has the exp function (math.h). You'll have to break the algebra down to its simplest form and solve for what ever variable you're looking for. Standard C++ libraries won't do that for you. A library like MathCAD might. Unless you need to break down a lot of different equations then you would be better to just do it yourself.
0
 
LVL 8

Expert Comment

by:JIEXA
ID: 24533968

/* Newton's method example.
   http://en.wikipedia.org/wiki/Newton%27s_method */
#include <stdio.h>
#include <math.h>
 
/* the function */
#define f(x)	exp(2*x)+exp(3*x)-5
/* the derivative on x */
#define f_(x)	2*exp(2*x)+3*exp(3*x)
/* the first quess */
#define x0	-1
/* precision */
#define epsilon	1e-9
 
int main()
{
    double prev,curr,delta;
    prev=curr=x0;
    while(1)
    {
	curr = prev - (f(prev)/f_(prev));
	delta = fabs(curr-prev);
	printf(">> %.10f e=%.10f\n",curr,delta);
	if (delta < epsilon)
	  break;
	prev = curr;
    }
    printf("%.10f\n",curr);
    return 0;
}

Open in new window

0
 
LVL 8

Assisted Solution

by:JIEXA
JIEXA earned 84 total points
ID: 24534003
Sorry, wrong usage of macros, here is new code.
/* Newton's method example.
   http://en.wikipedia.org/wiki/Newton%27s_method */
#include <stdio.h>
#include <math.h>
 
/* the function */
#define f(x)	exp(2*x)+exp(3*x)-5
/* the derivative on x */
#define f_(x)	2*exp(2*x)+3*exp(3*x)
/* the first guess */
#define x0	-1
/* precision */
#define epsilon	1e-9
 
int main()
{
    double prev,curr,delta;
    prev=curr=x0;
    while(1)
    {
	curr = prev - (f(prev))/(f_(prev));
	delta = fabs(curr-prev);
	/*printf(">> %.10f e=%.10f\n",curr,delta);*/
	if (delta < epsilon)
	  break;
	prev = curr;
    }
    printf("root=%.10f, f(root)=%.10f\n",curr,(f(curr)));
    return 0;
}

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 83 total points
ID: 24534010
>> Sorry, wrong usage of macros, here is new code.
Just a style tip, in C++ prefer to use in-line functions, macros have so many reasons for not being a good idea in C++ it's not even funny :)
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 250 total points
ID: 24542526
substitute e^x = A
A^m + A^n = k
depending on the values of m & n, the equation may be unsolvable or you may end up with non-real roots (for A).

x = ln(A)

wikipedia has a variety of formulas for solving cubic & quartic equations
http://en.wikipedia.org/wiki/Cubic_equation
http://en.wikipedia.org/wiki/Quartic_equation
there is no set formula for solving higher order equations. you can use newton's method as jiexa showed.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
Getting IP address 8 89
Using Diagnostic tools in VS2015: Unresoved allocations 19 105
Error C2678: binary '!=': no operator found... 4 58
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question