Tag Archives: Wolfram

Tutorial

Distance…

Mathematica is perfect for working with lists and matrices.  Essentially making it useful for defining and manipulating geometry through rotations and later on analysing the forces through stiffness methods.

A key task when working with series of co-ordinates is to check that the relative distances between points don’t change so that the structural elements in between remain the same, even though the structure may have changed position and shape.  When you’re working with Mathematica and there’s a task that you need to do repeatedly it’s often worth creating a custom function that can be called whenever needed.

A couple of the functions that I’ve written can determine the distance between each co-ordinate in a list for 2D and 3D co-ordinate lists.

First, create some co-ordinates to work with on the examples.

Now that we’ve got some points to work with we can create a function to determine the distance between the points in 2D.  On 2D points, Mathematica has an inbuilt function called  EuclideanDistance[] which is nice and fast when determining the distance between 2D points.

There are two key parts to the function, the first part maps a list of points across the   EuclideanDistance[] function, with the second section essentially iterating the co-ordinates to restructure the list so that the function can be mapped across the list by rearranging it in a  (First point, Second point)(Second point, Third point) format…. until the last element in the list.

This is nice and zippy for 2D co-ordinates, but won’t work unfortunately for 3D and needs modifying to make use of the inbuilt function  Norm[] .

Running a comparison on speed for how 2D points are handled can be compared by zeroing out the Z ordinate.

As can be seen, EuclideanDistance[] is faster, but Norm[] is more flexible and can be used on 2D and 3D co-ordinates.

Dynamic Arches…

Tinkering about with SystemModeler a little further, I’ve managed to finally build a sprung arch, complete with dampers on the revolute joints.  I’m intending on using this principle in my research to create folded structures, so it’s interesting to see what effect the spring stiffness will have on the behaviour of the arch during the unpacking process – specifically looking at the accelerations on the masses at key points.

The thing that I was struggling with was creating a structure that had a set of equations that could be solved, the key concept I was initially missing was the closing of the structure with the special type of revolute joint to complete the chain.  Without this special revolute chain the equations are essentially unsolvable, so it’s important that one of these joints sits in the system somewhere.

Another concept is that the structure in the video has 3 straight segments, each 1m long; but the supports are only 2m apart…. forcing the arch to pop into a stable shape that balances the weights at each of the joints.  This is essentially what makes the arch wobble when solving the initial set of equations.  Next step is applying external forces and measurement points along the structure for displacement etc…

General

SystemModeler….

I’ve had SystemModeler for a while, but struggled to get on with it.  I guess the issue that I’ve had with it is that there are very few learning resources and tutorials around to help cut your teeth.  Mathematica has been around for years, so there are endless resources that are suited to the individuals preferred method of learning, from PDF’s, Stack Exchange, and even Lynda.com now has a video based learning method for the basics.  Given that even simple models have dozens of flags that can be plotted and graphed and that certain flanges aren’t enabled by standard, I’ve waited until I’ve had a few minutes spare to start tinkering.

Today I’ve been fiddling about with a mass and two springs with dampers, what I’ve been trying to do (and failed) is to make both supports displace over a given time period to push a displacement through the system to see what effect spring stiffness and damping have on the system, eventually this will be scaled up to a chain of springs of about 40-50 elements and used to model a structural system but this is my validation model.

I’ve not worked out how to give the supports a nudge (something that we do when modelling in ANSYS) so I’ve cheated a bit by essentially shortening the left hand spring to give a similar effect and then watched to see how the system has damped down. Parameter settings below for the spring/damper system.

By specifying the spring length as 1m, then setting the nominal value for it as 0.5m to start with it gives the system a nudge to start it bouncing.  I’m sure there’s another more elegant way of achieving this but I’ll have to figure it out over the next few days.

I’ll keep tinkering over the coming days, to try and refine the process.   Hopefully I can find some more learning materials or a decent community… something comparable to the Mathematica StackExchange community would be ideal.

If anyone’s come across a decent text or resource I’d love to hear about it.

Arc…

Following on from my last post where I was investigating how to determine the parabolic equation when only provided with three co-ordinates, I wanted to extend that knowledge and create a Mathematica sheet to allow me to do this without having to rely on Wolfram Alpha.  This is for two reasons, firstly if I don’t have an internet connection, then I’m screwed as Wolfram Alpha isn’t going to work for me… secondly I need to encourage a deeper understanding of the procedure and process if I’m going to get the most from Mathematica.

The first part is trying to determine the parabolic equation when I know three points on the curve, typically these are going to be the start, midpoint, and the end support point.  To give an appreciation of the geometry, it’s important to firstly understand that parabolic curves are defined by the common equation:

$f(x) = a{x^2} + bx + c$

Determining the length of a parabola with cartesian co-ordinates is done through applying the equation below over the limits being considered.

$Length = \int\limits_a^b {\sqrt {1 + {{\left( {\frac{{dy}}{{dx}}} \right)}^2}} dx}$

As three co-ordinates are known along the curve, the equations can be expressed as follows, with (x1,y1) (x2,y2) (x3,y3) being taken as the co-ordinate points… this gives a nest of simultaneous equations that can be solved using traditional matrix methods or in this case elimination could be used given that one of the input variables is 0.

$\begin{array}{l} ax_1^2 + b{x_1} + c = {y_1}\\ ax_2^2 + b{x_2} + c = {y_2}\\ ax_3^2 + b{x_3} + c = {y_3} \end{array}$

Taking the co-ordinates above as (0,500) (2050,0) (4100,500) for the input variables, this gives the simultaneous equations below to solve:-

${a{{\left( 0 \right)}^2} + b\left( 0 \right) + c = 500}$

${a\left( {2050} \right)_{}^2 + b\left( {2050} \right) + c = 0}$

${a\left( {4100} \right)_{}^2 + b\left( {4100} \right) + c = 500}$

However, Mathematica is well equipped for solving simultaneous equations and simply requires that the expressions are stacked up neatly and logically…. there can be a few problems with mixing bracket types, but this is just down to practice.  To solve the simultaneous equations above, the following code will solve them, the double equals signs are used just to push up the accuracy of the expressions and Reals is used as an additional parameter just incase any of the solutions are imaginary numbers.

Solve[{a x1^2 + b x1 + c == y1, a x2^2 + b x2 + c == y2, a x3^2 + b x3 + c == y3}, {a, b, c}, Reals]

Which will return the following solution:-

$a = \frac{1}{{8405}};b = – \frac{{20}}{{41}};c = 500$

Substituting this back into the general expression determines the parabolic equation for this particular curve to be…

$f(x) = \frac{{{x^2}}}{{8405}} – \frac{{20x}}{{41}} + 500$

The last thing to do is to work out the length of the curve between the two support points, this is a simple process determined by:-

$Length = \int\limits_0^{4100} {\sqrt {1 + {{\left( {\frac{{dy}}{{dx}}} \right)}^2}} dx}$

For those of us with too little time on our hands, this calculation can easily be automated and defined within Mathematica using the following code, where f'[x] is used to differentiate the term within the brackets.

f[x_]:=500 – (20 x)/41 + x^2/8405;

NIntegrate[Sqrt[1 + (f'[x])^2], {x, 0, 4100}]

Which gives the length of the cable to be  4,257.24m  which matches Wolfram Alpha’s answer perfectly… the next step is defining equal length links to segment this curve to start creating the geometry for parabolic cable-chain structures… that’s definitely a post for another day.

The intention with this post is to help anyone who is trying to define parabolas and arc lengths using Mathematica… or traditional methods, if they’ve got time on their hands…

Mathematica…

One of the reasons that I started this blog was so that I could mess about with embedding some Mathematica files to help with testing out some ideas.  For this to make sense it’s easiest if I embed a few simple examples in this blog post.  Now if you want to interact with these examples, I’m afraid you’re going to have to download the Wolfram CDF player, which is completely free and works on PC’s and Mac’s alike.  Imagine it as a sort of PDF viewer but it lets you interact with the files as opposed to a PDF which is typically just a static and lifeless document.

Consider the following equation:

$Sin\left( {2x} \right)$

Most text books would draw the graph for this over whichever range they deemed to be suitable and then students would try and learn from these dull and boring diagrams.

[WolframCDF source=”https://dl.dropboxusercontent.com/u/22612196/Wordpress_Test5.cdf” width=”653″ height=”405″ altimage=”” altimagewidth=”https://dl.dropboxusercontent.com/u/22612196/Wordpress_Test5.png” altimageheight=””]

Now this is how I was taught maths and in fairness, it’s pretty dull and it’s difficult to gain any form of intuition as to how it might behave if the 2 became a 3 for example, this is where Mathematica’s CDF files come in handy because it has some nice tricks for letting you explore maths in an interactive fashion… let’s consider the following equation, from the previous graph most people wouldn’t really know how it would affect the graph.

$Sin\left( {a.{\rm{ }}x} \right)$

But if we crank this through Mathematica we can create a really nice interactive widget that can be shared with anyone for free!  As you change the slider, the graph updates in real time, and if you want to know what number you’re changing ‘a’ to be then simply click the little + sign next to the slider itself to expand the input values beneath it.  In fact if you think that messsing with sliders is far too much like hard work, then simply click the little play button in the top right and the widget will work the sliders for you… sit back and watch the pattern.

[WolframCDF source=”http://dl.dropbox.com/u/22612196/Wordpress_Test4.cdf” CDFwidth=”752″ CDFheight=”574″]

If you’re not familiar with Mathematica, you may be concerned that this sort of widget is really difficult to create, but actually I’m still on Chapter 3 on the text that I’m working through and the code is incredibly simple to create this kind of interactive learning tool and I’ve replicated it below to show how few lines of text can create this level of interaction.

Manipulate[ Plot[ Sin[a x], {x, -10, 10}], {a, 1, 5}]

Essentially this code starts with “I want a slider widget”, “Plot me a graph of Sin(a.x) over a range of values for x from -10 to 10”, then “make the slider vary a from 1 to 5”.

Now this seems ok, but the Manipulate command is actually incredibly powerful and with a little more twiddling, high quality interactive 3D plots can be created, so let’s consider the following expression.

$f{\rm{ }}Sin\left( x \right) + g{\rm{ }}Sin\left( y \right)$

This expression has four variables: f,g,x, and y.  Of course, I bet you’re dying to know what the graph looks like for this function so you can boost your maths skills…

[WolframCDF source=”http://dl.dropbox.com/u/22612196/Wordpress_Test3.cdf” CDFwidth=”752″ CDFheight=”758″]

This is where the CDF player starts to flex its muscles a little, not only can you mess around with the sliders to change the values of f and g… but you can click and rotate the 3D graph itself to get a better view of how you think it’s working.  For me this level of interaction is a real opportunity for playing with the maths to help build up a level of intuition and feeling of how the maths will behave.  And once again the code to get it to work is fairly straight forward even for a novice such as myself.

Manipulate[Plot3D[(f ) Sin[ x] + (g)  Sin [y], {x, 1, 10}, {y, 1, 10}], {f, -10, 10}, {g, -10, 10}]

Now here’s the rub, a full Mathematica licence is the best part of £1,000 for a lecturer to use, in these hard times that’s a lot of money.  But because I carry ‘dual’ status as I’m studying 2 degrees as well as working full time as a lecturer I was able to pick up a student licence for roughly £80.  Normally the cost for a student licence is a shade over £100 but it is possible to reduce the normal student price by 15% by using the discount code PD1637 at the Wolfram store checkout and I still retain the full functionality of sharing my CDF files via export.

I hope this helps someone, if you’ve any feedback on this post or would like to ask any questions, please get in touch or leave a comment below.