erst einmal vielen Dank für die aufnahme
Nun zu meinem Problem:
Ich soll eine simple Berechnung über einen Wertebereich von x plotten. Die Berechnung ist alles kein Problem, aber beim plotten komme ich nicht zu meinem gewünschten Ergebnis. Wenn ich in meinem range plotte, dann wird mir für jede Brechnung nur der eine Punkt im Diagramm gezeigt, ich möchte aber eine Kurve erhalten. Meine Recherche brachte mir keine adequate Lösung, drum hoffe ich, dass ihr mir helfen könnt.
Ich habe es auch schon geschafft, das mir über alle Werte nur ein Diagramm ausgegeben wird, aber da sind dann keine Werte zu finden.
Hier mein code:
Code: Alles auswählen
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
#define x and z(x)
def z(a):
if a < 0:
z = 0
return z
else:
z = np.sqrt(a)
return z
#value range for x
def frange(start, stop, step):
'''function to use the ability from range with floating numbers'''
i = start
while i < stop:
yield i
i += step
for x in range(-7, 8, 1):
#define w(x)
'''function of w(x) more specific than shown in task, because with the definition from the task,
w(x) will not calculate any value. If x = 0 or (x/7)^2>1 the sqrt will not work.'''
def w(x):
if x == 0:
w = 0
return w
elif np.square(x/7) > 1:
w = 0
return w
else:
w = 3*np.sqrt(1-(np.square(x/7)))
return w
#define m(x)
m = abs(x/2) + z(1-np.square(abs(abs(x)-2)-1)) - (1/112)*(3*np.sqrt(33)-7)*np.square(x)*w(x) -3
#define k(x)
k = 0.5*(3.0*(abs(x+0.5) + abs(x-0.5) + 6) -11 * (abs(x+0.75) + abs(x-0.75)))
#define r(x)
r = 1/2 * (3-x) + 6/7*np.sqrt(10) - 3/7 *np.sqrt(10) * z(4-np.square(x-1))
#define l(x)
l = 1/2 * (3+x) + 6/7*np.sqrt(10) - 3/7 *np.sqrt(10) * z(4-np.square(x+1))
#Heaviside function
'''the heaviside function is defined in numpy
Because x2 from the function isn't given in the task, I use x2 = 0'''
#final functions
f = w(x) +(l - w(x))*np.heaviside(x+3, 0) + (k - l)*np.heaviside(x+1, 0) + (r - k) * np.heaviside(x-1, 0) + (w(x) - r) * np.heaviside(x-3, 0)
g = 1/2 * (np.sign(x+4) - np.sign(x-4)) * m - w(x)
print("\n\n", x, "\n", f, "\n", g)
# plot
plt.figure(figsize = (12,5))
plt.plot(x, f, 'r', label ='f(x)')
plt.plot(x, g, 'k', label = 'g(x)')
plt.title('Plot for f(x) and g(x)')
plt. legend(loc = 'best')
plt.show()
input('Press return to exit')