'plot in python Piecewise function substitution

I want to plot a graph in python by substituting piecewise function.

import numpy as np
import matplotlib.pyplot as plt 
%matplotlib inline
#set up constants
d0=0.2
d1=0.2
L0=0.2
L=5
delta1=0.02
Dp=1
def fun(x):
    out = np.zeros_like(x)
    out[(x >= 0) & (x <= d1)] = d0
    mask = (x >= d1) & (x <= d1 + L0)
    out[mask] = d0-0.5*delta1*(1+np.cos(2*np.pi/L0)*(x[mask]-d1-0.5*L0))
    out[(x >= d1 + L0) & (x <= L)] = L0
    return out

# Define 100 points between 0 to 5 evenly       spaced
y = np.linspace(0, 5, 100) # Change
g = Dp*(y*y-(fun(y))**2) # Change
plt.plot(y, g) # Change
plt.show() # Change

enter image description here

dear fun(y)is not a function of y its a function of x. i am attaching the sample graph



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source