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

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

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!

• please check the allowed inputs/outputs, in particular storing input in predefined variables is not allowed May 9 at 8:47
• also, your ranges are not correct (they should go to r inclusive) May 9 at 8:53