# Plot Slope Fields Given Differential Equation

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:

Another example with this differential equation:

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

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.

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

# 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

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

# 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

• I think you should reverse the y axis (check with x+y) Commented May 9, 2022 at 9:03

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