Andres Torres
Andres Torres

Reputation: 13

graph functions with range in python

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()

Upvotes: 0

Views: 1661

Answers (2)

gboffi
gboffi

Reputation: 25053

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()

Upvotes: 1

Matteo Zanoni
Matteo Zanoni

Reputation: 4152

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

Upvotes: 1

Related Questions