Seite 1 von 1

Eine Funktion plotten

Verfasst: Montag 23. Februar 2015, 14:12
von Basilius Sapientia
Servus!

Ich habe diesen http://9gag.com/gag/a0LdAVB Link gefunden. Dort steht eine Formel:
fx=x**2+(y-(x**2)**(1/3))**2=1

Ich wollte dies dann plotten. Doch ich habe Schwierigkeiten damit. Kann mir bitte jemand erklären, wie ich dies plotte? Natürlich hier mein Versuch:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0, 100, 1)
y=np.linspace(0, 100, 1)

plot(show)
xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True)
savefig("test.png")
show()
Vielen Dank im Voraus!

Re: Eine Funktion plotten

Verfasst: Montag 23. Februar 2015, 14:46
von Sirius3
@Basilius Sapientia: Wo berechnest Du die Formel "fx=x**2+(y-(x**2)**(1/3))**2=1" und was meinst Du macht plot(show)?
Matplotlib hat eine große Sammlung an Beispielplots. Da findest Du sicher den für dich passenden.

Re: Eine Funktion plotten

Verfasst: Montag 23. Februar 2015, 17:26
von MagBen
So geht's:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

# x-Werte
anzahl = 100
x=np.linspace(0, 1, anzahl) # linspace(von, bis, Anzahl)

# y-Werte
# Gleichung x**2+(y-(x**2)**(1/3))**2=1
# nach y aufgeloest:
y = np.sqrt(1-x**2) + x**(2./3.)

# Test:
np.testing.assert_array_almost_equal(x**2+(y-(x**2)**(1./3.))**2, np.ones((anzahl,)))

# Plotten
plt.figure() 
plt.plot(x,y)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()

Re: Eine Funktion plotten

Verfasst: Montag 23. Februar 2015, 18:00
von Üpsilon
@Magben das ist jez aber auch nur die halbe Wahrheit. Bei der Wurzel musste schon auch +/- machen.

Re: Eine Funktion plotten

Verfasst: Montag 23. Februar 2015, 18:01
von MagBen
Und so war's gemeint:
Bild

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

# x-Werte
anzahl = 1000
# linspace(von, bis, Anzahl)
x=np.linspace(-1, 1, anzahl)

# y-Werte
# Gleichung
# x**2+(y-(x**2)**(1/3))**2=1
# nach y aufgeloest:
y1 =  np.sqrt(1-x**2) + (x**2.)**(1./3.)
y2 = -np.sqrt(1-x**2) + (x**2.)**(1./3.)

# Test:
np.testing.assert_array_almost_equal(x**2+(y1-(x**2)**(1./3.))**2, np.ones((anzahl,)))
np.testing.assert_array_almost_equal(x**2+(y2-(x**2)**(1./3.))**2, np.ones((anzahl,)))

# Plotten
plt.figure() 
plt.plot(x,y1, "r", linewidth=3)
plt.plot(x,y2, "r", linewidth=3)
plt.title('About as simple as it gets, folks')
plt.xlim(-1.05,1.05)
plt.ylim(-1.05,1.55)
plt.savefig("herz.png")
plt.show()