3
\$\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 mathmatical 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 seperate each point with a space.

\$\endgroup\$
6
  • \$\begingroup\$ Is a function that takes x and y acceptable as input? \$\endgroup\$
    – emanresu A
    May 4 at 1:09
  • \$\begingroup\$ Are you talking about \$\frac{dy}{dx} = x \wedge y\$? \$\endgroup\$
    – VJZ
    May 4 at 1:13
  • \$\begingroup\$ No, I mean a function f(x, y) which is passed to our program. \$\endgroup\$
    – emanresu A
    May 4 at 1:15
  • \$\begingroup\$ Yes, but keep in mind, the "function" is actually the derivative. \$\endgroup\$
    – VJZ
    May 4 at 1:17
  • \$\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 at 1:53

3 Answers 3

2
\$\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\$
    – graffe
    May 5 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 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 at 9:03
-1
\$\begingroup\$

PYTHON3 (166 bytes)(not counting the function and r)

r=10
def f(x,y):
    return (x-y)/(x-2*y)
def d(x,y):
    try:s=f(-x,y);return(s>0)*'/'+(s<0)*'\\'+'-'*(s==0)
    except:return'|'
s=''
for y in range(-r,r):
    for x in range(-r,r):
        s+=d(x,y)+' '
    s+='\n'
print(s)

Try it online!

\$\endgroup\$
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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