Examples
In this section we define all parts for a simple gear mechanism. We start with defining a
Wheel. It contains the geometry and a method
graph to draw itself.
Next, we combine a set of
Wheels. The graphics method maps over the
Wheels and asks them for their graphics information.
We now create a specific gear box.
| Out[4]= |  |
| Out[5]= |  |
This box can transmit a given angular velocity.
| Out[6]= |  |
Finally, we look at a graphical representation. Please evaluate the next input to make the graphics active.
| Out[7]= |  |
In this example we look at the benefit of easy creation of similar, but not identical objects. We will model the deceleration of a chain of cars when the first car slows down. The position of car number
j is
zj[t] and its velocity
zj'[t]. The first car has number one. Because all cars are in a chain,
zj[t]<zj-1[t].
In order to avoid crashes of cars, the braking force increases with an increasing relative speed of cars and decreases with an increasing distance of cars. The simplest model uses proportionality, here with constant
A.
This force must be equal to the inertial one.
A reaction time of nonzero can also be modeled.
Let us model a
Car as a class. The possibility to access "previous" values by means of the
history field will be implemented below in a derived class.
The access to the
history is done in a
CompiledFunction in order to be faster. It could be included in the class as a class method.
Now, we add the caching of a
Car's history.
Again we outsource the access to the
history field for convenience to keep the class small.
A bunch of cars is easily created, each with the same weight, break and reaction time. Later, we will change this,
The behavior of the first car is arbitrarily assumed : starting with constant speed of 30m/s, then a sine term is added. All cars start with constant spacing and with the same velocity as the first car.
| Out[11]= |  |
We are ready for the simulation.
The
Cars' movement s controlled by the first car.
| Out[15]= |  |
Now, we assume that the driver of the second ca reacts slower. Please observe that the example does not detect a car crash in
Mathematica version 5.
| Out[19]= |  |
In this example we will model spherical lenses in two dimensions. We focus on a simple model, calculation speed is not an issue here. First, we derive a function to calculate ray-surface intersections.
We calculate the intersection of a ray starting at
{rx, ry} with angle

and a plane as well as a circle of radius
R, both centered at
{cx, cy}.
| Out[2]= |  |
| Out[3]= |  |
In order to reduce calculation time during the simulation, we optimize the intersection calculation and plug it into a function. The handling of the messages
::"indet",
Divide::"infy" and
Power::"infy" is necessary, because the optimization does not take care of branches in the solution of Reduce that are not evaluated due to failed conditions within And.
Numeric Check of Ray-Surface Intersection
The numeric values to check the ray-surface intersection also cover some special cases.
In the plot,the found intersections are marked with red dots, while the red lines are generated from the calculated angular coordinates of the circle at the intersection points. Rays are drawn in black with their starting point marked with a dot. The circle and the plane are drawn in blue.
| Out[10]= |  |
The direction of the new ray is generated by refraction.
| Out[11]= |  |
In order to get physically correct lenses, the simulation will prevent that two surfaces intersect. The check for intersection is obtained analogous to the surface-ray intersections above. Because we are only interested, if there is an intersection, we pick one set of values for
C[1] and
C[2]. The test is restricted to lenses centered about the x axis.
| Out[12]= |  |
Numeric Check of Surface Intersection
A short numeric test is executed.
| Out[18]= |  |
| Out[19]= |  |
A ray contains geometry information on the travelled path as well as methods to create and terminate ray segments. The interface is introduced to later separate rays and optical elements.
For convenience, we define a ray source.
The model's main part are the surfaces. They collect all surface-specific data and provide a method to refract rays. The interface is introduced to later separate rays and optical elements.
A lens in this model is composed of two spherical surfaces. In order to keep the model simple, we only will vary the radii and the thickness of the lens, while its position is kept fixed.
Optical System
Finally, rays and optical elements are combined in an optical system. The screen should be illuminated by parallel rays with a large diameter. Additionally, no rays should be lost.The beamQualities method tests how good the criteria are fulfilled.
The optical system to be considered is composed of two lenses and a screen.
A first trace is executed.
| Out[28]= |  |
Here is the tracing's result.
| Out[29]= |  |
| Out[30]= |  |
We now execute a simple genetic optimization. First, a set of 5 optical systems is created and traced.
| Out[32]= |  |
Out[34]//TableForm= |
| |  |
| Out[35]= |  |
From every generation, we drop the systems with the worst qualities and create a clone of those with the best qualities. The qualities are taken into account independently.
| Out[36]= |  |
Here is a sketch of the obtained qualities.
| Out[37]= |  |
The last optimization step shows the "best" systems found. Please observe that the system is far from being good. The example was kept simple and should only show how modeling can be done.
| Out[38]= |  |
| Out[39]= |  |
In this example a bottom up design of analog computer circuits is shown. Every part of the model is a block with connections to other blocks.After defining specific elements from these blocks and connecting them, the blocks yield a set of differential equations that describe the system behavior.
The basic element contains all general functionality to define elements, connect them and obtain equations. Elements may have one output and several inputs.
For a visual check, the block gets functionality to draw itself.
Here are a few specific elements.
Next, we create a simple system.
| Out[13]= |  |
The equations that describe the system are automatically generated.
| Out[14]= |  |
We simplify by means of the algebraic part.
| Out[15]= |  |
| Out[16]= |  |
We are now ready to solve the equations.
This system can easily be expanded to incorporate damping.
| Out[26]= |  |
| Out[27]= |  |
| Out[29]= |  |