Category Archives: Tutorial

Research Teaching Tutorial

Kriss Kross...

I've been really busy with various things associated with my new house lately and not really had much time to tinker and meddle with Mathematica or my PhD.  One of the key elements that I need to investigate is to calculate the internal area of a polygon where cables cross...  This will vary, depending on how many internal cables that I have and so I need a quick way of finding out the internal area for lots of different permutations.

Internal Area

This is a simple polygon problem which can be easily calculated once you know the cartesian co-ordinates for each of the key points using the following equation which is presented for the general condition.  It's not a hard calculation, as really it's just triangles, but it is a tedious calculation, which means it's perfect for Mathematica or Excel.

Area = \left| {\frac{{\left( {{x_1}{y_2} - {y_1}{x_2}} \right) + \left( {{x_2}{y_3} - {y_2}{x_3}} \right) \ldots + \left( {{x_n}{y_1} - {y_n}{x_1}} \right)}}{2}} \right|

However, the tricky part comes from determining where the cables actually cross over as that is where the cartesian co-ordinates will come from that are needed to determine the areas.  The calculation of the co-ordinates can be done a few different ways, but I've decided to solve the problem through using some vector based geometry, consider the simple condition below which is just for two lines that cross, how do we find the point where the lines intersect?

Pantographs

You could describe the two lines in the general term of y=mx+c, but this isn't what I want to do within Mathematica as I want to automate this for a wide range of values and lines and so iterative geometry using vectors is likely to be far more beneficial.  Representing these two lines in vector format using cartesian co-ordinates gives:

\begin{array}{l} Lin{e_1} = \left( {1 - \lambda } \right){P_1} + \lambda {P_2}\\ Lin{e_2} = \left( {1 - \mu } \right){P_3} + \mu {P_4} \end{array}

Now we know the equations in vector format it becomes possible to solve both of these equations to determine λ and μ which when back substituted into either equation will give us the co-ordinate for the intersection point p5 in (x,y) format.

This isn't that difficult to do by hand, but requires a bit of vector manipulation to expand the brackets and given my low attention span I would inevitably get it wrong after a few run throughs, but in Mathematica the problem gets much more simple, so for the example below we'll assume that the four co-ordinates are at 4m away from the origin (0,0) that is point p1.

{p1, p2, p3, p4} := {{0, 0}, {4, 4}, {0, 4}, {4, 0}};

Solve[{(1 - λ) p1 + λ*p2 == (1 - μ) p3 + μ*p4}, {λ, μ}, Reals];

p5 = Flatten[(1 - λ) p1 + λ*p2 /. %]

Executing this codes gives the intersection as being at point (2,2) in a fraction of a second, which is where you would expect it to be.  Now to explain the three lines of code and what they do, I'm sure a more experienced user could make this code much neater but doing it this way makes sense to my tiny brain.

The first line simply sets the variables p1-p4 with their corresponding cartesian co-ordinate position, giving the point two in the top right corner 4m up and 4m across, p3 is along the bottom 4m away, p4 is straight up in the top left corner 4m above point p1, as shown in the figure below.

Example

The second line then solves the two vector equations of the lines for λ and μ and makes sure that they are real numbers, rather than imaginary.  The real part probably isn't necessary, but it's just a force of habit that I put this in when using the solver as a just incase.

The final line substitutes the λ value back into the equation and then stores this as variable p5 so that I can use this in up and coming calculations, this was the fiddly part for me and makes use of the strange /. % input that allows the equation to effectively go back and substitute the solutions from the Solve command into the equation specified and recalculate and because my equation has the λ within it Mathematica knows this is what I want substituting in even without me being explicit.   I spent ages trying to work out this final stage, but now I've cracked it as a principle I can start to roll it out on the other sheets that I've been working on and this was the reason for writing this post incase it too helps someone else.  Now I can directly write the solution outputs from Solve, FindRoot, and NSolve and then carry on using these within my calculations rather than having to manually type them in.

This process lets me know that the intersection point is at (2,2) and now I know that the process works I can start to extrapolate this algebraically for the general conditions and start to calculate the areas of my polygons.

All of the figures in this post have been written using OmniGraffle, which I'm slowly getting to grips with, I can't make it dance as well as I can Visio, but I think it will help create some nice looking diagrams for my thesis once I get more proficient with it.

 

General Tutorial

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.

Screen Shot 2012-06-03 at 19.22.03

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...

Teaching Tutorial

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.

 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.

 

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...

 

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.

General Tutorial

Equations...

Turns out that my web provider now provides a SQL and PHP server with my base package and this was all the incentive I needed to give a self hosted WordPress blog a whirl.  To get a basic blog system up and running literally took me 5 minutes with the free WordPress software and using the export function I was quickly able to copy the few posts over from my free WordPress blog.

Now I have my own hosted service this presents me with several advantages over the free wordpress accounts, but there are two in particular that are attractive to me.  The first one is that I can now embed proper equations into a blog post using LaTeX and MathML by linking it into my equation editor MathType...

By adding in the 'LaTeX for WordPress' plug in for the hosted WordPress, I can now copy equations straight from MathType and paste them directly into my blog by following this procedure.

1.) Open MathType and prepare your equation.

2.) Go to MathType -> Preferences -> Cut and copy preferences; and then select MathML or TeX; then LaTeX 2.09 and later

Click

3.) Highlight the equation in MathType 6.7d and then right click and select copy or press (⌘ + C)

4.) Find the position in your blog post where you want the equation to appear, then paste (⌘ + V)

Following this means that I can embed equations like the one below pretty easily, the only downside that I've found is that if you've colour coded your equation in MathType, none of this formatting will carry over when pasting, but the equations should work and be visible in any browser, certainly the main three and on the iPhones and Android devices that I've worked on so far.

{F_d} = \overbrace {\sum\limits_{j \ge 1} {{\gamma _{G,j}}{\rm{ }}{G_{k,j}}} }^{{\rm{Permanent}}} + \overbrace {{\gamma _p}{\rm{ }}P}^{{\rm{Prestress}}} + \overbrace {{\gamma _{Q,{\rm{ }}1}}{\rm{ }}{Q_{k,{\rm{ }}1}}}^{{\rm{Leading}}{\rm{ Variable}}} + \overbrace {\sum\limits_{i > 1} {{\gamma _{Q,{\rm{ }}i}}{\rm{ }}{\psi _{0,{\rm{ }}i}}{\rm{ }}{Q_{k,{\rm{ }}i}}} }^{{\rm{Other Variable Actions}}}

With minimal tweaking and a little trial and error with the LaTeX code I was able to apply some colour tags to get the equation to look the same as it does in my lecture notes.

{F_d} = \overbrace {\sum\limits_{j \ge 1} {{\gamma _{G,j}}{\rm{ }}{G_{k,j}}} }^{{\rm{\color{Red} {Permanent}}}} + \overbrace {{\gamma _p}{\rm{ }}P}^{{\rm{\color{Red}{Prestress}}}} + \overbrace {{\gamma _{Q,{\rm{ }}1}}{\rm{ }}{Q_{k,{\rm{ }}1}}}^{{\rm{\color{Red}{Leading}{\rm{\color{Red}{ Variable}}}}}} + \overbrace {\sum\limits_{i > 1} {{\gamma _{Q,{\rm{ }}i}}{\rm{ }}{\psi _{0,{\rm{ }}i}}{\rm{ }}{Q_{k,{\rm{ }}i}}} }^{{\rm{\color{Red}{Other Variable Actions}}}}

This might initially appear to be quite a minor thing, but I've found that colour coding my notes like this really helps the students follow the equations when I'm talking them through various parts of the equations and so I was keen to keep the high quality formatting on my blog.  One thing that I have noticed however though is that the equations appear much much sharper on mobile devices and Apple machines, whereas on windows machines they appear slightly pixelated.

As to the second advantage this is that I can now embed Wolfram Mathematica CDF files into my blog directly, which will help me share some of my examples with anyone interested in my research.  I'll write another blog post on this over the next few days...