Probleme bei der Berechnung

mit matplotlib, NumPy, pandas, SciPy, SymPy und weiteren mathematischen Programmbibliotheken.
Antworten
Vossi
User
Beiträge: 1
Registriert: Freitag 4. Dezember 2015, 09:47

Hallo meine lieben,

ich bin neu im Bereich der Python Programmierung und habe den folgenden Code geschrieben.
Den "gleichen" Code habe ich in Matlab bereits erfolgreich geschrieben.

Ich habe folgendes Problem. Wenn ich die Inkrementierung von phi_12+=0.01 auf phi_12+=0.1 ändere, dann klappt es. Jedoch ist dann der Plot falsch, da das Programm eine Inkrementierung von mind 0.01 benötigt. Wenn ich den Wert auf 0.01 belasse, dann erhalte ich kein Ergebnis bzw. das Programm kommt nicht zum Ende.

Kann mir einer sagen, was an dem Quellcode falsch ist?

Code: Alles auswählen

import numpy as np
import math
import matplotlib.pyplot as plt

L_1=150                			#Ausgemessen
L_2=40                 			#Ausgemessen
L_3=140                			#Ausgemessen
L_4=56                 			#Ausgemessen

phi_12=0               			#start condition aus CAD
phi_23=(83.87/180)*math.pi  	#Startbedingung aus CA
phi_34=(168.88/180)*math.pi 	#Startbedingung aus CAD
winkel1=[]
winkel2=[]

while phi_12<math.pi*2:
	while 1:
		J = np.array([[-L_3*math.sin(phi_23),L_3*math.cos(phi_23)],[-L_4*math.sin(phi_34),-L_4*math.sin(phi_34)]]) # Creates a matrix.
		f_1=np.array([-L_2*math.cos(phi_12)-L_3*math.cos(phi_23)-L_4*math.cos(phi_34), L_1-L_2*math.sin(phi_12)-L_3*math.sin(phi_23)-L_4*math.sin(phi_34)])
		dphi=np.linalg.solve(J,f_1)     	# Solve the linear equation system.
		#dphi=np.dot(np.linalg.inv(J),f_1)
		phi_23=dphi[0]+phi_23
		phi_34=dphi[1]+phi_34
		if dphi[0]<0.01 and dphi[1]<0.01:
			break
	winkel1.append(phi_12)
	winkel2.append(phi_23)
	phi_12+=0.01
plt.plot(winkel1,winkel2)
plt.show()
Grüßle
Vossi
BlackJack

@Vossi: Wenn man sich in der äusseren Schleife `phi_12` ausgeben lässt, sieht man, dass der Wert nur bis 2.7 hochgezählt wird, und dann hängt das Programm in der inneren Schleife.

Und wenn man sich dann in der inneren Schleife die `dphi`-Werte ausgeben lässt, sieht man das die sich immer wiederholen, also nicht zu einem Wertepaar führen das sich 0 annähert:

Code: Alles auswählen

…
- [ 0.16058058  0.72255542]
- [ 0.07558241  0.57551119]
- [-0.13771783  1.69484118]
- [-0.2977093   1.42981424]
- [ 0.02179475  1.04333238]
- [ 0.17746939  0.81713089]
- [ 0.16058058  0.72255542]
- [ 0.07558241  0.57551119]
- [-0.13771783  1.69484118]
- [-0.2977093   1.42981424]
- [ 0.02179475  1.04333238]
- [ 0.17746939  0.81713089]
- [ 0.16058058  0.72255542]
…
Der Quelltext lässt sich übrigens lesbarer formatieren. Insbesondere die Zeilen 18 und 19 sind „write only“ und sehen nach 80er-Jahre Heimrechner-BASIC aus, wo jedes Bytes kostbar war und man versucht hat so viel wie möglich in eine Zeile zu quetschen. :-)

Code: Alles auswählen

#!/usr/bin/env python
# coding: utf8
from __future__ import absolute_import, division, print_function
import math
from math import cos, sin
import matplotlib.pyplot as plt
import numpy as np

# Die folgenden Konstanten sind ausgemessen.
L_1 = 150
L_2 = 40
L_3 = 140
L_4 = 56


def main():
    # 
    # Startbedingungen aus CAD.
    # 
    # TODO Das `math`-Modul kennt Funktionen zur Umrechnung zwischen
    #   Grad<=>Bogenmass.
    # 
    phi_12 = 0
    phi_23 = (83.87 / 180) * math.pi
    phi_34 = (168.88 / 180) * math.pi
    winkel1 = list()
    winkel2 = list()
     
    while phi_12 < math.pi * 2:
        print(phi_12)
        while True:
            J = np.array(
                [
                    [-L_3 * sin(phi_23), L_3 * cos(phi_23)],
                    [-L_4 * sin(phi_34), -L_4 * sin(phi_34)]
                ]
            )
            f_1 = np.array(
                [
                    -L_2 * cos(phi_12) - L_3 * cos(phi_23) - L_4 * cos(phi_34),
                    (
                        L_1
                        - L_2 * sin(phi_12)
                        - L_3 * sin(phi_23)
                        - L_4 * sin(phi_34)
                    )
                ]
            )
            dphi = np.linalg.solve(J, f_1)
            phi_23 += dphi[0]
            phi_34 += dphi[1]
            print('-', dphi)
            if dphi[0] < 0.01 and dphi[1] < 0.01:
                break
        winkel1.append(phi_12)
        winkel2.append(phi_23)
        phi_12 += 0.01

    plt.plot(winkel1, winkel2)
    plt.show()


if __name__ == '__main__':
    main()
Antworten