Jump to content

Chaos Theory on your AutoCAD


Lee Mac

Recommended Posts

Many of you have probably heard of Chaos Theory, and, if you haven't - its a fascinating area of Mathematics to delve into.

 

There is the well-known saying:

 

"If a butterfly flaps its wings in China, there would be a hurricane half-way around the world..."

 

Although this quote of course cannot be proven - it does capture the essence of Chaos Theory in that small deviations in a system can amplify themselves over time to bring about Chaotic Motion.

 

So, I thought I'd give you an example of Chaos Theory for you to try on your AutoCAD...

 

 

This particular well-known example is called the Logistic Map, and it can be used to model populations among other things.

 

I have created two program for you to play with: Logistic and Bifurcation.

 

The function we are dealing with is a simple one, which is why it is surprising that its behaviour leads to Chaos:

 

x -> kx(1-x)  0 < x <= 1   0 < k <= 4

Hence a value "x" is mapped to kx(1-x) where "k" can take any value between 0 and 4.

 

We shall iterate this function, meaning that we will take a starting value of "x" and map it to kx(1-x), then we shall take the result of the mapping and use it as our new value of x. Hence we are ultimately solving the intersection of f(x) = kx(1-x) and f(x) = x.

 

My first function: "Logistic" will allow you to experiment with this iteration, using different values of k.

 

Open a new Drawing in AutoCAD, and try k = 2, 2.5

 

Now see what happens when you use k = 3, 3.5, 3.7, and, ultimately 4.

 

Here is what you should see for k = 2 & 3:

 

K = 2:

 

k=2.png

 

K = 3:

 

k=3.png

 

 

You will see that as you increase the value of "k", the behaviour will become ever more chaotic, and the iteration will no longer converge to a point.

 

My second program "Bifurcation" demonstrates an extension of this phenomenon, known as Bifurcation Theory.

 

If we plot a graph of k against the values of x retrieved when we run the iteration, we can see how the behaviour develops.

 

You will notice that for 0

 

Bifurcation.png

 

Have a play with the code, and, if you have any questions, just ask. :)

 


;; Function to demonstrate the Chaotic Behaviour of
;; the Logistic Mapping.
;;
;; Author: Lee McDonnell (Lee Mac) 24/09/2009
;;
;; Logistic Mapping Function: x -> kx(1-x)
;;
;; 0 < x <= 1
;; 0 < k <= 4
;;
;; System enters a Period 2 Orbit at approx. k = 3
;;
;; More information can be found at:
;; http://en.wikipedia.org/wiki/Logistic_map
;;
;; =======================================================


(defun c:logistic (/ *error* spc doc k x0 flag y0 eLst)
(vl-load-com)

;; Error Handler

(defun *error* (e)
(if doc (vla-EndUndomark doc))
(and eLst (mapcar 'vla-delete eLst))
(or (wcmatch (strcase e) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " e " **")))
(princ))

(setq spc (vla-get-ModelSpace
(setq doc (vla-get-ActiveDocument
(vlax-get-acad-object)))))

;;-------------------------------------------------------------;;

;; Function: x -> kx(1-x) { 0 < x < 1 }

;; Want to iterate: x{n+1} = kx{n}(1-x{n})

(while (and (not (initget 7))
(< 4 (setq k (getreal "\n{ x -> kx(1-x) } k = "))))
(princ "\n** Value of k must be less than 4 **"))
(vla-StartUndoMark doc)

;; Make Axes:

(vla-put-Color
(car
(setq eLst
(cons (mkline '(0 0 0) '(1 0 0)) eLst))) acYellow)

;; First derivative of the function to get Maximum
;; f(x) = kx(1-x) -> f'(x) = k - 2kx

(vla-put-Color
(car
(setq eLst
(cons (mkline '(0 0 0) (list 0 (/ k 4.) 0)) eLst))) acYellow)

;; Plot Curve for value of k

(vla-put-Color
(setq Curv
(mkSpline
(append (list 0 0 0)
(list 0.5 (/ k 4.) 0.)
(list 1 0 0))
(list 1 k 0)
(list 1 (- k (* 2 k)) 0))) acGreen)
(setq eLst (cons Curv eLst))

;; Plot Line f(x) = x for Iteration

(vla-put-Color
(car
(setq eLst
(cons (mkLine '(0 0 0) '(1 1 0)) eLst))) acRed)

;; Zoom to see it all...

(vla-ZoomWindow
(vlax-get-acad-object)
(vlax-3D-point '(-0.01 -0.01 0))
(vlax-3D-point (list 1 (+ 0.5 (/ k 4.)) 0)))
(vla-regen doc acActiveViewport)

(setq flag t x0 0.1 y0 0.)

(getstring "\nPress Enter to Begin Iteration...")

;; Begin Iteration:

(while flag

(setq test (mkLine (list x0 y0 0) (list x0 (1+ y0) 0)))

(setq iPt (vlax-invoke test 'IntersectWith Curv acExtendThisEntity))

(vla-delete test)
(vla-put-Color
(car
(setq eLst
(cons (MkLine (list x0 y0 0) iPt) eLst))) acCyan)
(vla-put-Color
(car
(setq eLst
(cons (MkLine iPt (list (setq x0 (cadr iPt))
(setq y0 (cadr iPt)) 0)) eLst))) acCyan)


;; End of Iteration

(initget "Yes No")
(if (eq "No" (getkword "\nContinue? [Y/N] <Yes> : "))
(setq flag nil)))


(and eLst (mapcar 'vla-delete eLst))
(vla-EndUndoMark doc)
(princ))


;; Make VL Spline

(defun mkSpline (lst t1 t2)

(vla-AddSpline spc
(vlax-make-variant
(vlax-safearray-fill
(vlax-make-safearray
vlax-VBDouble (cons 0 (1- (length lst)))) lst))
(vlax-3D-point t1)
(vlax-3D-point t2)))

;; Make VL Line

(defun mkLine (p1 p2)

(vla-AddLine spc
(vlax-3D-point p1)
(vlax-3D-point p2)))

;;-------------------------------------------------------------;;
;;-------------------------------------------------------------;;
;;-------------------------------------------------------------;;

(defun c:bifurcation (/ *error* spc doc k x0 kinc x eLst)
(vl-load-com)

;; Error Handler

(defun *error* (e)
(if doc (vla-EndUndomark doc))
(and eLst (mapcar 'vla-delete eLst))
(or (wcmatch (strcase e) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " e " **")))
(princ))

(setq spc (vla-get-ModelSpace
(setq doc (vla-get-ActiveDocument
(vlax-get-acad-object)))))

;;-------------------------------------------------------------;;

;; Function: x -> kx(1-x)

;; Want to find limit of x{n+1} = kx{n}(1-x{n}
;; for various values of k.

;;-------------------------------------------------------------;;

;; -- Adjustments --

(setq k 1.) ;; Initial K value

(setq x0 0.1) ;; Initial x value

;; -----------------

(setvar "PDMODE" 0)
(setvar "PDSIZE" 0)

(vla-StartUndoMark doc)
(vla-ZoomWindow
(vlax-get-acad-object)
(vlax-3D-point '(1 0 0))
(vlax-3D-point '(4.5 1.5 0)))

(setq kinc 0.01)

;; ===== Single Point =====

(while (<= k 2.9)

(setq x x0)

;; Converge the Point

(repeat 40
(setq x (* k x (- 1 x))))

;; Plot the rest

(repeat 30
(setq eLst
(cons
(vla-AddPoint spc
(vlax-3D-point (list k (setq x (* k x (- 1 x))) 0))) eLst)))

(setq k (+ k kinc)))


;; ==========================

;; ====== Period 2 =======

(getstring (strcat "\nK = 3, the system will now enter a Period 2 Orbit."
"\nPress Enter to Continue..."))

(setq kinc 0.005)

(while (<= k 3.5)

(setq x x0)
(repeat 100
(setq x (* k x (- 1 x))))
(repeat 40
(setq eLst
(cons
(vla-AddPoint spc
(vlax-3D-point (list k (setq x (* k x (- 1 x))) 0))) eLst)))

(setq k (+ k kinc)))

;; ==========================

;; ====== Period 4 =======

(getstring (strcat "\nK = 3.5, The Period will now double to a Period 4 Orbit."
"\nPress Enter to Continue..."))

(while (<= k 3.56)

(setq x x0)
(repeat 100
(setq x (* k x (- 1 x))))
(repeat 50
(setq eLst
(cons
(vla-AddPoint spc
(vlax-3D-point (list k (setq x (* k x (- 1 x))) 0))) eLst)))

(setq k (+ k kinc)))

;; ==========================

;; ====== Period 8 =======

(getstring (strcat "\nK = 3.56, The Period will now double once again..."
"\nPress Enter to Continue..."))

(while (<= k 3.567)

(setq x x0)
(repeat 100
(setq x (* k x (- 1 x))))
(repeat 50
(setq eLst
(cons
(vla-AddPoint spc
(vlax-3D-point (list k (setq x (* k x (- 1 x))) 0))) eLst)))

(setq k (+ k kinc)))

;; ==========================

;; ======== Chaos =========

(getstring (strcat "\nK -> 4, The System will go to Chaos..."
"\nPress Enter to Continue... (Please be patient)"))

(while (<= k 4.)

(setq x x0)
(repeat 100
(setq x (* k x (- 1 x))))
(repeat 250
(setq eLst
(cons
(vla-AddPoint spc
(vlax-3D-point (list k (setq x (* k x (- 1 x))) 0))) eLst)))

(setq k (+ k kinc)))

;; ==========================

(vla-EndUndoMark doc)
(princ))


;;-------------------------------------------------------------;;
;;-------------------------------------------------------------;;
;;-------------------------------------------------------------;;


More information about Chaos Theory and the Logistic Map can be found here:

 

http://en.wikipedia.org/wiki/Logistic_map

http://en.wikipedia.org/wiki/Bifurcation_theory

http://en.wikipedia.org/wiki/Chaos_theory

Link to comment
Share on other sites

I'll stand by what I said the other day; you have way too much time on your hands. :)

 

This is really cool Lee. I've read some on Chaos, but my math level isn't nearly high enough (yet....hopefully). I'm going to study this some more.

Thanks for sharing.

Link to comment
Share on other sites

I'll stand by what I said the other day; you have way too much time on your hands. :)

 

This is really cool Lee. I've read some on Chaos, but my math level isn't nearly high enough (yet....hopefully). I'm going to study this some more.

Thanks for sharing.

 

Well, this is relevant to my course - so I suppose it counts as studying o:)

 

I wasn't sure how this was going to be received on here - whether people would be the slightest bit interested or not, but I wanted to post it anyway, as I know a few of you guys also get the same kick out of mathematics that I do :P

Link to comment
Share on other sites

Well, this is relevant to my course - so I suppose it counts as studying o:)

 

I wasn't sure how this was going to be received on here - whether people would be the slightest bit interested or not, but I wanted to post it anyway, as I know a few of you guys also get the same kick out of mathematics that I do :P

I love math, so I appreciate the post and will have fun digging through this.

Link to comment
Share on other sites

ditto but I will struggle...

 

There is a lot of information out there, and, if you take each step slowly, and make sure you understand what is happening at each - I'm sure you'll be fine :)

Link to comment
Share on other sites

Nice Lee,

 

I'm not much of a math person (Odd seeing as I use it daily...) but I am intrigued by you posting. You should get into the Straight skeleton discussion that started at the swamp a few months back . It could benefit from your knowledge.

 

Very nice

Link to comment
Share on other sites

Nice Lee,

 

I'm not much of a math person (Odd seeing as I use it daily...) but I am intrigued by you posting. You should get into the Straight skeleton discussion that started at the swamp a few months back . It could benefit from your knowledge.

 

Very nice

 

Thanks Tim,

 

I have actually looked at that thread before, but I haven't really delved into it too much - it does look like an interesting problem to solve. :)

Link to comment
Share on other sites

Thanks Tim,

 

I have actually looked at that thread before, but I haven't really delved into it too much - it does look like an interesting problem to solve. :)

 

It was solved By LE (I think) but like a lot of the things that he posts, it was proprietary, so there was no source code, and he didn't get into a discussion about the formula.

 

it's a shame......

Link to comment
Share on other sites

It was solved By LE (I think) but like a lot of the things that he posts, it was proprietary, so there was no source code, and he didn't get into a discussion about the formula.

 

it's a shame......

 

Yeah, I've seen quite a few of his animations - with users begging him for the source code.... :wink:

Link to comment
Share on other sites

There is a lot of information out there, and, if you take each step slowly, and make sure you understand what is happening at each - I'm sure you'll be fine :)

 

Can't fight that the telomeres are shortening:cry:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...