In search of efficiency

My equations work by rotating some stuff, then splitting that stuff into triangles and calculating some stuff of each triangle. However, my MATLAB implementation splits it up into triangles before rotating (due to the way a particular function in MATLAB works). What this means, is that for an n-sided polygon, the MATLAB code is calculating 3n-6 triangles, where my equations only require n triangles. For example, a hexagon will take 12 triangles in the code, but only needs 6. What that means is that the code is running many many more calculations than it needs to. It’s still correct, it’s just doing it inefficiently.

I’ve been considering this problem for a while now, and yesterday I started trying to solve it. Like I mentioned above, it’s still correct, but it’s slower than it needs to be. One of the motivations for coming up with these equations is that they are much faster than using computer simulations, so we want these equations to be implemented efficiently to increase that advantage.

It turns out that this problem is much more difficult to solve than I anticipated. It’s making me think and try various methods – and I’m loving it! This is the sort of stuff I love doing: problem solving. Working out ways to make stuff better.

Right now my code is calculating what I want in the order of ~30-50 milliseconds, but I think it could be reduced to 20-30 milliseconds with some tweaking. Hopefully I’ll be able to work this thing out!

Confused happiness

Recently I’ve been thinking about the magnetic field for a special case of geometry. Basically, if you have a 3D magnetised Pacman, what happens to the magnetic field inside its mouth?

I thought the field would be downwards, since the “upper mouth” is closer to the top of the magnet than the “bottom mouth”. It made sense to me, since magnets can be modelled as charged plates, and if the top mouth is closer to the top of the magnet, shouldn’t it have a higher magnetic charge than the bottom mouth? If so, then the field would flow downwards.

However, today I ran a simulation on that style of geometry. The results showed that the field went upwards inside the mouth. This really worried me, because I was pretty sure my equations wouldn’t work for this type of magnet, rendering them useless for some cases. I was also confused – is the simulation correct? Surely it is. Do I understand magnets? My logic felt so solid, and I started to doubt myself a little.

But it turns out my equations work for this! I was confused because I didn’t understand why the field was upwards, but incredibly happy because my work is correct! I was very concerned about this type of shape (non-convex polyhedron). I knew it worked for convex, and it seems to work for non-convex too!

Later on in the evening, I was of course still thinking about it and why it works the way it does, so I opened up my textbook and started reading. I looked at the basic equations and considered all the variables and what happens to these equations, then it all slowly clicked in my head. It all makes so much sense. Even though the top mouth is closer to the top of the magnet, the outward-facing normal vector is roughly in the opposite direction as the magnetisation vector, meaning that it’s more like a negative magnetic charge.

Today was a bit of a rollercoaster, going from thinking I understood, to being super concerned when the simulation finished, to confused relief when my equations worked, to content when I understood what was going on.

My work seems really promising, and I’m keen to extend it further soon!


Yesterday and today I’ve been trying to verify my equations. I’ve been working on these for about a month; I’ve put in so much work and effort. Yesterday I managed to get FEM working – that’s running a computer simulation of magnets. It’s not 100% accurate but it gets very close. I ran it on a triangular prism, something that doesn’t have published equations for (that I know of). The main advantage of using FEM is that most of the time you can’t analytically calculate the field or forces, so FEM is the only option.

Once I got the FEM results, I compared them to that of my equations. I got about a 10% error. That’s small but still considerable. So I reduced the tolerance of the FEM, so the results were more accurate at the cost of a longer simulation duration. This time, I got an error of roughly 2%, indicating my equations are correct!

Today I modified my code a bunch to make it better. For example, in my old code, you’d have to input the points of each polygon facet of the polyhedron. Now, you just have to put in the vertices of the polyhedron and MATLAB will calculate the convex hull of the points (i.e. giving a convex polygon with those points). I was able to calculate the field at any point for a tetrahedron in approximately 50 milliseconds. This is pretty amazing, considering an accurate FEM simulation could take minutes or hours. As the polyhedron gets more complex, the equations are slower, but they are still very fast.

Today I also managed to use my code to plot the field of a small Halbach array. This produced results as expected, again indicating my equations are correct.

I’m really happy with my results so far! I was always worried that my equations would be wrong and I’d have wasted a month, but everything is going so well.

There are a few things I want to do next. One is run simulations on various polyhedrons and compare that to my results. This will support my claim better. Secondly, I want to create a Halbach array with my code, but using non-cuboid magnets to see what happens. And finally, I need to start writing some stuff for my mid-first-year-review. I don’t think the review will be difficult but it’s always a good idea to get a start on it as soon as I can.

Right now things are looking pretty great for me!

Back on track!

Today I decided to write some code to actually compute and process all the work and thinking I’ve done recently. There were many hiccups along the way, but I think it’s working exactly as I hoped! I need to do a bunch of testing but it all looks good so far. I’m going to show my supervisor tomorrow – I think he’ll be really happy with it. With this code, I can actually get some results and compare them to computational simulations to check their accuracy. I might be getting a little ahead of myself though, because I still need to make the code more modular and improve a bunch of things with it. If I can do that, I can hopefully start really testing things.

One problem with my code so far is that inputting data is very laborious, so if I can come up with a better way of doing it, it’d benefit me quite a lot. There are a few things I’ve come up with but I need to spend a lot more time working on them, as well as do a bunch more research on how to get them working. The method I’m thinking is probably not very efficient, but it is code that gets executed rarely so it doesn’t matter too much. I’m much more interested in optimising the “loop code” – code that runs many thousands or millions of times per execution. Lately I’ve been using a lot of things that come from 3D geometry. It’s taking me all the way back to highschool (although still far more complicated!), and it’s really reminding me of how far I’ve come since then.

Tomorrow I want to show my supervisor my code (and some preliminary results from it), and either start on my literature review or work on my code more. Right now I think I’m in a really good spot!


Lately I’ve had a bit of trouble keeping my motivation. There has been some stuff from my personal life interfering with this project, but I’m trying to minimise the effect of that. The last few days I’ve felt a lot better though, and I think I’m getting my motivation back!

One thing that messed up my motivation was finding a particular paper the other day. I’ve spent the last month or so trying to do a certain thing, thinking no one else has done it. But then I found a paper from a few decades ago, which did a very similar thing to me. Finding this destroyed me a little – I thought the work I was doing was new and exciting, but it turns out people have thought about this before. I guess that’s one of the things I should learn to deal with as a researcher, since it’s likely it’ll happen a fair bit.

The upside to this, though, is that the paper was pretty terrible. They didn’t have a conclusion, they didn’t discuss their results, there were no graphs, etc. They said in their abstract that extensive numerical tests have been done, but there was no evidence of this, let alone evidence that their equations were correct. Additionally, they left out a lot of details, such as how they came up with their coordinate systems and how to transform between systems. The transforming between systems is quite an old idea, but it’s still important to at least mention. And finally, although they did something similar to me, they used a different method, meaning I might still have something new. I will discuss this with my supervisors and see what they think – I feel like there is still scope to produce some new work on this.

Due to the loss of motivation, I haven’t really gotten too much work done lately. I’ve spent a lot of time thinking about some ideas on how to solve certain issues. I think I know what I need to do to get the result I’m looking for, it just requires quite a lot of work and thinking. It’s one of those things where you need to really think hard about it before the idea makes sense in your head.

This update doesn’t really feel like a positive one, but I’ll try to leave it on a good note. I think there’s scope to continue the work I’m doing and maybe produce new research on it. There are some cool things looking to happen in my personal life soon, too. And I feel like this motivation roadblock is over, and I can continue doing what I’m good at now. I think one thing I need to work on is reading more papers – if I had have read that paper earlier, I wouldn’t have been as stressed!

I think there are good things to come now!

I think my idea worked!

The last few days I’ve been working on an idea I had. Basically I’ve been trying to “cut up” polygons in certain ways to calculate the magnetic field around them. I came up with a bunch of equations for this and today I implemented the algorithms. They seemed to have worked!

The next step was to verify that my solutions were correct. I’ve not fully verified anything but they seem to be on the right track. I modelled a cuboid magnet using these methods and compared it to the field calculated from the analytic equations I derived a few weeks ago. The results were identical. Then I found an analytic equation for the field along the axis of a cylinder magnet. I modelled an n-sided polygon (where I could vary n) and calculated the field along the axis using my methods. As n increased (as the polygon approaches a circle), the results began to match up!

I think the next major step here is to learn FEA with magnets and simulate polygonal magnets. This is a much more versatile approach than finding analytic equations since these equations don’t exist for many shapes. It’s a fantastic way to verify my solutions for many different cases with many different geometries.

This seems to be an area which hasn’t been researched too much, so there’s a chance I could come up with some new stuff here. I want to extend this idea further, but it’ll take a lot of work. But I think I can do it!


Today I managed to reproduce the result from a paper I’ve been reading about triangular magnets. One way to calculate the field around magnets is by pretending they are charged plates and using field equations on those plates. This paper concerned triangular plates.

On the first read of the paper last week I thought it was pretty useless – it was a specific case of the calculation from another paper. Then I started thinking about it, and realised it could be used as a “cheat code” for any polygon. Any polygon can be broken up into a finite number of triangles, so the field equations for triangles can be used to calculate the field around a polygon. If the polygon is broken up the right way, this paper presents a set of equations that can be used to find the field of the entire polygon – although this needs a lot of work to make happen.

I managed to reproduce the y-component today. The x-component should be very similar, and the z-component might take a bit of work. Once I find all three components, I want to extend it a little. The paper talks about right angled triangles, but I want this extended to acute triangles at first, then obtuse a bit later. This would reduce calculation time pretty significantly. Once that’s done, I want to apply rotational transformation matrices to generalise it further. Maybe a bit later on, I could also apply linear transformations to fully generalise it.

I believe, with a lot of work, that I could come up with a general set of equations for the magnetic field around a general polygonal-prism. Maybe way down the track this could be extended to a general polyhedron. This would require a lot of maths, but would be a fantastic result.

I’m not sure how much research has been done in this area, but it doesn’t seem like there’s a huge amount. I wonder if I could find a little gap somewhere and make it my specialty? Time will tell.