## Math mistakes; driving me crazy

Last night I kinda lost steam on the programming part of this project so I was just noodling around doing Google searches for other interesting functions to plot. One that appealed to me was the “butterfly curve” which has the following parametric equations:

At first I thought I couldn’t do this (yet) as my language is missing an exponential function (trivial to add, just haven’t done it) but then, duh, I realized I do have a power function and all I needed to do was stick a fairly precise value of e in there (I used this much, 2.7182818284) and, as we used to say, it would be good enough for government work.

So here’s what I got:

Vaguely interesting but NOT the right answer. And worse, last night (and I neglected to save the results and now can’t reproduce them) it didn’t look like this and it was rotated some as well.

Well, this was driving me crazy – is this a bug in my “language” and my interpreter. Now as I’ll get to later I have another curve that I got wrong and that too was driving me crazy so this morning I just reused some of my code and built a “test” plotting routine where I have to code expressions directly in C# (harder to change than my interpreter approach, but still fairly quick) and so I got:

which is the “right” answer, although as I’ll continue later, it’s not exactly the same as some of the other published curves. So now I thought I must have some bug to repair in my code UNTIL I noticed my real mistake:

See my mistake – stupid, too much late night (and maybe a little too much bourbon) but a simple misreading of the equations got the wrong answer (and again my actual answer was even more wrong, some other mistake I can’t recreate in morning light).

So after hardcoding the butterfly in C# and getting the right answer, then noticing my mistake and coding the butterfly in my “language” (I guess I’ll have to think of a name for it instead of just always saying “my language”) everything worked out, but not quite. Various images I’ve found of the Net have more loops and density – duh, what happens if I run this for more cycles. And lo and behold:

That’s two.

And five (had to change scale as this got bigger). And

And fifty. Then it doesn’t seem to change much.

So as an exercise for you, Dear Reader, why do any more iterations make any difference at all?

But now on to my second mistake, which is even more galling. But actually I have to write some code now to even write this post and, possibly, like the example above find yet another of my mistakes.

See addendum below this original content.

In my late night searches for more curves I stumbled onto the following curve, sometimes labeled as the “mystery curve” (you can search for it and get numerous hits).

I think this is a really beautiful curve. It’s getting more attention as it’s described in a book, Creating Symmetry: The Artful Mathematics of Wallpaper Patterns,  and discussed here, and with an interactive demo here.

So this looked like fun but I ran into this problem. Here is the equation that usually is used to generate this curve:

That’s fine except for a serious detail – this is using complex numbers instead of the ordinary x,y parametric equations and my program doesn’t do any of this. In essence you just iterate t and then plot the real and imaginary part (as your x,y) to get the visualization of this equation. I didn’t get anywhere, with searches, to find a x,y parametric equivalent so I’m sorta out of luck, although now considering for my programming todo list adding complex numbers.

But, as I’ve had no reason to use complex numbers since I first learned about them in high school over 50 years ago I had to “review” a little of their math, with the goal of creating my own numerical routines for the needed operators (add, multiply, and exponential, with the exponential being the tough one (I quickly remembered how to do add and multiple)).

Then, in doing my review I came across Euler’s formula,

(hey, all you math guys out there, give me a break. I’m a software architect (with degree in materials science) so my math learning stopped after the compulsion, 18.0x courses at MIT. I know this stuff is trivial for you but I haven’t needed this kind of math since passing final exams)

So, there I’ve got it, the real part and the imaginary part. So I did a little simple cranking, just to be careful showing my work, converting the terms in the complex parametric of the mystery curve, expanding with Euler’s formula:

 eit cos t i sin t -1/2 e6it -1/2 cos 6t -1/2 i sin 6t 1/3 e-14it 1/3 cos -14t 1/3 i sin -14t sum cos(t) – 1/2cos(6t) + 1/3cos(-14t) i sin(t) – 1/2sin(6t) + 1/3 sin (-14t)

And now I’ve got it, these I can run through my program, and, here’s what I got:

Close but no cigar. My figure has the much more boring completely symmetric rendering (although with the same overall 5-fold symmetry) vs the real answer which has some beautiful asymmetry to it.

So what’s wrong! Lots of noodling around didn’t get me any closer, stubbornly everything I tried created shapes (some nice) but with too much symmetry. So I went to bed, finally, disappointed I couldn’t get the right answer and debating how to solve it, which led my to the programming exercise I’m about to do. I did find I didn’t need to compute operations on complex numbers because Microsoft has done it for me, in the .Net System.Numerics namespace which I am now going to try (in my stripped down plotting routine where I could equations in C# instead of my language. Let’s hope it works!

Well, there’s my first issue, can’t find that namespace in Visual Studio (my hobbyist, rather than professional version). Let’s see if I need a reference. Yep, that solved it. Onward. Never having used this class in C# I may screw this up.

Nope, got it right, first time even, with some very cautious C# code, wie so:

```Complex t1 = new Complex(0, t);
Complex t1e = Complex.Exp(t1);
Complex t2 = new Complex(0, 6 * t);
Complex t2e = Complex.Exp(t2) / new Complex(-2, 0);
Complex t3 = new Complex(0, -14 * t);
Complex t3e = (Complex.Exp(t3) / new Complex(3, 0)) * new Complex(0, 1);
Complex u = t1e + t2e + t3e;

double x = u.Real;
double y = u.Imaginary;```

So, not that it’s a surprise I do get the right answer. So what in blazes went wrong with my math diddling to do the expansion myself.

So, math aces out there, some help please. Unlike the first example, where I found my mistake I’m not spotting my mistake in this one. But at least I have one idea (probably wrong).

So I thought I’d just check and do it again in my language, and, oh glories, don’t even get the same wrong answer:

Cute, but not right!

Don’t see any typos in my “program”

```a sin(T -14 0 0.333333)
b sin(T 6 0 -0.5)
c sin(T)
d rpn (a b + c +)```
```f = cos(T -14 0 0.333333)
g = cos(T 6 0 -0.5)
h = cos(T)
i = rpn(f g + h +)```
```x = value(d)
y = value(i)```

So, now will my one hack do any good.

Guess not, so the mystery curve remains a mystery for how to represent in x,y parametric equations and/or to code it properly.

Let’s try a range of sin(t) – 1/2sin(Bt) + 1/3 sin (-At)

Some cute curves but nothing even close.

Now one more try before I give up. The expansion I did looks a lot like the “magic curve” (various searches reveal use of that name), so let’s just try it (where’s my code for that?)

Nope, it’s not the magic curve, phooey. So where am I going wrong!

I guess I’ll just have to wait for the math guys to help me out, but one last search to see if I can find the parametric equations for this. Nope, nada (except one hit where someone says to apply Euler’s formula, done that, been there, didn’t work for me. I guess I’ve got my dummy hat on!)

Enough, got other things to do.

Stay tuned.

Addendum: While a Google search of a standard x,y parametric for this complex equation didn’t come up with an answer I happened to randomly stumble on a proposed answer (here) and it looks like:

Some my expansion was all wrong! But why, come on math aces, give me the explanation! Note the two key differences in correction expansion: a) all the minus are gone, b) the third term switches sine ⇔ cosine.

So here’s what I got with the correct parametric equations,

Now assuming symmetry in the equations there are 4 combinations of sine/cosine in the last two terms and four combinations of sign in the last four terms or sixteen possibles to try to guess the right answer:

A little hard to see but I have to shrink to fit in WordPress, but here are all sixteen combinations with only one right (but it has a similar twin).

So problem solved, EXCEPT, I have no clue how to do the conversion from complex form to x,y parametric form; clearly simple-minded use of Euler’s Formula doesn’t work, but why not?

Second Addendum: The source for the parametric equation is WRONG. The second term should be a subtract!

See the difference? It’s subtle! In the wrong one (+ second term) the top peak is slightly phase shifted to right of the polar grid and in the right one (- second term) it’s shifted slightly left, which matches both the result I got with my plotting of the complex function and also the original source.

So somebody else screwed up their conversion of complex to parametric also.

old fat (but now getting trim and fit) guy, who used to create software in Silicon Valley (almost before it was called that), who used to go backpacking and bicycling and cross-country skiing and now geodashes, drives AWD in Wyoming, takes pictures, and writes long blog posts and does xizquvjyk.
This entry was posted in comments, development, experiment and tagged , , . Bookmark the permalink.

### 1 Response to Math mistakes; driving me crazy

1. Para says:

Hi

Nice blod. Pl. take a look at \

You will see these and other parametric equations plotted.

Like