Re: Complex numbers and equation optimization using GA
Quote:
Originally Posted by Nighteyes
Guess it was just wishful thinking that the N-R program was good...
Please don't be discouraged. In fact, I highly approve of your efforts and ability to learn.
When I was in grad school years ago, the general programming skills of university reserch professionals and professors were very poor. There has been unfortunately little improvement in this area due to the lack of communication between cutting-edge professional programmers and university researchers. We are trying to close this gap. And you should keep trying to improve as well.
I will be away from all offices on Friday and return on Sunday.
My last post was a bit terse on the description of the Newton iteration. If I get a chance, then I might be able to work out a sample program for computing the derivative of a generic function. This would provide you with a better impression of what I am talking about in terms of style and implementation.
Keep working on this stuff. You are going in the right direction. Post your progress and further questions when you are finished with the next steps in a few days.
Sincerely, Chris.
1 Attachment(s)
Re: Complex numbers and equation optimization using GA
Hi Craig,
I was thinking about my recent response about Newton Iteration. At first you might want to stick with your first-order estimate of the derivative. This will keep the implementation simple.
That Numeric Recipes book that I had mentioned really does a good job explaining this kind of stuff.
Quote:
Originally Posted by dude_1967
If I get a chance, then I might be able to work out a sample program for computing the derivative of a generic function. This would provide you with a better impression of what I am talking about in terms of style and implementation.
It was so much fun that I looked at some of my notes from 20 years ago. I wrote down the central difference rules for numerical derivatives and worked out a sample C++ file for testing these. They are really neat little formulae. See the attatchment.
Sincerely, Chris.
Re: Complex numbers and equation optimization using GA
wow thanks mate!
Got my C++ programming book back of my bro today so gonna read a few more chapters in that tonight, esp multi-file programs.
I am gonna stick to the first order calculation, just because this is what is used in the code I am trying to replicate to show genetic algorithms/ differential evolution algorithm can do it faster. (this may not be the case)
I am set on learning C++ to the best of my abilities and as fast as possible so I will not be discouraged, just encouraged to try an do even better with the next draft of the code
The "Guess it was just wishful thinking that the N-R program was good..." is my everlasting optimism that I can do something perfect first time (like that will ever happen :S)
I had a good look though your code for the derivatives and then looking at the book called "Numerical recipes in C" I have decided to change my fdash function from
fdash= F(x+h)-F(x) / h to fdash=F(x+h)-F(x-h) / 2h
So I can calculate the actual difference this makes on the calculation of the zero point although this will have to kept as fdash= F(x+h)-F(x) / h for calculating weather a Genetic algorithm or newton method is faster for getting convergence
also interestingly the book suggests by setting a new variable temp as
temp = x+h so temp is an exact representable number in binary
The error can be reduced significantly. Would setting temp, then for example 1, count as the smallest possible exactly represented binary number.
Also in you notes what is the symbol in 1st derivatives
fdash = m1 + unknown_symbol (dx^2)
Thanks again for all the help, I think im learning much faster than if I was doing this on my own.
Also how do you access g++ documentation/help files? cant find it anywhere on my computer, but then again I only started using linux and programming 2 weeks ago now
Craig
P.S do you think it is a good Idea to change the title of this thread to "Numerical programing" or something like that?
Re: Complex numbers and equation optimization using GA
Quote:
Originally Posted by Nighteyes
I am gonna stick to the first order calculation, just because this is what is used in the code I am trying to replicate to show genetic algorithms/ differential evolution algorithm can do it faster. (this may not be the case)
That sounds fine.
Quote:
Originally Posted by Nighteyes
I am set on learning C++ to the best of my abilities and as fast as possible so I will not be discouraged, just encouraged...
Great! C++ is a great language for numerical calculations.
Quote:
Originally Posted by Nighteyes
I had a good look though your code for the derivatives and then looking at the book called "Numerical recipes in C" I have decided to change my fdash function from
fdash= F(x+h)-F(x) / h to fdash=F(x+h)-F(x-h) / 2h
This is a good compromise between tersity of code and precision.
Quote:
Originally Posted by Nighteyes
Also in you notes what is the symbol in 1st derivatives
fdash = m1 + unknown_symbol (dx^2)
The symbol dx is the step-size, often called h.
Quote:
Originally Posted by Nighteyes
Also how do you access g++ documentation/help files?
You can look at the manual pages of g++ by typing the command 'man g++'. There is also a good documentation available online from GNU. I'll get the link tomorrow. I also have this book (but it does not contain any more than the above mentioned docs): GCC book
Quote:
Originally Posted by Nighteyes
P.S do you think it is a good Idea to change the title of this thread to "Numerical programing" or something like that?
We shouold keep going in this thread for this assignment. It is not unusual to have a thread go for 50 or more entries over the course of a few weeks. If we had done anything wrong, the mods would have commented. After a while, certain threads like this one don't draw the full attention of the users. So if you have an entirely different question, then you might want to post an additional thread. Also I might mention that I am not often on the board because I have very many other topics of work. But I will continue checking your progress on this thread.
Feel free to post additional questions as you progress.
Sincerely, Chris.
3 Attachment(s)
Re: Complex numbers and equation optimization using GA
Just posting a little update.
Read another 400 pages of my c++ book. read:
8) operator overloading
9)Inheritance
10)Pointers
11)Virtual Functions
12) Streams and Files
13) Multifile Programs
I now understand why you made the namespace's.
So now I made the Ashley program so it imported into the Newton_R method solver and finds the zero.
At the moment there is a big error in the ashley program as can be seen in the pic "ah.jpg" this is not due to the code though this is due to the variables A, B, C and D which I will renter tomorrow. Otherwise I am quite pleased with my first attempt at a multifile program. Though I think I am going to do alot of changes to how the program will work in the end.
I am thinking have a input file which reads in different omega values and initial guesses and then outputs the final k value along with the inputed omega value.
On a side point to make the Newton_R Method program an independent module which can be used to solve all sorts of equations would it be a good Idea to make it in to a class? or at least a function that takes an input of the function that needs to be solved and an initial guess....
Thanks Craig
1 Attachment(s)
Re: Complex numbers and equation optimization using GA
Hi Craig,
Sorry about the late response. I was unavailable for a while.
You should probably try to have a separate cpp file for the Ashley stuff. By putting the static functions right in the header file, you have essentially just included one cpp file into another. You need to make a separate header file for the Ashley stuff which includes only the function declarations in the namespace---and only those needed by other modules.
I am having difficulty understanding you implementation of Newton-Raphson. I am unable to see where you re-evaluate the function and its derivative using the new midpoint. I am unsure, but I beleive that something might be wrong here. But I have written a sample included below which might help you clear this up.
As to your question about a generic implementation of Newton-Raphson, I would definitely consider this to be the right thing to do. However this is often simply implemented as a procedure, not a class. You have an additional complication with your independent function GA::Eigen. It takes two input parameters, the second one named omega. Most simple, generic implementations of Newton-Raphson procedures deal with functions with only one independent variable, say x.
I have written down an implementation of Newton-Raphson using a third order derivative formula in the test program shown below. It is based on the algorithm shown in the book "Numerical Recipes in C++". You should look carefully how a function pointer is passed as an input parameter to the Newton-Raphson method as well as to the derivative algorithm. This technique is essential if you want to create any kind of generic algorithm for these kinds of things.
We could modify this technique to accomidate your function GA::Eigen(x, omega) if you would like. You could also try to do this on your own and get back to me with any problems.
Good luck with your further progress.
Sincerely, Chris.