Jump to content

Recommended Posts

Posted

Do anyone mind to send me a lisp of

y = x^3 + x^2 + x + 1

graphic?

  • Replies 44
  • Created
  • Last Reply

Top Posters In This Topic

  • automaticad

    17

  • marko_ribar

    6

  • irneb

    6

  • LibertyOne

    4

Top Posters In This Topic

Posted Images

Posted

What exactly did you want the lisp to do, that can't be done in Excel in half a minute?

 

The range for x values is only from about -3 to +3 and then the line gets asymptotic to the vertical axes.

graph.JPG

Posted
What exactly did you want the lisp to do, that can't be done in Excel in half a minute?

 

Hey man, I am asking a lisp of y=x^3 + x^2 + x + 1 function for autocad, not ms excel or any else.

Posted

a couple of ways long hand can do to the power easier

 

(setq x (getreal "Enter x))
(setq y (+  (+ 1.0 (+ (* (* x x ) x) (* x x)) ) x)
(princ y)

Posted
a couple of ways long hand can do to the power easier

 

(setq x (getreal "Enter x))
(setq y (+  (+ 1.0 (+ (* (* x x ) x) (* x x)) ) x)
(princ y)

 

The above lisp isn't complete isn't it?

 

Do you mind to send me the complete one.

 

Also, I need additional option such as specify first point, specify end point.

Posted (edited)

Try the attached LISP to visualize the sin-function.

Notice that you have to define your function by yourself within the source code.

BTW, I use a DWT-Template consisting of an coordinate system before running the LISP.

Good luck

Jochen

fkt_sin_eng.lsp

Edited by scj
typo's
Posted
(setq x (getreal "Enter x))
(setq y (+  (+ 1.0 (+ (* (* x x ) x) (* x x)) ) x)
(princ y)

 

BIGAL, note that the * and + operators are not limited to a certain number of numerical arguments; hence the function could be written:

 

Mathematical notation:

f(x) = x^3 + x^2 + x + 1

AutoLISP:

(defun f (x) (+ (* x x x) (* x x) x 1))

Posted

Here, this one was Lee's one from a long time ago... Familiar, Lee?

 

(defun c:DynFun ( / _GenerateFunctionPoints fun p1 gr )
 ;; © Lee Mac 2010

 ;; Function to evaluate
 (setq fun (lambda ( x ) (+ (expt x 3) (expt x 2) x 1.0)))
 ;; exp, atan, cos, sin, log - undefined for 0.0, sqrt
 ;; Lee's suggestion : (setq fun (lambda ( x ) (* (sin (* 2.0 x)) (exp (/ x 10.0)))))
 (defun _GenerateFunctionPoints ( p1 p2 / d a1 a2 i j )
   (setq d (distance p1 p2) a1 (angle p1 p2) a2 (+ a1 (/ pi 2.)) i 0 p3 (polar p1 a2 (fun 0.)))

   (while (<= (setq x (* 0.1 (setq i (1+ i)))) d)
     (grdraw p3 (setq p3 (polar (polar p1 a1 x) a2 (fun x))) 3)
   )
 )

 (if (setq p1 (getpoint "\nSpecify First Point: "))
   (while (= 5 (car (setq gr (grread 't 13 0)))) (redraw)
     (_GenerateFunctionPoints p1 (cadr gr))
   )
 )
 
 (princ)
)

Posted

That is very old indeed...

 

Here is an updated version:

[color=GREEN];; Dynamic Function Plotter  -  Lee Mac[/color]
([color=BLUE]defun[/color] c:dynfun ( [color=BLUE]/[/color] f i o p q x )
   ([color=BLUE]setq[/color] f [color=BLUE]sin[/color] [color=GREEN];; Function to evaluate[/color]
         i 0.1 [color=GREEN];; Increment (i > 0)[/color]
   )
   ([color=BLUE]or[/color] ([color=BLUE]setq[/color] o ([color=BLUE]getpoint[/color] [color=MAROON]"\nSpecify Origin for Function <0,0>: "[/color]))
       ([color=BLUE]setq[/color] o '(0.0 0.0 0.0))
   )
   ([color=BLUE]princ[/color] [color=MAROON]"\nMove cursor to alter x-range..."[/color])
   ([color=BLUE]while[/color] ([color=BLUE]=[/color] 5 ([color=BLUE]car[/color] ([color=BLUE]setq[/color] p ([color=BLUE]grread[/color] [color=BLUE]t[/color] 13 0))))
       ([color=BLUE]redraw[/color])
       ([color=BLUE]setq[/color] x ([color=BLUE]abs[/color] ([color=BLUE]-[/color] ([color=BLUE]caadr[/color] p) ([color=BLUE]car[/color] o)))
             x ([color=BLUE]-[/color] x ([color=BLUE]rem[/color] x i))
             q ([color=BLUE]mapcar[/color] '[color=BLUE]+[/color] o ([color=BLUE]list[/color] x (f x)))
       )
       ([color=BLUE]repeat[/color] ([color=BLUE]fix[/color] ([color=BLUE]/[/color] x i 0.5))
           ([color=BLUE]grdraw[/color] q ([color=BLUE]setq[/color] q ([color=BLUE]mapcar[/color] '[color=BLUE]+[/color] o ([color=BLUE]list[/color] ([color=BLUE]setq[/color] x ([color=BLUE]-[/color] x i)) (f x)))) 3)
       )
   )
   ([color=BLUE]princ[/color])
)

For the function in question, set variable f to:

(lambda ( x ) (+ (* x x x) (* x x) x 1))

Posted

Here is mine prehistoric version - I translated it to English... It draws 3dpoly based on y(x) combined with z(x)... You should enter for y(x) when asked : x^3+x^2+x+1 and for z(x) : 0... Note that you must type small letter x - not X...

 

(defun c:func ( / listaptt cmde cecol oscmd bp yx yxl yxlorig yxlen xsearch yxlnew yxnlnew yxmlnew yxn yxm zx zxl zxlorig zxlen zxlnew zxnlnew zxmlnew zxn zxm seg sx sy sz a ax b bx ab dx sseg n m xn yn zn ptn ptnw xm ym zm ptm ptmw izb )
 (setq cmde (getvar "cmdecho"))
 (setvar "cmdecho" 0)
 (arxload "geomcal.arx")
 (command "_.ucs" "")
 (terpri)
 (prompt "\nFunctions are: sin(angle)=sin(x),cos(angle)=cos(x),tang(angle)=...~(x),asin(real)=...~,acos(real)~,atan(real)~,ln(real)~,log(real),exp(real),exp10(real),sqr(real),sqrt(real),abs(real),round(real),trunc(real),r2d(angle),d2r(angle),pi")
 (terpri)
 (terpri)
 (prompt "\nFunction expression MUST BE ENTERED with little letter x - not with X")
 (terpri)
 (terpri)
 (textscr)
 (setq oscmd (getvar "osmode"))
 (setq cecol (getvar "cecolor"))
 (setvar "osmode" 0)
 (setq bp (list 0.0 0.0 0.0))
 (setq yx (getstring "\nFunction expression y(x) : "))
 (setq yxl (vl-string->list yx))
 (setq yxlorig yxl)
 (setq yxlen (length yxl))
 (repeat yxlen
   (setq xsearch (car yxl))
   (if (= xsearch 120) 
     (progn 
       (setq yxlnew (cons 120 yxlnew))
       (setq yxlnew (cons 110 yxlnew))
     )
     (setq yxlnew (cons xsearch yxlnew))
   )
   (setq yxl (cdr yxl))
 )
 (setq yxnlnew (reverse yxlnew))
 (setq yxlnew nil)
 (setq yxl yxlorig)
 (repeat yxlen
   (setq xsearch (car yxl))
   (if (= xsearch 120) 
     (progn 
       (setq yxlnew (cons 120 yxlnew))
       (setq yxlnew (cons 109 yxlnew))
     )
     (setq yxlnew (cons xsearch yxlnew))
   )
   (setq yxl (cdr yxl))
 )
 (setq yxmlnew (reverse yxlnew))
 (setq yxlnew nil)
 (setq yxn (vl-list->string yxnlnew))
 (setq yxm (vl-list->string yxmlnew))
 (terpri)
 (prompt "\nFor observing changing of points (x,y,0) in base plane XY, for function expression z(x) you should enter 0")
 (terpri)
 (setq zx (getstring "\nFunction expression z(x) : "))
 (setq zxl (vl-string->list zx))
 (setq zxlorig zxl)
 (setq zxlen (length zxl))
 (repeat zxlen
   (setq xsearch (car zxl))
   (if (= xsearch 120) 
     (progn 
       (setq zxlnew (cons 120 zxlnew))
       (setq zxlnew (cons 110 zxlnew))
     )
     (setq zxlnew (cons xsearch zxlnew))
   )
   (setq zxl (cdr zxl))
 )
 (setq zxnlnew (reverse zxlnew))
 (setq zxlnew nil)
 (setq zxl zxlorig)
 (repeat zxlen
   (setq xsearch (car zxl))
   (if (= xsearch 120) 
     (progn 
       (setq zxlnew (cons 120 zxlnew))
       (setq zxlnew (cons 109 zxlnew))
     )
     (setq zxlnew (cons xsearch zxlnew))
   )
   (setq zxl (cdr zxl))
 )
 (setq zxmlnew (reverse zxlnew))
 (setq zxlnew nil)
 (setq zxn (vl-list->string zxnlnew))
 (setq zxm (vl-list->string zxmlnew))
 (setq seg (getreal "\nEnter number of segments of graph of function: "))
 (command "_.view" "SWISO")
 (command "zoom" "c" bp "25.0")
 (command "xline" "h" bp "")
 (command "-color" "white")
 (setq sx (ssadd))
 (ssadd (entlast) sx)
 (command "-color" "yellow")
 (command "xline" "v" bp "")
 (setq sy (ssadd))
 (ssadd (entlast) sy)
 (command "-color" "red")
 (command "ucs" "x" -90)
 (command "xline" "v" bp "")
 (command "ucs" "p")
 (setq sz (ssadd))
 (ssadd (entlast) sz)
 (sssetfirst nil sx)
 (setvar "osmode" 512)
 (setq a (getpoint "\nPick point of starting value of observing segment of function on x axis: "))
 (setq ax (car a))
 (setq b (getpoint "\nPick point of ending value of observing segment of function on x axis: "))
 (setq bx (car b))
 (setq ab (distance a b))
 (setq dx (/ ab seg))
 (setq sseg (fix seg))
 (setq n -1)
 (repeat sseg
   (setq n (+ n 1))
   (setq m (+ n 1))
   (setq xn (+ ax (* dx n)))
   (setq yn (cal yxn))
   (setq zn (cal zxn))
   (setq ptn (list xn yn zn))
   (setq ptnw (trans ptn 1 0))
   (setq xm (+ ax (* dx m)))
   (setq ym (cal yxm))
   (setq zm (cal zxm))
   (setq ptm (list xm ym zm))
   (setq ptmw (trans ptm 1 0))
   (setq listaptt (cons ptnw listaptt))
 )
 (setq listaptt (cons ptmw listaptt))
 (setvar "osmode" oscmd)
 (command "._3dpoly")
 (while (> (getvar 'cmdactive) 0) 
   (foreach pt listaptt 
     (command pt)
   ) 
   (command "")
 )
 (command "change" "l" "" "p" "c" "green" "")
 (setvar "cmdecho" cmde)
 (setvar "cecolor" cecol)
 (command "_.ucs" "p")
 (princ)
)

 

M.R.

Posted

To draw the graph of f(x)=x^3+x^2+x+1, between 2 x values

(defun C:ec3 (/ f a b c s)
 (defun f (x) (* (1+ x) (1+ (* x x))))
 (if
   (and
     (setq a (getreal "\nEnter interval\nStart point: "))
     (setq b (getreal "\nEnd point: "))
     )
   (progn
     (if (< b a) (setq c a a b b c))
     (setq s (getvar 'splmethod))
     (setvar 'splmethod 0)
     (command "SPLINE")
     (repeat (1+ (fix (* 10 (- b a))))
       (command "_non" (list a (f a) 0.0))
       (setq a (+ 0.1 a))
       )
     (command "" "" "")
     (setvar 'splmethod s)
     )
   )
 (princ)
 )

Posted

Buddies, thank you.

 

But I do not need a graph plotter such as matlab or a calculator or even a ms excel.

 

What I need is a lisp file which can create a y = x^3 + x^2 + x + 1 graphic that can be loaded (appload) into my acad worksheet for the 2d drafting and 3d modeling purpose, not for a calculator or a graph plotter such as matlab, etc.

 

I need to know how to specify first point, and to specify end point too.

Posted

But I need a lisp for the 2d drafting and 3d modeling purpose, not for a calculator or a graph plotter such as matlab, etc.

 

You should use my version here - you can change (command "_.3dpoly") line to (command "_.spline"), or above posted code from Stefan...

Posted

None of all codes posted in this thread are working.

 

Because I don't know how to use it like when you want to create a line, you type a line in the command box.

 

I can only load it by the appload.

 

So, what should I enter in the command box to use these lisps?

Posted

So, what should I enter in the command box to use these lisps?

 

After you load them with APPLOAD (or just copy+paste *.lsp file into CAD interface), you should use syntax for each lisp you loaded that is defined in first line of *.lsp)

 

For example :

- if function (lisp) is defined as command function - it starts with :

(defun c:functionname ( / var1 var2 ... varn ) ...rest of code... (princ))

c: after (defun) function means that this is command function and you can call it with just typing syntax after c: [Command: functionname] and hit enter after you typed [functionname]... Note that command functions may be called as lisp function - from another lisp - just use this syntax (c:functionname)...

 

- if function is defined as non-command function - note that then it may also have arguments that have to be supplied to execution of this function in order to pass them and calculate operations with them defined later in body of this kind of function... None of our posted lisps aren't defined in this way :

(defun functionname ( arg1 arg2 ... argn / var1 var2 ... varn ) ...rest of code... )

No preceding c: after (defun) function means that this function is non-command and it may be called only through lisp as lisp function with : (functionname arg1 arg2 ... argn)... Note that for all defined arguments within function, for calling it properly, you have to supply all arguments to avoid error (to few arguments)... Arguments may be any type of data (number - real, integer; list; string; ename; vla-object; symbol; another defined variable; T symbol; nil symbol), just be sure you supplied them all as required by function definition...

Posted

Nope, still not working.

 

What is the best book containing the complete lisp lessons?

 

Already read some lisp books, most isn't good enough.

Posted

Or maybe anyone mind to send me a lisp of

y = 2r (a straight horizontal line, where the r is a variable and its value is a constant)

between the x1 and the x2.

 

The options are:

specify first point (which is the value of the x1),

specify end point (which is the value of the x2).

 

As an example:

 

y = 2*3 (r = 3)

 

or

 

y = 6

 

x1 = 1 and x2 = 17.

 

It's as a straight horizontal line with length of 16 units.

 

Need it as soon as possible.

Posted
None of all codes posted in this thread are working.

 

Nope, still not working.

.....

Already read some lisp books, most isn't good enough.

 

Wow, where's all this aggravation coming from? :glare:

 

An yet...

 

Need it as soon as possible.

 

Listen, As far a i can tell Lee/BigalMarko/Stefan pointed you at the right direction.

 

Perhaps an illustration on what you need will make us here understand what you mean by:

 

What I need is a lisp file which can create a y = x^3 + x^2 + x + 1 graphic that can be loaded (appload) into my acad worksheet for the 2d drafting and 3d modeling purpose

 

I need to know how to specify first point, and to specify end point too

Posted

The errors:

 

Stefan's codes:

start point: 1

end point: 2

nothing happened, no graphic is displayed.

Also there is an error message:

: error: AutoCAD variable setting rejected: SPLMETHOD 0

http://s1.postimg.org/5hwz48v8f/stefan_s_code.png

 

Marko's codes error:

The only option is specify first point.

Both entering a value (e.g. 1 or 2, etc.) or coordinate (e.g. 0,1), will create a graphic.

The graphic is likely the y = x^3 + x^2 + x + 1 graphic.

But the graphic is dissappear when zoom in or zoom out.

n5id2fcuxhttp://s17.postimg.org/j6bljb9qn/marco_s_codes.png

 

Lee's codes error:

specify origin for function :

I assume is .

So I type .

move cursor to alter x-range...

The graphic (green) is dissappear when zoom in or zoom out.

Also the graphic is likely not the y = x^3 + x^2 + x + 1 but y = sin a or y = cos a (please cmiiw).

http://s21.postimg.org/pvba8cwuv/lee_s_codes.png

 

2nd Marco's codes error:

Command: func (enter)

Function expression y(x):

What is the expression?

It says function expression must be entered with little letter x - not with X.

I enter x.

What about x^2?

What should I type?

x*x, x.x, or x^2?

Function expression z(x):

Why z(x)?

Why two functions, y(x) and z(x)?

The function I need is only one, it is y = f(x) = x^3 + x^2 + x + 1.

It automatically changing view from 2D to the 3D view such as isometric view.

http://s13.postimg.org/6aekr41if/2nd_marco_s_codes.png

Posted
The errors:

 

Lee's codes error:

specify origin for function :

I assume is .

So I type .

move cursor to alter x-range...

The graphic (green) is dissappear when zoom in or zoom out.

Also the graphic is likely not the y = x^3 + x^2 + x + 1 but y = sin a or y = cos a (please cmiiw).

http://s21.postimg.org/pvba8cwuv/lee_s_codes.png

 

 

Well, perhaps if you read Lee's explaination correctly. He explicitly stated to change out the variable for f.

You obviously didn't do that. Try that first then come back and give us your results.

 

 

[color=GREEN];; Dynamic Function Plotter  -  Lee Mac[/color]
([color=BLUE]defun[/color] c:dynfun ( [color=BLUE]/[/color] f i o p q x )
   ([color=BLUE]setq[/color] f [color=BLUE]sin[/color] [color=GREEN];; Function to evaluate[/color]
         i 0.1 [color=GREEN];; Increment (i > 0)[/color]
   )
   ([color=BLUE]or[/color] ([color=BLUE]setq[/color] o ([color=BLUE]getpoint[/color] [color=MAROON]"\nSpecify Origin for Function <0,0>: "[/color]))
       ([color=BLUE]setq[/color] o '(0.0 0.0 0.0))
   )
   ([color=BLUE]princ[/color] [color=MAROON]"\nMove cursor to alter x-range..."[/color])
   ([color=BLUE]while[/color] ([color=BLUE]=[/color] 5 ([color=BLUE]car[/color] ([color=BLUE]setq[/color] p ([color=BLUE]grread[/color] [color=BLUE]t[/color] 13 0))))
       ([color=BLUE]redraw[/color])
       ([color=BLUE]setq[/color] x ([color=BLUE]abs[/color] ([color=BLUE]-[/color] ([color=BLUE]caadr[/color] p) ([color=BLUE]car[/color] o)))
             x ([color=BLUE]-[/color] x ([color=BLUE]rem[/color] x i))
             q ([color=BLUE]mapcar[/color] '[color=BLUE]+[/color] o ([color=BLUE]list[/color] x (f x)))
       )
       ([color=BLUE]repeat[/color] ([color=BLUE]fix[/color] ([color=BLUE]/[/color] x i 0.5))
           ([color=BLUE]grdraw[/color] q ([color=BLUE]setq[/color] q ([color=BLUE]mapcar[/color] '[color=BLUE]+[/color] o ([color=BLUE]list[/color] ([color=BLUE]setq[/color] x ([color=BLUE]-[/color] x i)) (f x)))) 3)
       )
   )
   ([color=BLUE]princ[/color])
)

For the function in question, set variable f to:

(lambda ( x ) (+ (* x x x) (* x x) x 1))

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...