Transcendental equations?

Write your question here.
I need help. From the subject from the Faculty of Electrical Engineering I was given the task to calculate a certain physical quantity, however I got transcendental equations and in that case it is necessary to program that equation in some programming language. As I know c ++ best, I tried to program it, but it doesn't work.

The equation is:

ln (d / 0.0015) * d * d = 10/3
c++ authors for some unholy reason made the log function names really weird.
here, log = ln, and log10 is 'log', and lg is __lg. I am sure there is madness in the naming method, but it escapes me.

also note that c++ will do integer division unless one of the things being divided is floating point.
so, consider
while( fabs(10.0/3 - log(d/0.0015)*d*d) > 1e-5) //when its close to zero, you have an approximate answer
{
update d somehow.
}

note that coding it yourself in c++ is the hard way. It may not matter here, you can probably brute force the answer to this one, but in general, if you get nasty equations, you should have access to a math package like maple or mathmatica etc to do these things (does your college have a math-based computer lab?). In c++, you will have to consider numerical methods to avoid roundoff problems as well as convergence issues. Of course, if the goal is to study coding such things yourself, carry on..!
yes, the simple while loop with some hands-on trial and error yield:
d=0.733674 -> 3.33334

show us your code, working or not...
Last edited on
Natural log is written as 'log' in various academic/mathematical contexts. __lg is reserved for internal use, hence the double underscore. But yes, you probably need to use numerical methods. Perhaps Newton's method.

As I know c ++ best, I tried to program it, but it doesn't work.
Show us what you tried, and what errors or incorrect results you run into.
Single-point iteration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
using namespace std;

double invert( double dm, double RHS )
{
   const double TOL = 1.0e-20;
   double d = 1.0, dold = d + 1.0;
   while ( abs( d - dold ) > TOL )
   {
      dold = d;
      d = sqrt( RHS / log( d / dm ) );
   }
   return d;
}


int main()
{
   cout << invert( 0.0015, 10.0 / 3.0 ) << '\n';
}

0.733674




Newton-Raphson if you prefer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
using namespace std;

double invert( double dm, double RHS )
{
   const double TOL = 1.0e-10;
   double d = 1.0;
   while ( true )
   {
      double L = log( d / dm );
      double f = L * d * d - RHS;
      if ( abs( f ) < TOL ) break;
      double fprime = d * ( 1.0 + 2.0 * L );
      d -= f / fprime;
   }
   return d;
}


int main()
{
   cout << invert( 0.0015, 10.0 / 3.0 ) << '\n';
}

Last edited on
dang you guys are industrious. I just said
d = 1
while()
d -= .000001; //ok, yea, I tried += first.
The benefits of having terraflop computers @ home is you can be a little lazy/sloppy.

Ganado, I looked it up, and it appears that 'log' is used for all 3 (bases 2, e and 10) of the most common logs, partly tied to the discipline at hand and who wrote the book. So that one is whatever. But the real complaint is lack of consistency. LOG2, LOG10, LOGE would have been fine names (whatever case). And LG should be commonly available, not just internal, as .. well these computers all seem to work in base 2... but much like the misnamed vector, no one asked me .. they never do :)
Last edited on
I don't deserve to be lumped in with lastchance, he always does the cool math* stuff. :) I'm too lazy.

*or "maths" as he would say
Last edited on
jonnin wrote:
c++ authors for some unholy reason made the log function names really weird.
here, log = ln, and log10 is 'log', and lg is __lg. I am sure there is madness in the naming method, but it escapes me.

Hysterical Raisins

The oldest C libraries had log for the natural logarithm. In retrospect it should have been named “ln”, but alas, while Dennis Ritchie studied maths his friend Ken Thompson did not. As you can see, the very first logarithm function added to the C library was by him:

 Quoting http://man.cat-v.org/unix-1st/3/log
1
2
3
4
5
6
7
8
9
10
11
12
11/3/71														LOG (III)

NAME		log -- logarithm base e
SYNOPSIS	jsr r5,log
DESCRIPTION	The logarithm base e of fr0 is returned in fr0. The floatingpoint simulation should be active in either floating or double
           	mode, but in single precision integer mode.
FILES		kept in /etc/liba.a
SEE_ALSO	fptrap
DIAGNOSTICS	The error bit (c--bit) is set if the input argument is less thanor equal to zero.

BUGS	
OWNER		ken

We cannot fault him much for this, as “log” is a perfectly reasonable shortening of “logarithm”, and even today the choice between short forms depends a lot on context.

Further, I think it unlikely that either Ritchie or Thompson cared much. You can easily rewrite any logarithm to another base with a simple division.

So after some 50 years of use it is kind of hard to rewrite the meaning of a function, so what do you do? Be explicit.

    log    →  loge
    log10  →  log10
    log2   →  log2


“lg” does not appear at any place in the C or C++ Standard Libraries.
__lg” is an implementation defined helper (for GCC): https://stackoverflow.com/questions/40434664/what-is-std-lg — in other words, don’t use it.

Again, the modern Standard does a good job of naming things explicitly, as “lg” does not necessarily refer to log-base-2: https://mathworld.wolfram.com/Lg.html

Hope this helps.
Topic archived. No new replies allowed.