Newton-Raphson Fractals with ChatGPT
Finding roots of z^4-1.

Newton-Raphson Fractals with ChatGPT

I am proficient in seven programming languages. Javascript is not one of them. Neither is HTML. And my mathematical mind has always read CSS as C-- (S -> subtract -> minus sign) and thus somehow the opposite of C++, which is way off the mark. That means this little project with ChatGPT (version 4o, I think) is not just my first foray into any sort of web development but also an exploration with languages about which I know next to nothing.

The premise is simple. Anyone with a background in mathematics knows that fractals are stunningly complex patterns created from simple formulas. (They're really geometric objects with "fractional" dimension, but let's not go down that tangent.) One popular way of creating fractals is using the Newton-Raphson method for finding polynomial roots. Since the math for creating these "Newton Fractals" is very simple, it makes a good test case for ChatGPT. Since my ability to evaluate Javascript is limited, it makes a good test case for debugging by prompt engineering and intuition.

ChatGPT struggled in two areas.

  1. Figuring out how to parse the polynomial. Since I didn't want to have to try to figure out a regular expression parser, in some sense ChatGPT was on it's own. I just pointed out when and, to the extent I could tell, how a polynomial was being parsed incorrectly. I mostly took the role of a unit tester, telling it when something didn't evaluate properly. Each time, ChatGPT was able to find and fix the problem. In one case it even picked up a problem I had missed; the subtlety of converting between x's (real variables) and z's (complex variables).
  2. Complex Numbers. ChatGPT needed prompting to come up with a Complex class (called a function in Javascript; no wonder I don't get this language) even though fractals are inherently complex. It needed a bit more prompting to fully integrate the Complex class with the rest of the code.

I mostly ignored the text surrounding the code. As a rule, I don't need ChatGPT telling me what it's going to do before writing the code and, after the code is written, what it's done. I suppose I'll have to wait for ShakeGPT for an LLM that realizes brevity is the soul of wit.

The code comments were pretty good. Certainly better than I would have bothered with. The only editing I had to do for index.html was to switch out styles.css for style.css. ChatGPT had a little confusion with plurals. (Thanks to Dave Holmes-Kinsella for catching that bug.)

The code can be found here. The page can be tested here. I'm no artist; I stuck with the default color template, which works better with some polynomials than others. Plenty of other bells and whistles, such as changing the scaling or adding a zoom feature. I'll get to those at some point. As a PoC, and less than an hour's work, it's pretty decent.

Warning: Might be a bit slow. If you try it, give it a few seconds.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了