6
\$\begingroup\$

Slope fields or direction fields, are a graphical representation of the solutions to a first-order differential equation of a scalar function. A slope field shows the slope of a differential equation at certain vertical and horizontal intervals on the cartesian plane, and can be used to determine the approximate tangent slope at a point on a curve, where the curve is some solution to the differential equation. For example consider the differential equation:

$$ \frac{dy}{dx} = \frac{y-4x}{x-y} $$

The corresponding slope field will look like:

enter image description here

Another example with this differential equation:

$$ \frac{dy}{dx} = \frac{x-y}{x-2y} $$

enter image description here

Your task is to generate such slope fields in the console with characters of text. You are to print the following for each coordinate pair:

  • / If the differential equation evaluated at the point \$(x, y)\$ is positive.
  • \ If the differential equation evaluated at the point \$(x, y)\$ is negative.
  • - If the differential equation evaluated at the point \$(x, y) = 0\$.
  • | If the differential equation evaluated at the point \$(x, y)\$ is undefined.

The inputs are a string and a number consisting of:

  • Expression string that can be evaluated (or parsed if you have to).
  • A number that outlines the radius of the plot from \$(0, 0)\$.

The expression string can use any built-in mathematical function and will only either \$x\$ or \$y\$ as variables. Any combination of logarithms, (inverse and/or hyperbolic) trigonometric functions, or functions like sqrt(), pow(), exp(), abs() may be used in the expression. The plot radius may also be 0, which means only print one character containing the slope at the origin. A sample input may look something like:

plotRadius = 10
expression = "(x - y) / (x - 2 * y)"

Notice how both the boundaries of the plot follow: \$ -\text{plotRadius} \leq x, y \leq \text{plotRadius}\$. This results in the following slope field being displayed:

/ / / / / / / / / / / / / / / / / / / / — 
/ / / / / / / / / / / / / / / / / / / — \ 
/ / / / / / / / / / / / / / / / / / — \ \ 
/ / / / / / / / / / / / / / / / / — \ \ \ 
/ / / / / / / / / / / / / / / / — \ \ \ \ 
/ / / / / / / / / / / / / / / — \ \ \ \ | 
/ / / / / / / / / / / / / / — \ \ \ | / / 
/ / / / / / / / / / / / / — \ \ | / / / / 
/ / / / / / / / / / / / — \ | / / / / / / 
/ / / / / / / / / / / — | / / / / / / / / 
/ / / / / / / / / / | / / / / / / / / / / 
/ / / / / / / / | — / / / / / / / / / / / 
/ / / / / / | \ — / / / / / / / / / / / / 
/ / / / | \ \ — / / / / / / / / / / / / / 
/ / | \ \ \ — / / / / / / / / / / / / / / 
| \ \ \ \ — / / / / / / / / / / / / / / / 
\ \ \ \ — / / / / / / / / / / / / / / / / 
\ \ \ — / / / / / / / / / / / / / / / / / 
\ \ — / / / / / / / / / / / / / / / / / / 
\ — / / / / / / / / / / / / / / / / / / / 
— / / / / / / / / / / / / / / / / / / / /

Please separate each point with a space.

\$\endgroup\$
6
  • \$\begingroup\$ Is a function that takes x and y acceptable as input? \$\endgroup\$
    – emanresu A
    May 4, 2022 at 1:09
  • \$\begingroup\$ Are you talking about \$\frac{dy}{dx} = x \wedge y\$? \$\endgroup\$
    – VJZ
    May 4, 2022 at 1:13
  • \$\begingroup\$ No, I mean a function f(x, y) which is passed to our program. \$\endgroup\$
    – emanresu A
    May 4, 2022 at 1:15
  • \$\begingroup\$ Yes, but keep in mind, the "function" is actually the derivative. \$\endgroup\$
    – VJZ
    May 4, 2022 at 1:17
  • 2
    \$\begingroup\$ Does this have to be approximated ASCII art, or would submissions in e.g. Desmos be allowed? Would drawing an actual graph be a separate question? \$\endgroup\$
    – Bbrk24
    May 6, 2022 at 1:53

3 Answers 3

1
\$\begingroup\$

Julia 1.0, 90 bytes

N\~=-(r=-N:N).|>y->println(join(r.|>x->abs(x~y)<Inf ? "\\-/"[Int(sign(x~y))+2] : "|"," "))

Try it online!

takes plotRadius\df, where df is a black-box function and prints the plot

\$\endgroup\$
2
  • \$\begingroup\$ We need to update Julia on TIO! \$\endgroup\$
    – user108721
    May 5, 2022 at 13:39
  • 1
    \$\begingroup\$ @graffe TIO is not supported anymore, there's no new languages. There is ato.pxeger.com/run with the latest version of Julia \$\endgroup\$
    – MarcMush
    May 5, 2022 at 13:45
1
\$\begingroup\$

Burlesque, 71 62 bytes

pes1Jngjr@sajJ<>cpm{%1!{{@0.<}'\{@0.>}'/{@0==}'-{1}'|}cn}jcosp

Try it online!

First stab, still some optimisation to be done

pes1       # Parse function and save 
J          # Dup
ng         # Negate
jr@        # Range -n..n
sa         # Get length for chunking later
jJ<>cp     # Transform to -n..n x -n..n
m{         # Map
 %1!       # Call function
 {
  {@0.<}'\ # If <0  \
  {@0.>}'/ # If >0  /
  {@0==}'- # If ==0 -
  {1}'|    # else   |
 }cn       # Condition
}
jco        # Chunks of len
sp         # Pretty output
\$\endgroup\$
1
  • \$\begingroup\$ I think you should reverse the y axis (check with x+y) \$\endgroup\$
    – MarcMush
    May 9, 2022 at 9:03
0
\$\begingroup\$

Python 2, 122 bytes

def g(r,e):
 q=range(-r,r+1)
 for x in q:
    l=""
    for y in q:
     try:l+="-/\\"[cmp(e(-x,y),0)]+" "
     except:l+="| "
    print l

Try it online!

Takes an integer and a function as input, prints the result.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.