user4206400
user4206400

Reputation:

How to plot a 3-Dimension graph with Racket

I am using Racket and Dr. Racket. There is a great library called plot which allows you to plot graphs in 2D or 3D.

After importing the package:

(require plot)

I can plot graphs like f(x,y) = 3 * x with:

(plot3d (surface3d (lambda (x y) (* x 3)) (- 10) 10 (- 10) 10))

enter image description here

However, when I try plotting this equation:

f (x,y,z) = 2(x-2)^2 + (y-1)^2+(z-3)^2 - 10

(plot3d (surface3d (lambda (x y z) (+ (* 2 (expt (- x 2) 2))
                                      (expt (- y 1) 2)
                                      (expt (- z 3) 2)
                                      -10))
                                      (- 10) 10 (- 10) 10))

I get a big message of error which I cannot understand:

surface3d: contract violation
  expected: a procedure that accepts 2 non-keyword argument
  given: #<procedure:...top/graficos.rkt:14:19>
  in: the 1st argument of
      (->*
       ((-> any/c any/c Real))
       ((or/c Real #f)
        (or/c Real #f)
        (or/c Real #f)
        (or/c Real #f)
        #:alpha
        Nonnegative-Real
        #:color
        (or/c
         Integer
         Symbol
         String
         (recursive-contract g147 #:impersonator)
         (cons/c
          Real
          (cons/c Real (cons/c Real '()))))
        #:label
        (or/c #f String)
        #:line-color
        (or/c
         Integer
         Symbol
         String
         (recursive-contract g147 #:impersonator)
         (cons/c
          Real
          (cons/c Real (cons/c Real '()))))
        #:line-style
        (or/c
         Integer
         'transparent
         'solid
         'dot
         'long-dash
         'short-dash
         'dot-dash)
        #:line-width
        Nonnegative-Real
        #:samples
        Positive-Integer
        #:style
        (or/c
         Integer
         'transparent
         'solid
         'bdiagonal-hatch
         'crossdiag-hatch
         'fdiagonal-hatch
         'cross-hatch
         'horizontal-hatch
         'vertical-hatch)
        #:z-max
        (or/c Real #f)
        #:z-min
        (or/c Real #f))
       any)
  contract from: 
      <pkgs>/plot-lib/plot/private/plot3d/surface.rkt
  blaming: /home/pedro/Desktop/graficos.rkt
   (assuming the contract is correct)
  at: <pkgs>/plot-lib/plot/private/plot3d/surface.rkt:49.9

It is absolutelly possible to graph this. I did it on Wolfram Alpha:

enter image description here

Upvotes: 1

Views: 250

Answers (1)

user5920214
user5920214

Reputation:

Regardless of the details of the Racket implementation, what you are trying to do makes no sense. You are trying to plot a function which maps from R^3 to R, and you can't do that as a surface plot in 3 dimensions: you need 4.

So, in as far as there is an answer to this problem, it is to rethink what you are trying to do. One approach would be to curry your function so you can plot it for fixed values of one of its arguments: (plot3d (surface3d (curryr (lambda (x y z) ...) 3.0) ...)) will plot it for z=3.0 for instance.

Upvotes: 2

Related Questions