[英]Solving nonlinear equations with “solve”, incorrect solution
我正在測試MATLAB功能,以解決我打算做的項目的方程式,所以我給了它一個簡單的測試運行,但它給我的結果是不正確的。 我試圖解決兩個帶有兩個未知數的非線性方程,其中一個解是正確的,另一個不是。
syms theta d x y
eq1 = d * cos(theta) == x;
eq2 = d * sin(theta) == y;
sol = solve(eq1, eq2, theta, d)
sol.theta
sol.d
d的解決方案是正確的,但對於theta我得到:
-2*atan((x - (x^2 + y^2)^(1/2))/y)
-2*atan((x + (x^2 + y^2)^(1/2))/y)
對theta的正確答案就是atan(y / x)
然后,當我用x = 1,y = 0評估這些解決方案時,我得到:
eval(sol.d)
eval(sol.theta)
d = 1, -1
theta = NaN, -3.1416
d的解決方案是正確的,但那個場景中的theta應該是0.我做錯了什么?
編輯:用手解決它看起來像這樣:用x等式划分y等式
y/x = (d * sin(theta)) / (d * cos(theta))
y/x = sin(theta)/cos(theta)
y/x = tan(theta)
theta = atan(y/x)
即使matlab以其他方式解決它並獲得不同的表達式,當我使用數字時它應該仍然產生相同的最終結果,而它總是如此。
對於x = 1和y = 0,θ應為0,=>這不起作用,它給出NaN(下面的解釋)
對於x = 1和y = 1,theta應該是45度=>這是有效的
對於x = 0和y = 1 theta應該是90度=>這是有效的
我只是用x和y的45度和90度值再次檢查它並且它可以工作,但是對於x = 1和y = 0,它仍然將NaN作為答案之一,這是因為它得到了0/0表達它的方式
-2*atan((x - (x^2 + y^2)^(1/2))/y)
-2*(1 - (1^2 + 0^2))^(1/2)/0
-2*(1 - 1)^(1/2)/0
0/0
但如果它以atan(y / x)的形式出現,結果是
theta = atan(0/1)
theta = atan(0)
theta = 0
你的意思是解決這個問題:
syms a b theta d real
eq1 = a==d * cos(theta) ;
eq2 = b==d * sin(theta) ;
[sol] = solve([eq1 eq2],[d theta] ,'IgnoreAnalyticConstraints', true,'Real',true,'ReturnConditions',true);
當使用符號x
和y
求解方程時,求解器將找到具有特定條件的解,可以使用參數'ReturnCondition'
:
syms x y theta d real
eq1 = d*cos(theta) == x;
eq2 = d*sin(theta) == y;
sol = solve([eq1; eq2],[d theta],'ReturnConditions',true);
這給出了sol
的以下結果
>> sol.d
(x^2 + y^2)^(1/2)
-(x^2 + y^2)^(1/2)
>> sol.theta
2*pi*k - 2*atan((x - (x^2 + y^2)^(1/2))/y)
2*pi*k - 2*atan((x + (x^2 + y^2)^(1/2))/y)
>> sol.parameters
k
>> sol.conditions
y ~= 0 & in(k, 'integer')
y ~= 0 & in(k, 'integer')
如您所見,y = 0不滿足求解器給出的這種通用解,導致y = 0的問題。您可以通過使y
數字而不是符號來找到y = 0的解,或者通過添加假設:
syms x y theta d real
assume(y==0)
sol = solve([eq1; eq2],[d theta],'ReturnConditions',true);
我想在這個條件下設置y = 0數字更容易,因為上面三條線路已經有4種可能的解決方案和條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.