;|
GraphPolynomial.lsp (GPN) graphs any polynomial equation in the form of
y = ax^n + bx^(n-1) + cx^(n-2) ... + dx + e
using the origin of the current UCS as 0,0
The equation may be of any degree, i.e., no limit to highest exponent.
The equation is both displayed as a text string and assigned to the
global symbol GPNequation for use in labeling.
Results may be plotted as points, line segments, a polyline, or a spline.
Usage:
Command: GPN ( or GRAPHPOLYNOMIAL )
Minimum X value: -10
Maximum X value: 10
X increment: 1
Highest exponent: 4
Coefficient of x^4: .001
Coefficient of x^3: 0
Coefficient of x^2: .1
Coefficient of x^1: 5
Coefficient of x^0: 6
Draw [pOints Lines Polyline Spline] : ( spline drawn)
y = .001x^4 + .1x^2 + 5x + 6 ) ( equation displayed )
by Bill Gilliss
bill realerthanreal com
Comments and suggestions always welcome.
Hats off to Reini Urban for (mapcar 'command ptlist) syntax.
No warranty, either expressed or implied, is made as to the fitness of
this information for any particular purpose. All materials are to be
considered 'as-is' and use thereof should be considered as at your own
risk.
v 1.00 2010-03-14 - original release
Keywords: AutoCAD AutoLISP graph function XY polynomial
======================================================================
|;
(defun c:graphpolynomial ( / degree xmin xmax xinc n ex y pt ptlist pwr
*osmode drawGraph coeff coefflist)
(vl-load-com)
(command "._undo" "_begin")
(initget 1) ;;no null
(setq xmin (getreal "Minimum X value: "))
(setq xmax (1- xmin))
(while (< xmax xmin)
(initget 1)
(setq xmax (getreal "\nMaximum X value: "))
(if (< xmax xmin)
(prompt "\nMaximum X must be greater than minimum X."))
)
(setq range (- xmax xmin))
(setq xinc (* range 2.0))
(while (>= xinc range)
(initget 7)
(setq xinc (getreal "X increment: "))
(if (> xinc range)
(princ (strcat "\nIncrement must be less than " (rtos range 2)))
)
)
(initget 7) ;;no null, no zero, no negative
(setq degree (getint "Highest exponent: "))
(setq coeffList (list))
(setq n degree)
(while (>= n 0)
(initget 1)
(setq coeff (getreal (strcat "Coefficient of x^" (itoa n) ": ")))
(setq coeffList (cons coeff coeffList))
(setq n (1- n))
)
;;note: coefficients reversed in list
(initget "pOints Lines Polyline Spline")
(setq drawGraph
(getkword "Draw [pOints Lines Polyline Spline] : "))
(if (not drawGraph) (setq drawGraph "Spline"))
(setq ptlist (list))
(setq x xmin)
(while (<= x xmax)
(setq y 0)
(setq pwr (1- (length coefflist)))
(while (>= pwr 0)
(setq y
(+ y
(* (nth pwr coeffList)
(if (> pwr 0)
(expt x pwr)
1 ;;can't do 0^0
)
)
)
)
(setq pwr (1- pwr))
)
(setq pt (list x y))
(setq ptlist (cons pt ptlist))
(setq x (+ x xinc))
)
(setq *osmode (getvar 'osmode))
(setvar 'osmode 0)
(cond
( (= drawGraph "pOints")
(foreach pt ptlist (command "._point" pt))
)
( (= drawGraph "Lines")
(command "._line")
(mapcar 'command ptlist)
(command "")
)
( (= drawGraph "Polyline")
(command "._pline")
(mapcar 'command ptlist)
(command "")
)
( (= drawGraph "Spline")
(command "._spline")
(mapcar 'command ptlist)
(command "" "" "")
)
( T )
);cond
(setvar 'osmode *osmode)
(setq GPNequation "y = ")
(setq n (length coeffList))
(repeat (length coefflist)
(if (/= 0 (nth (1- n) coeffList)) ;;do not include zero coefficients
(progn
(setq GPNequation
(strcat GPNequation
(vl-string-trim "0" (rtos (nth (1- n) coeffList) 2)))
)
(setq GPNequation (vl-string-right-trim "." GPNequation))
(if (> n 2)
(setq GPNequation (strcat GPNequation "x^" (itoa (1- n)) " + ")))
(if (= n 2)
(setq GPNequation (strcat GPNequation "x + ")))
)
)
(setq n (1- n))
);repeat
(command "._undo" "_end")
(princ (strcat "\n" GPNequation))
(princ)
);defun
(defun c:GPN ()
(c:graphpolynomial)
)
(princ "GraphPolynomial loaded. Type GRAPHPOLYNOMIAL or GPN to run.")
(princ)