江一a 发表于 2024-5-13 18:33:40

FishC 发表于 2024-5-13 17:00
在绘制图形的代码中,存在一些问题。以下是修复后的代码:




为什么只画出一条曲线
from sympy import symbols, Eq, solve, nsolve
import math
import turtle as t
L1,L2,L3,L4 = 26.5,111.6,67.5,87.5 #杆长

#角位移
angle1 = []
angle2 = []
angle3 = []
for i in range(120):#每3度求解存储数据
    angle1.append(i*3)
for constant in angle1:
    #解非线性方程,求解角位移
    constant_c = L4-L1*math.cos(math.radians(constant))
    constant_s = -L1*math.sin(math.radians(constant))
    w, x, y, z = symbols('w x y z ')
    eqs = [Eq(L2*w - L3*x, constant_c),
         Eq(L2*y - L3*z, constant_s),
         Eq(w**2 + y**2,1),
         Eq(x**2 + z**2,1)]
    displacement_quation = solve(eqs, )
    #判断符合实际的解
    if displacement_quation>= 0:
      angle2.append(math.acos(displacement_quation))
      angle3.append(math.acos(displacement_quation))
    else:
      angle2.append(math.acos(displacement_quation))
      angle3.append(math.acos(displacement_quation))

#角速度
speed_angle1 = 1 #rad/s
speed_angle2 = []
speed_angle3 = []
for i in range(len(angle1)):
    constant2_s = L2*math.sin(angle2)
    constant3_s = L3*math.sin(angle3)
    constant1_s = L1*math.sin(math.radians(angle1))*speed_angle1
    constant2_c = L2*math.cos(angle2)
    constant3_c = L3*math.cos(angle3)
    constant1_c = L1*math.cos(math.radians(angle1))*speed_angle1
    x, y = symbols('x y')
    eqs = [Eq(-constant2_s*x + constant3_s*y, constant1_s),
         Eq(constant2_c*x - constant3_c*y, -constant1_c)]
    speed_quation = solve(eqs, )
    speed_angle2 .append(speed_quation)
    speed_angle3 .append(speed_quation)


#角加速度
acceleration_angle1 = 0
acceleration_angle2 = []
acceleration_angle3 = []
for i in range(len(angle1)):
    constant2_s = L2*math.sin(angle2)
    constant3_s = L3*math.sin(angle3)
    constant1_s = L1*math.sin(math.radians(angle1))
    constant2_c = L2*math.cos(angle2)
    constant3_c = L3*math.cos(angle3)
    constant1_c = L1*math.cos(math.radians(angle1))
    constant_s = constant2_s*speed_angle2**2 - constant3_s*speed_angle3**2 + constant1_s
    constant_c = constant2_c*speed_angle2**2 - constant3_c*speed_angle3**2 + constant1_c
    x, y = symbols('x y')
    eqs = [Eq(-constant2_s*x + constant3_s*y,constant_c),
         Eq(constant2_c*x - constant3_c*y,constant_s)]
    acceleration_quation = solve(eqs, )
    acceleration_angle2 .append(acceleration_quation)
    acceleration_angle3 .append(acceleration_quation)


#数据输出
print("{:>4},{:>10},{:>6},{:>6},{:>9},{:>7},{:>6}".format("角",
    "角2",
    "角2速度",
    "角2加速度",
    "角3",
    "角3速度",
    "角3加速度"))

t.hideturtle()
t.speed("fastest")
# 直角坐标系
t.color('red')
t.penup()
t.goto(-500, 0)
t.pendown()
t.goto(500, 0)
t.penup()
t.goto(0, -500)
t.pendown()
t.goto(0, 500)

t.penup()
t.goto(5*angle1, 5*math.degrees(angle2))
t.color('blue')
t.width(2)
t.pendown()
for i in range(len(angle1)):
    t.goto(5*angle1,5*math.degrees(angle2))
t.penup()
t.goto(5*angle1, 5*math.degrees(*speed_angle2))

t.color('blue')
t.width(3)
t.pendown()
for i in range(len(angle1)):
    t.goto(5*angle1, 5*math.degrees(speed_angle2))
t.penup()
t.goto(5*angle1,5*math.degrees(acceleration_angle2))


t.color('blue')
t.width(4)
t.pendown()
for i in range(len(angle1)):
    t.goto(angle1, math.degrees(acceleration_angle2))
t.penup()
t.goto(5*angle1, 5*math.degrees(angle3))


t.color('yellow')
t.width(2)
t.pendown()

for i in range(len(angle1)):
    t.goto(5*angle1,5*math.degrees(angle3))
t.penup()
t.goto(5 * angle1, 5 * math.degrees(speed_angle3))


t.color('yellow')
t.width(3)
t.pendown()
for i in range(len(angle1)):
    t.goto(angle1, math.degrees(speed_angle3))
t.penup()
t.goto(5*angle1,5* math.degrees(acceleration_angle3))


t.color('yellow')
t.width(4)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(acceleration_angle3))

t.mainloop()
t.done()

FishC 发表于 2024-5-13 18:35:28

江一a 发表于 2024-05-13 18:33
为什么只画出一条曲线
from sympy import symbols, E ...

问题出在对角度列表进行索引的地方。由于angle1、angle2和angle3在代码中定义为空列表,所以无法对其进行索引操作。为了解决这个问题,您可以使用enumerate函数来同时遍历angle1、angle2和angle3列表,并获取它们的索引值和对应的元素值。以下是修复后的代码:

from sympy import symbols, Eq, solve
import math
import turtle as t

L1, L2, L3, L4 = 26.5, 111.6, 67.5, 87.5# 杆长

# 角位移
angle1 = []
angle2 = []
angle3 = []
for i in range(120):# 每3度求解存储数据
    angle1.append(i * 3)
for constant in angle1:
    # 解非线性方程,求解角位移
    constant_c = L4 - L1 * math.cos(math.radians(constant))
    constant_s = -L1 * math.sin(math.radians(constant))
    w, x, y, z = symbols('w x y z ')
    eqs = ...
    displacement_equation = solve(eqs, x, y)
    # 判断符合实际的解
    for d in displacement_equation:
      if d >= 0:
            angle2.append(math.acos(d))
            angle3.append(math.acos(d))

# 角速度
speed_angle1 = 1# rad/s
speed_angle2 = []
speed_angle3 = []
for i, a1 in enumerate(angle1):
    constant2_s = L2 * math.sin(angle2)
    constant3_s = L3 * math.sin(angle3)
    constant1_s = L1 * math.sin(math.radians(a1)) * speed_angle1
    constant2_c = L2 * math.cos(angle2)
    constant3_c = L3 * math.cos(angle3)
    constant1_c = L1 * math.cos(math.radians(a1)) * speed_angle1
    x, y = symbols('x y')
    eqs = ...
    speed_equation = solve(eqs, x, y)
    for s in speed_equation:
      speed_angle2.append(s)
      speed_angle3.append(s)

# 角加速度
acceleration_angle1 = 0
acceleration_angle2 = []
acceleration_angle3 = []
for i, a1 in enumerate(angle1):
    constant2_s = L2 * math.sin(angle2)
    constant3_s = L3 * math.sin(angle3)
    constant1_s = L1 * math.sin(math.radians(a1))
    constant2_c = L2 * math.cos(angle2)
    constant3_c = L3 * math.cos(angle3)
    constant1_c = L1 * math.cos(math.radians(a1))
    constant_s = constant2_s * speed_angle2 ** 2 - constant3_s * speed_angle3 ** 2 + constant1_s
    constant_c = constant2_c * speed_angle2 ** 2 - constant3_c * speed_angle3 ** 2 + constant1_c
    x, y = symbols('x y')
    eqs = ...
    acceleration_equation = solve(eqs, x, y)
    for a in acceleration_equation:
      acceleration_angle2.append(a)
      acceleration_angle3.append(a)

# 数据输出
print("{:>4},{:>10},{:>6},{:>6},{:>9},{:>7},{:>6}".format("角",
                                                         "角2",
                                                         "角2速度",
                                                         "角2加速度",
                                                         "角3",
                                                         "角3速度",
                                                         "角3加速度"))

t.hideturtle()
t.speed("fastest")
# 直角坐标系
t.color('red')
t.penup()
t.goto(-500, 0)
t.pendown()
t.goto(500, 0)
t.penup()
t.goto(0, -500)
t.pendown()
t.goto(0, 500)

t.penup()
t.goto(5 * angle1, 5 * math.degrees(angle2))
t.color('blue')
t.width(2)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(angle2))

t.width(3)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(speed_angle2))

t.width(4)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(acceleration_angle2))

t.penup()
t.goto(5 * angle1, 5 * math.degrees(angle3))
t.color('yellow')
t.width(2)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(angle3))

t.width(3)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(speed_angle3))

t.width(4)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(acceleration_angle3))

t.mainloop()
t.done()

这样应该能够正确地在画布上绘制出计算出来的点了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

六翻了 发表于 2024-5-14 10:15:53

6

宁宁学代码 发表于 2024-5-14 16:17:53

真的很高深这代码,我还是初学者我还是再学习学习吧{:10_254:}

江一a 发表于 2024-5-19 22:33:27

江一a 发表于 2024-5-13 18:33
为什么只画出一条曲线
from sympy import symbols, Eq, solve, nsolve
import math


from sympy import symbols, Eq, solve, nsolve
import math
import turtle as t
import tkinter as tk

def on_submit_button_click():
    global L1, L2, L3, L4,speed_angle1# 声明变量是全局变量


L1_value = L1_entry.get()
L2_value = L2_entry.get()
L3_value = L3_entry.get()
L4_value = L4_entry.get()
speed_angle1_value = speed_angle1_entry.get()


root = tk.Tk()
root.title("变量赋值界面")

# 创建Entry控件和标签
labels = ['L1', 'L2', 'L3', 'L4', 'speed_angle1 ']
entries = []
for label in labels:
    tk.Label(root, text=label + ":").pack()
    entry = tk.Entry(root)
    entry.pack()
    entries.append(entry)

L1_entry, L2_entry, L3_entry, L4_entry, speed_angle1_entry= entries

submit_button = tk.Button(root, text="提交", command=on_submit_button_click)
submit_button.pack()


# 开始Tkinter事件循环
root.mainloop()





#L1,L2,L3,L4 = 26.5,111.6,67.5,87.5

#角位移
angle1 = []
angle2 = []
angle3 = []
for i in range(120):#每3度求解存储数据
    angle1.append(i*3)
for constant in angle1:
    #解非线性方程,求解角位移
    constant_c = L4-L1*math.cos(math.radians(constant))
    constant_s = -L1*math.sin(math.radians(constant))
    w, x, y, z = symbols('w x y z ')
    eqs = [Eq(L2*w - L3*x, constant_c),
         Eq(L2*y - L3*z, constant_s),
         Eq(w**2 + y**2,1),
         Eq(x**2 + z**2,1)]
    displacement_quation = solve(eqs, )
    #判断符合实际的解
    if displacement_quation>= 0:
      angle2.append(math.acos(displacement_quation))
      angle3.append(math.acos(displacement_quation))
    else:
      angle2.append(math.acos(displacement_quation))
      angle3.append(math.acos(displacement_quation))

#角速度
speed_angle1 = 1 #rad/s
speed_angle2 = []
speed_angle3 = []
for i in range(len(angle1)):
    constant2_s = L2*math.sin(angle2)
    constant3_s = L3*math.sin(angle3)
    constant1_s = L1*math.sin(math.radians(angle1))*speed_angle1
    constant2_c = L2*math.cos(angle2)
    constant3_c = L3*math.cos(angle3)
    constant1_c = L1*math.cos(math.radians(angle1))*speed_angle1
    x, y = symbols('x y')
    eqs = [Eq(-constant2_s*x + constant3_s*y, constant1_s),
         Eq(constant2_c*x - constant3_c*y, -constant1_c)]
    speed_quation = solve(eqs, )
    speed_angle2 .append(speed_quation)
    speed_angle3 .append(speed_quation)


#角加速度
acceleration_angle1 = 0
acceleration_angle2 = []
acceleration_angle3 = []
for i in range(len(angle1)):
    constant2_s = L2*math.sin(angle2)
    constant3_s = L3*math.sin(angle3)
    constant1_s = L1*math.sin(math.radians(angle1))
    constant2_c = L2*math.cos(angle2)
    constant3_c = L3*math.cos(angle3)
    constant1_c = L1*math.cos(math.radians(angle1))
    constant_s = constant2_s*speed_angle2**2 - constant3_s*speed_angle3**2 + constant1_s
    constant_c = constant2_c*speed_angle2**2 - constant3_c*speed_angle3**2 + constant1_c
    x, y = symbols('x y')
    eqs = [Eq(-constant2_s*x + constant3_s*y,constant_c),
         Eq(constant2_c*x - constant3_c*y,constant_s)]
    acceleration_quation = solve(eqs, )
    acceleration_angle2 .append(acceleration_quation)
    acceleration_angle3 .append(acceleration_quation)




t.hideturtle()
t.speed("fastest")
# 直角坐标系
t.color('red')
t.penup()
t.goto(-500, 0)
t.pendown()
t.goto(500, 0)
t.penup()
t.goto(0, -500)
t.pendown()
t.goto(0, 500)

t.penup()
t.goto(5*angle1, 5*math.degrees(angle2))
t.color('blue')
t.width(2)
t.pendown()
for i in range(len(angle1)):
    t.goto(5*angle1,5*math.degrees(angle2))
t.penup()
t.goto(5*angle1, 5*math.degrees(speed_angle2))

t.color('blue')
t.width(3)
t.pendown()
for i in range(len(angle1)):
    t.goto(5*angle1, 5*math.degrees(speed_angle2))
t.penup()
t.goto(5*angle1,5*math.degrees(acceleration_angle2))


t.color('blue')
t.width(4)
t.pendown()
for i in range(len(angle1)):
    t.goto(angle1, math.degrees(acceleration_angle2))
t.penup()
t.goto(5*angle1, 5*math.degrees(angle3))


t.color('yellow')
t.width(2)
t.pendown()

for i in range(len(angle1)):
    t.goto(5*angle1,5*math.degrees(angle3))
t.penup()
t.goto(5 * angle1, 5 * math.degrees(speed_angle3))


t.color('yellow')
t.width(3)
t.pendown()
for i in range(len(angle1)):
    t.goto(angle1, math.degrees(speed_angle3))
t.penup()
t.goto(5*angle1,5* math.degrees(acceleration_angle3))


t.color('yellow')
t.width(4)
t.pendown()
for i in range(len(angle1)):
    t.goto(5 * angle1, 5 * math.degrees(acceleration_angle3))

t.mainloop()


赋值界面有错误

江一a 发表于 2024-5-19 22:36:47

宁宁学代码 发表于 2024-5-14 16:17
真的很高深这代码,我还是初学者我还是再学习学习吧

{:5_100:}我也是小菜鸡

小肥狼haoran 发表于 2024-5-20 21:39:48

怎么样?楼主,AI解决掉你的问题了吗?
页: 1 [2]
查看完整版本: 代码跑不起来