I did get a few minutes to have a crack at my C++ Newton Rapshon code. It took me long enough, but I finally realized that C++ doesn’t evaluate while loops the same way Python and MatLab/Octave do. So that obviously means the program isn’t as simple a port as I kind of expected it to be. That said, I haven’t done enough research to find out just how different the loop evaluation is between the languages in order to truly solve my problem. Instead, I employed an old goto (pun intended). A method many refer to as brute forcing, and most agree is quite obsolete. Either way, here is the current iteration and the results.

// Newton-Raphson method

#include

#include

#include

using namespace std;int j(0);

float xg(-0.1), x2(0), xnew(0), errtolz(.00000000001), errfound(1000);int main()

{

mylabel:

while (errfound > errtolz )

{

x2 = xg-(( 1.0162 * sin( xg ))-( 1.5185 * xg * exp( 0.25 * pow( xg,2.0 ))))/(( -1.0162 * cos( xg )) + ( 1.5185 * 0.25 * 2.0 * pow( xg,2.0 )) * exp( -0.25 * pow( xg,2.0 )) – 1.5158 * exp( -25 * pow( xg,2.0 )));

errfound = abs(( 1.0162 * sin( xg )) – ( 1.5185 * xg * exp( 0.25 * pow( xg,2.0 ))));

xg = x2;

j = j + 1;

if ( errfound > errtolz ) goto mylabel;

}xnew = x2;

cout<<xnew<<endl;

cout<<j<<endl;return(0);

}Results

-7.12246e-14

126

It takes 0.030 seconds to run the program (the first time) then:

-7.12246e-14

126

It takes 0.001 seconds to run the program

For clarification, the tiny number is the final answer and the integer is the number of iterations needed to find the answer.

For reference, the original Matlab/Octave results were

xnew = -7.3628e-14

j = 126

It takes 2.924 seconds to run the program (the first time)then:

xnew = -7.3628e-14

j = 126

It takes 0.288 seconds to run the program (the 2nd time)

So at least one thing is obvious from these results… Even brute forced, C++ is much faster than MatLab/Octave. At this level I haven’t noticed a big difference in time taken in the writing and troubleshooting part of the process but can already see a difference of up to a couple seconds in runtime. I knew there was going to be a difference but I definitely wasn’t expecting seeing it this soon, by just tossing in one loop.

All that said, I have my research to do to truly understand what the difference is in the evaluation of loops in each language so I can truly “fix” the C++ program and have something a little more finessed in it’s execution.

That’s all I wanted to say with this post. If you have any secrets you want to share with me on the subject then by all means, PLEASE! Any other feedback, criticism, and discussion is also welcome as always. Hopefully I’ll have some more fleshed out Newton Rapshon C++ code the next time you hear from me.