Title: de Moivre's theorem in VBA Post by: Iariak on January 30, 2016, 09:49:11 PM So basically, I wrote a little 'program' that can draw 2D fractals in Excel using VBA. Recently I tried working the de Moivre's theorem into it so that I can get different powers of Z easier. It worked, not entirely though. Here is the code.
Code: Sub Grid() In case you wanted to try it out, run Grid first, then run Draw. The exponent is set to 4, you can change it to whatever. Now the problem is that for some reason this doesn't work at all with odd exponents :'( When I try to render something with odd exponent, the image has weird cut-outs at top and bottom. Even exponents work just fine. If anybody could tell me why it doesn't work, that would be awesome. I also wanted to try rendering Glynn Fractals which require fractional exponents, from what I understand though, the Formula doesn't really work with fractional exponents? What options would I have if I wanted to get that to work if any? I know VBA and Excel is not suitable for this kind of stuff at all :D Title: Re: de Moivre's theorem in VBA Post by: cKleinhuis on January 30, 2016, 11:22:24 PM a picture says always more than a thousand words ;)
glynn fractals indeed need fractional exponents, fractional exponents are very easy to calculate once you get the idea: multiply the distances, add the angles ;) which means take the atan2 of x/y and the lengthes of your vectors, multiply the angle by your exponent, and exponentiate your length to get the desired fractional complex multiplication outcome, basically this i have tried to present in my complex multiplication tutorial ;) https://www.youtube.com/watch?v=9n5kve9HzRo Title: Re: de Moivre's theorem in VBA Post by: Iariak on January 31, 2016, 10:55:59 AM Thanks! I made some pictures...as you can see, the sets with odd exponents don't look how they're supposed to.
(https://farm2.staticflickr.com/1573/24357702619_909987f585_b.jpg) (https://flic.kr/p/D7pAnZ)Multibrot Sets (https://flic.kr/p/D7pAnZ) The fractional exponents are very easy to calculate once you get the idea: multiply the distances, add the angles ;) which means take the atan2 of x/y and the lengthes of your vectors, multiply the angle by your exponent, and exponentiate your length to get the desired fractional complex multiplication outcome, basically this i have tried to present in my complex multiplication tutorial ;) I believe you are refering to this?if i is the real component and j is the imaginary component of a complex number then to get any power of that number you do. Angle = Atn(j / i) Mag = Sqr(i * i + j * j) (This would be the distance from origin, absolute value, magnitude or whatever you wanna call it) iNew = (Mag ^ Power) * Cos(Angle * Power) jNew = (Mag ^ Power) * Sin(Angle * Power) I already knew that, I cannot get Glynn fractals to appear using this method though and it also doesn't appear to work with odd exponents. Title: Re: de Moivre's theorem in VBA Post by: cKleinhuis on January 31, 2016, 11:18:07 AM thats right, this is exactly the way how complex exponents are calculated
for the odd ones, are you sure you implemented it correctly ? if you are using some kind of recursion this might be a problem, basically integer exponentiation is a repeated multiplication with the source, so odd exponents should not lead to the cut out parts of your image although it looks like a "nice error " ;) Title: Re: de Moivre's theorem in VBA Post by: Iariak on January 31, 2016, 12:02:43 PM Well, I am absolutely NOT sure I am implementing it correctly. I can't think of a reason why it wouldn't work with odd exponents. Thanks for the help though, I will look into it :)
Title: Re: de Moivre's theorem in VBA Post by: M Benesi on February 04, 2016, 06:54:16 AM Well, I am absolutely NOT sure I am implementing it correctly. I can't think of a reason why it wouldn't work with odd exponents. Thanks for the help though, I will look into it :) Hmm. I don't think atn returns the same values as atan2, which might be the problem. https://en.wikipedia.org/wiki/Atan2#Definition_and_computation I don't want to lead you on a goose chase... so try an implementation in which you take the absolute value of the angle parts of inew and jnew and see if it's continuous for odd n: iNew = (Mag ^ Power) * abs(Cos(Angle * Power)) + i jNew = (Mag ^ Power) * abs(Sin(Angle * Power)) + j If that solves the continuity problem, you might want to code in atan2. Title: Re: de Moivre's theorem in VBA Post by: Iariak on February 05, 2016, 10:18:02 AM Hmm. I don't think atn returns the same values as atan2, which might be the problem. That was totally it! I used Atan2 and everything works now, even Glynn fractals :D thanks a lot!Title: Re: de Moivre's theorem in VBA Post by: M Benesi on February 06, 2016, 07:37:06 AM Awesome. Glad I could help. Now, I need you to code a universe in Excel. O0 :D |