Possibly too many details

Before I get started talking about the latest program I am trying to write C++, I wanted to talk about what I was able to do with BASH much more easily than I expected. Also let me say that BASH was one of the biggest reasons (along with those listed in the “About” section) I wanted to come back around to using Linux for this project (and for everyday use). I just find BASH so much more intuitive than the Windows underlying command language (not DOS?) when it comes to quick little scripts. I knew BASH would do what I wanted to do but I wasn’t knowledgable in BASH to really get it done, especially do it well. A few minutes of Google searching later and I had what has the most effecient BASH script I’ve seen with my own eyes.
I wanted the CPU/OS to record the exact time taken by each of my programs to run. So I could have the computer tell me within fractions of a second which program was running fastest. Whether Python can outrun MatLab. Whether C++ can outpace both of them. Of course much this was based on the way I wrote the programs as well, but did that really need to be said. It’s kind of par for the course that a beginner to a language won’t use it in the most effecient manner. That said, if bad C++ code can outpace decent MatLab code then that’s quite the argument. Now… Let’s see this wonderful BASH script then, shall we?
For timing C++

2 # bash script to record execution of C++ programs
4 TIMEFORMAT=’It takes %R seconds to run the program’
5 time {
6 $1
7 }

For timing Python

2 # bash script to record execution of python programs
4 TIMEFORMAT=’It takes %R seconds to run the program’
5 time {
6 python $1
7 }

For timing MatLab/Octave

1 #!/bin/bash
2 # bash script to record execution of Octave programs
4 TIMEFORMAT=’It takes %R seconds to run the program’
5 time {
6 octave $1
7 }

It should be easy to see that there is only one difference between the three scripts. Line 6 in each script is the line the executes the program for me so it has a very repeatable start time. You may also the unchanging feature on line 6, the “$1”. This is my very….rudementary… definitely not lazy… way of interacting with the script so I don’t have to recompile new version of this script for every new C++, Python, or Octave script I write. This character combinaiton in this usage case in BASH accepts the first argument passed against the execution of the script. In case my explanation wasn’t that great, here’s an example to show you what I mean. If I wanted to time the Python version of my tension program from the last couple of weeks, it would look like this.

tylerdurden@codebase:~$ ./time.sh tension.py
Final Tension is 49.05
It takes 0.016 seconds to run the program

In BASH arguments are passed by giving a space after the initial command and typing the argument. BASH scripts will executes BASH commands as if you were in the command line, so in line 6 I only need to provide which program I want to initiate and I can then type the name of which script I want to run. It may also be worth mentioning here that line 6 in the C++ timer has only the ” $1 ” and nothing else. As we’ve discussed before, C and C++ create executables separate from the source files so running the BASH script with my C++ files looks slightly different. Like this.

tylerdurden@codebase:~/Documents/Cprogs$ ./time.sh ./tension1
It takes 0.017 seconds to run the program

One last clarification for those unfamiliar with the Linux Born Again SHell (BASH), the ” ./ ” before certain scipts is needed for executables that aren’t stored in the /usr/bin partition of a Linux system. Programs like Octave and Python themselves are installed into the /usr/bin partition, somewhat like the sys32 folder in Windows, and can therefore be hardlinked to by the system and called upon simply by the name of the program. Ok so now to the mathematical programming.

The iterative method I decided to go with is the Newton-Raphson Method. (http://mathworld.wolfram.com/NewtonsMethod.html) Why? Honestly because I had a MatLab script still laying around from my school days. So I actually started by converting the MatLab to Python. There were a few bumps there but this blog isn’t about my Python scripts. It’s about my C++ scripts. And this particular one is having it’s own problems. All semantic. It wasn’t that difficult to get a script together that would compile; getting one that even comes close to the right answer has proven a bit of a problem. So far it would be great I could just get it to iterate. Something has it convinced that it has found the answer after only one pass through the loop. Plus the fact that the answer is wrong. The problematic C++ source code:

1 // Newton-Raphson method
2 #include
3 #include
4 using namespace std;
6 int errfound(100000), J(0);
7 double xg(-0.1), x2, xnew, errtolz(1.0e-13);
9 int main()
10 {
11 while( errfound > errtolz ){
12 x2 = xg-(( 1.0162 * sin( xg ))-( 1.5185 * xg * exp( 0.25 * pow( xg,2 ))))/(( -1.0162 * cos( xg )) + ( 1.5185 * 0.25 * 2 * pow( xg,2 )) * exp( -0.25 * pow( xg,2 )) – 1.5158 * exp( -25 * pow( xg,2 )));
13 errfound = abs(( 1.0162 * sin( xg )) – ( 1.5185 * xg * ( exp( 0.25 * pow( xg,2 )))));
14 J++;
15 xg = x2;
16 cout<<J<<endl;
17 }
19 xnew = x2;
20 cout<<x2<<endl;
21 cout<<xnew<<endl;
22 cout<<J<<endl;
24 return(0);
25 }

So far the output is

tylerdurden@codebase:~/Documents/Cprogs/source$ ./a.out

Compare this to MatLab, Python, and pen&paper and it becomes even more obvious there is a real problem here. Original MatLab from school days with results.

1 % Newton Raphson Method for solving Taylor series
3 xg=-0.1;
4 errfound=1000; % how we assess quality of f(x)=0
5 errtolz=1.0e-13;
6 j=0;
7 while(errfound>errtolz);
8 j++;
9 x2=xg-((1.0162*sin(xg))-(1.5185*xg*exp(0.25*(xg^2))))/((-1.0162*cos(xg))+(1.5185*.25*2*xg^2)*exp(-.25*xg^2)-1.5185*exp(-25*xg^2));
10 errfound=abs((1.0162*sin(xg))-(1.5185*xg*(e^0.25*(xg^2))));
11 xg=x2;
12 end;
14 j
xnew = -7.3628e-14
j = 126

The Python is really not very different at all.

1 #!/usr/bin/python
2 # Newton-Raphson method for solving taylor series
4 import math
6 xg = -0.1 # short for x guess
7 err1 = 1000 # big so it won’t stop first try
8 errtarg = 1.0e-13 # closest to zero as computorly possible
9 j = 0
10 while (err1>errtarg):
11 j = j+1
12 xnew = xg – ((1.0162 * math.sin(xg)) – (1.5185 * xg * math.exp(0.25 * math.pow(xg,2)))) / ((-1.0162 * math.cos(xg)) + (1.5185 * 0.25 * 2 * math.pow(xg ,2)) * math.exp(-0.25 * math.pow(xg,2)) – 1.5185 * math.exp(-25 * math.pow(xg,2))) # taylor series in action
13 err1 = abs( (1.0162 * math.sin(xg)) – (1.5185 * xg * math.exp(0.25 * math.pow(xg,2))) ) # more taylor
14 xg = xnew
15 print ( xg )
16 print ( j )

Although it has a slightly different answer but close


Up to this point I haven’t had a lot of time for troubleshooting the C++ code, but I wanted to get this posted before too long. I will continue to troubleshoot until I have working code to post, it’s just a matter of having time to do so. I continue to look for and appreciate any feedback, suggestions, or anything of the sort. Hopefully next time you see me I have some good news and working code.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s