Monday, March 24, 2014

N order systems of equations.

I updated my previous 2 order problem solver and 1 order problem solver to work with N order systems of equations, where N > 0 and < 100.  At N = 1 you are doing Runge-Kutta order 4 estimation.

You just have to set up all the equations and then pass them in along with an initial value for each one.  Your functions will be passed an array of long doubles in u[] from 0 to n-1.

Source code is here:

First you create an object, setting it's start, end, steps per unit, and title.
Next you add Orders one at a time, with the initial value and function to use at that order.
One all the orders are entered, call the  calculate function to generate the results.
Then call the print function to output the results.
There are also methods to get the results programatically.
Finally dispose of the object and you are done.

Thinking about adding an actual result function that, if present, will print out with the results as the first column.

Friday, March 21, 2014

Estimating Systems of Higher-Order Equations

Evolving the Runge-Kutta method to solve higher-order equations is an interesting challenge. To get the first program working I did it in stages.  First the entire problem was in main, and I was happy to just get u1 and u2 sorted out.  After that I just got one iteration of the K1_1 - K4_2 sorted out, all eight values.  I found a couple of problems with my formulas and fixed them so that I got the same values as the example from the book.

The main loop was next.  I was able to loop through all 10 iterations without any problems because I took my time on that first iteration.  I printed out the results and it matched the output from the example in the book.  All that took about 4 hours.  It is tough to translate from a math book to a computer program, but I guess with practice one probably gets faster at it.

After that I wrapped the code in a C object and created an interface to the function.  I followed the same interface as the Runge-Kutta module I previously did.

I solved a homework problem as well, and print out the results from the actual function. 

The source code:

As I find problems in the code I will update the git repository.

Left to do:

This solves a specific order (order 2) of higher order problems.  To be truly general this should be made into a general function where you can add a function and matching initial values one at a time, then tell it solve all the levels, no matter how wide.

In that case this should solve when you tell it to print or try to pull out a value from the solved array.  It should not allocate the arrays for the work or the temp k values until you know how level of order you need.

Thursday, March 20, 2014

4th Order Runge-Kutta numerical method.

Wrote a C object to manage the code and data around generating Runge-Kutta 4th order interpolation of data.  Fairly efficient algorithm.  I estimate that it will process about 3 million points a second on a single core of a slow netbook computer.  I tried to use the same variable names in the interface that seemed to be used in most books and papers on this topic.

I  do auto ranging based on a step size per number unit.  So if your function runs from 0 to 5 and you ask for 5 steps per number, then you would end up with 5*5+1 = 26 max steps.  The +1 comes from the initial value at the start of the range, then you would do 5 steps in the 1 interval, 5 steps in the 2 interval, and so on.

Source code is here:

The example program, you pass in the starting value, the start and end points, the step per unit, and the function you are interpolating.
The object does the rest with the interface below:

The Object Interface to the Runge-Kutta module.
Once you create an object you can retrieve all the properties and data values for the object.
When you are done, you dispose the object to free the memory. 

The output for the example from the book, exact same values.

Saturday, March 15, 2014

The recent Apple SSL issue.

The double goto fail line caused the next line to be unreachable.  A fact that should have been see in compiler warnings, in code reviews, and in the IDE itself. 

Tuesday, March 11, 2014

GNU MP arbitrary precision library.

Tonight I am learning the arbitrary precision math library from gnu called gmp.   It is tough to work only with functions instead of just using math expressions.   It takes multiple lines of obscure code to do what you can do with a single C expression.  Instead of saying:
bb = floor (n *sqrt(.5) +1);
You have to say:
  mpf_t n;  mpf_init2(n, 256);  mpf_set_ui(n, 10);  mpf_pow_ui(n,n,12);
  mpf_t bb;  mpf_init2(bb, 256);  mpf_set_ui(bb, 5);  mpf_div_ui(bb, bb, 10);
  mpf_t srpf;  mpf_init2(srpf, 256);  mpf_sqrt(srpf, bb);

  mpf_mul(bb, srpf, n);  mpf_add_ui(bb,bb, 1);  mpf_floor(bb, bb);

Why deal with this extra complexity?  Because it is amazingly easy to run into problems of precision with just the 64 bits that the standard data structures give you.  Simple things like working with numbers in the 10^16 range, or 100!, or the 5000th Fibonacci number and you get the wrong values coming out of your functions.   The 5000th Fibonacci number runs you out of bits in the exponent of long double.

I am playing around with the problems on the Project Euler site and am running into a lot of situations where I cannot solve the problems with C because the problem sets have large enough numbers that they are even overflowing 64 bit integers and floating point numbers are fuzzy enough that two things that should match don't because the lowest precision bits don't match, when they should.

Monday, March 10, 2014

Going solar.

If you spend a fortune on 50KW of solar panels and the batteries to store 3 days worth of power, you can live just like you always have, with no changes needed.  But this is foolish, expensive, and wasteful. A better way to go solar is to adjust your life to require a much lower level of power, and size the solar panels to meet that reduced footprint.  This means that you need less batteries, less panels, less wiring, and less things to go wrong.

Of course, this would require people to take positive action, so it would be bad and difficult to get anyone to agree to do.  A better way would be to let the appliances work together intelligently in order to reduce the maximum power draw, and to run the most power hungry appliances during times when the batteries are charged and you can run entirely off the solar panels.

Power efficient appliances.

There are freezers and refrigerators that are much more efficient and lower power than anything you can buy at a mall store.  These freezers and refridgerators are smaller than the huge ones in most people's houses today, but you have to ask yourself, do we really need such huge fridges?  Can't we make do with less rather than eventually having to make do with none.  There are washing machines that use much less energy and water to run as well.

One of the things you have to watch out for are hidden energy costs.  A 1200 Watt microwave oven actually draws 2400 watts of power.  Many gas ovens use up to 400 Watts of power when the oven is on to keep the ignitor hot enough to light the gas as it kicks on and off.  A watt meter like the Kill-A-Watt meter is very useful to see how many watts and KWh a machine uses.

Solar Water Heating.

One of the highest energy expenses in any home is the water heater and heating water on the stove. One thing that helps with this is to use a small, well insulated water heater that can provide several cups of boiling water on demand. This small water heater could keep running even when the house was in a degraded mode, allowing people to make hot beverages and small hot meals.

By directly heating water with the heat of the sun you can reach amazingly high efficiencies.  Any system I build will have a large insulated water tank to store heat to both heat the house and my own drinking water, and a solar panel designed to heat antifreeze up to 180 degrees.  Or there may be a water tank to store heat for the house and a different tank for drinking water. This heated fluid would circulate in heat transfer loops around the base of the water tank to transfer the hot water to the cold water in the bottom of each water tank.  Another loop could come from a wood stove to heat the water in the tank as well.  The best design is to limit the length of your water lines and heavily insulate both lines.  More loops could be placed in the floor to heat the floors in your house, the most efficient kind of heating.

Solar cooking.

It is possible to cook entire tasty, nutritious and healthy meals in a couple of hours times in solar ovens. You can also sterilize water this way for drinking.  There are entire websites devoted to these techniques. It would be interesting to explore how to build several solar ovens into the design of a house to allow free cooking of foods.

Home entertainment.

My media player is already based on the Raspi running XBMC. This media player only draws 3.5 watts, plays HD video, and it is easy to load in another SD memory card to turn your TV into a game system that can play most of the games written before about 8 years ago.   As similar more powerful boards are released every few years it should be easy to upgrade the player.   I eliminated the older file server I was using, saving 10 watts of power, moving all the functions it was performing over to the Raspi already acting as my media player.  So far it has been working flawlessly.

One of the most power hungry appliances in the house has always been the TV, and while they are more efficient for the screen size than older TVs, a large plasma or LCD monitor can still draw up to hundreds of watts.  I would like to use a low powered projection device drawing no more than a dozen watts with a highly reflective screen  in a dark room to get a great picture while not drawing hundreds of watts of power.

The intelligent house.

Ideally an advanced house would schedule when the various appliances ran.  I would load the laundry and dishwasher, and the house would clean the dishes, then wash the clothes so they were done just as I got home for the day, sending a notice to my phone to put the clothes in the dryer.  While the washing machine motor was running at full speed, the freezer and refrigerator compressors would be turned off for a few minutes.  The water heater would only run when nothing else needed power, turning off when the furnace ran the blower.

In this way, instead of needing to provide 100 amp service, you could run the house on 20 or even 10 amps, if the compressors and motors are made smaller, and more efficient, with a corresponding reduction in the size of the solar array and batteries you need to provide power for a house.  It may be possible to run a generator on demand to top off batteries periodically when there is no grid power available and demand is greater than can be provided by solar alone.

An intelligent house would monitor the environment, in and out of the house, adjust light, humidity, and temperature levels to make things comfortable for the people living inside.  There should be a reporting area that graphs how much power is available projecting how many days the house can provide full power before shifting to a degraded mode of operation.  The generator fuel supply could be intelligently applied to this prediction and usage graphed over time.

It may be possible to run a few LED lights, and run the solar water heater to heat the house indefinitely, while not being able to run various appliances except when the batteries are charged and the solar panels voltage would otherwise be wasted.

Sunday, March 9, 2014

Dotnet on Linux using Mono

I had a function with no counterpoint in Linux that I wanted to run, something that returns just the last few digits of a very large number.  There was a function in a Windows Numerics library that did the trick.

When I first tried to compile the program it complained that it couldn't find the Numerics.dll, so I had to use the Ubuntu Software Center to install "libmono-system-numerics4.0-cil".

After that I had to figure out where it put the file I needed:
locate System.Numerics.dll
And finally I was able to compile and run the program:
 gmcs -r:/usr/lib/mono/4.0/System.Numerics.dll Test.cs  
It did the trick.  Now I am ready to try to do larger projects using C# and dotnet on Linux.

Here is a link to an example of a hello world program:

Raspberry Pi taking over my home network.

I had a separate small mini-itx board acting as a file server.  I didn't have a good enough power supply attached to it, so it would lock up about once a week. Despite this, it seemed to do a good job until last week.  Suddenly last week the server locked up, and afterwards it appeared that upnp services were gone from my network.  I have no clue why even now.

I had been watching how much running Raspbmc was loading the Raspberry PI I have been using as a media player for over a year now, and it seemed like I could put many more services on this low power device.  Losing upnp services gave me an excuse to try to move everything over to the Raspi.

First thing I did was install COPS, from here:  with some explanation here:  Essentially you load your books into Calibre, storing everything on the file server, then you point the COPs server configuration file to where the Calibre database file is located and this will share all those books to many different book readers on your local network.  I use fbreader on a galaxy tab 2 pad computer to read my books.

To get cops to work I had to load in the php and data base modules with the following commands:
sudo apt-get updatesudo apt-get install php5-gd php5-sqlite
I used a long USB extension cord to connect the hard drives from where they were sitting to a free port on the Raspi.  The hub I used does not feed power back into the Raspi.  Feeding power backwards through the usb port is something you evidently must avoid doing, from what I have read.  This can cause the Poly fuses to trip.  Evidently in newer raspi designs these usb poly fuses have been removed.
Once attached all the drives immediately were mounted and instantly showed up in the XBMC video file menu.  I was able to browse and play all the media I had on these drives right away.  I pointed the COPS server at the calibre database on the drives and it instantly began to work.

The last thing I needed was the transmission file download server.  I had been running it for a while on the mini-itx box and loved how easy it was to manage the downloads from my laptop machine.  It installed with the following command:
sudo apt-get install transmission-daemon
Then I followed this guide to configure it:

I am concerned about running the external HD's all the time so they don't use a lot of power and wear out faster than they should, and I didn't want to write to the SD Card in the Raspi that boots the OS, so I added an 8 GB SD card to the hub, formatted it was an ext4 file system and am having transmission save incomplete files to this drive, then copying the files to a hard drive once it is done.  So now, running just the Raspi and saving to this extra SD card is only drawing about 3 watts, not the 20 watts it was taking before.

You can easily log into the Raspi and run top to watch what is going on and most of the time I see over 40% idle even while watching an HD movie and copying a few GB of files across the network.