0

It is my first question on stackoverflow, if I am missing some data, they tell me, What I need is to create a function graph with the python language but I do not find information on how to do it, I find how to make the graphs but not the limits of the functions

What I need to graph in python is similar to this function with the ranges enter image description here
This is the function

       { V₁x             0 ≤ x < a
       { V₁x - q(x-a)/2  a ≤ x ≤ a+l  # probably it's q(x-a)²/2
M(x) = { V₂(L-x)         a+l < x ≤ L
       { 0               otherwise



from matplotlib import pyplot as plt
x1 = [40, 50, 60, 70, 80, 90, 100]
y1 = [40, 50, 60, 70, 80, 90, 100]
plt.plot(x1, y1)
plt.xlabel('Like Geeks X Axis')
plt.ylabel('Like Geeks Y Axis')
axes = plt.axes()
axes.set_xlim([4, 8])
axes.set_ylim([-0.5, 2.5])
plt.show()
3
  • Does this answer your question? Plot Piecewise Function in Python Commented May 7, 2021 at 16:06
  • Andres, ¿ are you sure of the formula for the interval a<x<a+l ? In my informed understanding, it should be V1*x-q*(x-a)²/2 Commented May 7, 2021 at 19:51
  • @sophros In my understanding the question you referenced is about a (misunderstood) way to represent a function that has a single point discontinuity, so the answers given there are not applicable here. Commented May 12, 2021 at 16:48

2 Answers 2

1

I think you can just create a function that reproduces your mathematical formulation and then get the ys using that function.

If you need your code to be generic then do it like this:

from matplotlib import pyplot as plt


def create_function(V1, V2, a, l, q, L):
    def f(x):
        if x >= 0 and x < a:
            return V1 * x
        elif x >= a and x <= a + l:
            return V2 * x - q * (x - l)
        elif x > a + l and x <= L:
            return V2 * (L - x)
        return 0

    return f


f = create_function(1, 2, 1, 2, 0.5, 5)

x1 = list(range(15))
y1 = list(map(f, x1))

plt.plot(x1, y1)
plt.xlabel("Like Geeks X Axis")
plt.ylabel("Like Geeks Y Axis")
plt.show()

This way you have a generic function create_function that returns the function from your image with any parameter choice

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

Comments

1

If I understand what you are trying to do, plotting the diagram of the bending moment for a beam that is partially loaded with a constant distributed loading, here it is your graph

enter image description here

and here it is the code that produced it… but first a few preliminary remarks

  • if it's really the bending moment, there is a mistake in its definition, I took the liberty and corrected your definition
  • Matplotlib (and its brother Numpy) is not about manipulation of symbols (if you need that, look into Sympy) so all the quantities involved, q, l, a and L must be specified as numbers

that said, here it is the code

import matplotlib.pyplot as plt

def M(L, a, l, q, npoints=201):
    # npoints is an OPTIONAL argument with a default value
    from numpy import linspace, where
    # we MATERIALIZE our x axis as a sequence of points, an array in Python-speak
    x = linspace(0, L, npoints)
    # compute the reactions
    V2 = (l*q)*(a+l/2) / L
    V1 = l*q-V2
    # compute the bending moment,
    # our where is similar to Excel "IF()" function, if you know Excel 
    bm = where(x<a, V1*x, where(x<a+l, V1*x - q*(x-a)**2/2, V2*(L-x)))
    # the caller of this function wants to know the points in the arrays x and bm
    return x, bm

# L = 10 units of length, a = 4, l = 2;  q = 40 units of force per unit of length
x, bm = M(10, 4, 2, 40) # note: no optional npoints, 201 points will be used

fig, ax = plt.subplots()
ax.plot(x, bm)
# good mannered individuals plot the bending moment on the side of tension stresses
# so we invert the y axis
ax.invert_yaxis()
ax.grid()
plt.show()

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.