Recent Activity

Wave Analyzer

I've posted some new code under the projects section, a wave analyzer. It's still pretty early in development, but you can use it to see how waves actually move, rather than just looking at formulas.

A simple sine wave is composed of 4 basic components: amplitude, frequency, wave length and phase angle. The wave analyzer allows you to add waves to the graph, and then tweak these components to see how it effects the output.

It gets interesting when you add multiple waves and then combine their output. This shows a similar output to what you would see if you had several wave generators hooked up to a string (in real life) and looked at their combined effect.

To get started, first go download the app (or source code). Run the application and then follow these steps to get something like this:


Wave Analyzer Screenshot

1. Click "Add Waveform" 2. Click "Add Waveform" again to get a second wave 3. Now in the Function Generators window, increase the wave length parameter of the second wave. 4. You should now see the plot of two waves on the green screen, to see the sum, click the "Sum All Channels" button, on the main window 5. To see *just* the sum, go back toe the Function Generators window and click the "visible" button on both the waves to hide them

Posted in Math, Waves, VB.NET and Modeling on February 5th

Modeling with Euler's Method

Recently, I have been learning how to create mathematical models in code so I figured I would share a little. Models are used to show how some phenomenon in life behaves and are typically based on an equation that represents the thing to be modeled. So the trick is to find a way to solve those equations in code.

Euler's method (pronounced Oiler) is one way of doing this, and I will show a very simple example using the following differential equation:

dy/dx = 2x

When solved by integration, the actual solution is:

y = x ^ 2

So that is the value that we want to obtain from the program after it completes. The Euler method requires initial known values for a starting point and then determines the slope of the tangent line at that point in order to find the next *approximate* point on the curve. The initial x, y values can be arbitrary (x=1 and y=1 are used in the example), but it must be a solution to the system - using x=1, y=8 would be bad since x^2=8 is false.

Since this is an approximation, the the final result will always be off by some margin of error (for example, for xMax=2, y should equal 4, since 2 ^ 2 = 4, but when you run this code, you will see that it is off by a small amount). This error is a function of the step size, dx. Smaller step sizes are generally more accurate. In the example below, a step size of 0.01 is used, which results in an error of 0.25% and a step size of 0.005 results in an error of 0.103%.

In the code below, xMax is the ending x-value that will be approximated, dx is the step size, and x & y are the initial known values. The code was written to be run as a console application in VB.NET 2005.

Sub Main() Dim x As Double = 1 Dim y As Double = 1 Dim xMax As Double = 2 Dim dx As Double = 0.01 Console.WriteLine("X , Y -- % Error") Do While x < xMax Euler(x, y, dx) Console.WriteLine( _ "{0:f2} , {1:f2} -- Error: {2:f3}%", _ x, y, (1 - (y / x ^ 2)) * 100) Loop Console.Read() End Sub Sub Euler(ByRef x As Double, _ ByRef y As Double, _ ByVal dx As Double) Dim slope As Double = 2 * x Dim change As Double = slope * dx y += change x += dx End Sub

Here is a sample run:

X , Y -- % Error ... 1.91 , 3.64 -- Error: 0.249% 1.92 , 3.68 -- Error: 0.250% 1.93 , 3.72 -- Error: 0.250% 1.94 , 3.75 -- Error: 0.250% 1.95 , 3.79 -- Error: 0.250% 1.96 , 3.83 -- Error: 0.250% 1.97 , 3.87 -- Error: 0.250% 1.98 , 3.91 -- Error: 0.250% 1.99 , 3.95 -- Error: 0.250% 2.00 , 3.99 -- Error: 0.250%

As you can see from the sample run, the output claims that:

x ^ 2 = y 2 ^ 2 = 3.99

illustrating the approximation error. However, models are not supposed to be exact, they are supposed to be a representation that exhibits behavior similar to the thing your are trying to analyze. Also, there are much better approximation methods (such as Runge-Kutta). You wouldn't want to really use this method to solve the equation in this example, this was just to establish how the concept works. As the complexity of the model grows, this method becomes much more attractive.

This basic framework can be used to model all sorts of neat stuff. Coming up, I will post an example model for a cooling liquid using this method in conjunction with Newton's law of cooling, and then gravity and falling objects.

You can read more about Euler's method here.

Posted in Math, VB.NET, Modeling and Algorithms on January 20th