簡體   English   中英

使用python解決三角非線性方程:我在做什么錯?

[英]solving trigonometric nonlinear equations using python: what am I doing wrong?

我正在嘗試求解一個三角方程組,我認為Python無法生成正確的解決方案。 我想解決的方程式:

  • 1 - 2cosθ1 +2cosθ2 - 2cosθ3 = -0.8

  • 1 - 2cos5θ1 +2cos5θ2 - 2cos5θ3 = 0

  • 1 - 2cos7θ1 +2cos7θ2 - 2cos7θ3 = 0

我的Python代碼:

from scipy.optimize import fsolve
import math
import numpy as np

def equations(p):
    x,y,z = p
    f1 = (1 - 2*math.cos(math.radians(x)) + 2*math.cos(math.radians(y)) - 2*math.cos(math.radians(z)) + 0.8)
    f2 = (1 - 2*math.cos(math.radians(5*x)) + 2*math.cos(math.radians(5*y)) - 2*math.cos(math.radians(5*z)))
    f3 = (1 - 2*math.cos(math.radians(7*x)) + 2*math.cos(math.radians(7*y)) - 2*math.cos(math.radians(7*z)))
    return (f1,f2,f3)

x,y,z = fsolve(equations,(0,0,0))

print equations((x,y,z))

打印:

(-1.9451107391432743e-13、4.241273998673023e-12,-1.5478729409323932e-12)

這是錯誤的,因為我使用以下命令進行了檢查:

print (1 - 2*math.cos(math.radians(5*-1.9451107391432743e-13)) + 
           2*math.cos(math.radians(5*4.241273998673023e-12)) - 
           2*math.cos(math.radians(5*-1.5478729409323932e-12)))

這不會打印0,但會打印-1 有人可以告訴我我在做什么錯嗎?

另一個問題是fsolve根據初始值生成解決方案。 因此,如果我將初始值從(0,0,0)更改為(1,1,1), I might get another new solution. Is there a way I can define a "range" of initial values for each variable (1,1,1), I might get another new solution. Is there a way I can define a "range" of initial values for each variable x,y,z` (1,1,1), I might get another new solution. Is there a way I can define a "range" of initial values for each variable並獲得一系列的解決方案?

您的代碼似乎沒問題。 它在最后顯示錯誤,而不是解決方案。 實際上,您是在通過最后兩行中由fsolve找到的答案調用方程式來檢查解決方案。 如果要查看變量的值,可以print x, y, z

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM