2

Note: This is for homework so please don't post full code responses, just help on what I'm misusing would be appreciated

I'm trying to plot a piecewise defined function where when 0 < x <= 10 it will be a constant (KQ/10) and for x > 10 it will be KQ/x for 10 < x < 50. Currently my result comes back as a single value instead of my expected result of an array with a constant value up until x > 10 and then varying values until x = 50

My current code

import matplotlib.pyplot as plt
import scipy


x = np.linspace(0, 50, 1)
R = 10
r = np.linspace(10, 50, 1)
k = 1/(4*np.pi*constants.epsilon_0)
Q = 1
def inside(x):
    return k*Q/R
def outer(x):
    return k*Q/x
result = np.piecewise(
    x,
    [x <= R, x > R ],
    [lambda x: inside(x), lambda x: outer(x)]
)
result
#plt.plot(x,result)
#plt.axis([0, 50,0, 500000])
#plt.xlabel('x')
#plt.ylabel('y')```

1 Answer 1

2

x = np.linspace(0, 50, 1)

isnt how linspace works... this only creates one data point ...

x = np.linspace(0, 50, 10000) ... would create 10k datapoints

perhaps you wanted np.arange(0,50,1) ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.