2017-01-23 56 views
-1

我給出了2個等式,其中總共有8個變量,但將定義6個。 6個變量的列表不會每次都保持不變。我想解決變化的未知變量。我被告知嘗試使用fsolve。如何使用2個方程和多個預定義變量列表求解多個變量

的公式是:

0=Rao*cos(theta2)+Rab*cos(theta3)+Rbc*cos(theta4)-Rco*cos(theta1); 
0=Rao*cos(theta2)+Rab*cos(theta3)+Rbc*cos(theta4)-Rco*cos(theta1)]; 

凡的r是變量和所有的θ驅動是爲好。

+1

你嘗試過什麼到目前爲止? – Suever

+0

我讓用戶輸入已知變量並留下未知空白,然後將2個方程式放入一個函數中,並將函數調用到fsolver中,但得到錯誤的未定義函數或變量。即時通訊也不完全確定如何使用fsolve或開始爲腳本 –

+1

這兩個方程對我來說看起來是一樣的,(除了在第二個末尾的']')? – Justin

回答

0

根據Mathworks文檔(https://www.mathworks.com/help/optim/ug/fsolve.html),fsolve需要至少兩個參數:funx0

fun是一個函數,當在解中計算時產生零向量。

x0是解決方案的初始猜測。

換句話說,如果

x = fsolve(fun,x0) 

然後

fun(x) = 0 

在你的情況,什麼是funx

從你的問題中,我不清楚x是什麼,但它應該是一個2×1的矢量包含兩個未知的變量。 (對於這個答案的目的,我會假設你要解決RaoRco和編輯,如果你更新你的問題。)

fun是根據你列出的公式確定。它們已經在等式一邊爲0的格式。

此外,從你的問題,你的兩個方程似乎是相同的,這意味着fsolve將找到一個解決方案,但它不會是唯一的。我最好的猜測是,你的意思是要解決

0=Rao*cos(theta2)+Rab*cos(theta3)+Rbc*cos(theta4)-Rco*cos(theta1); 
0=Rao*sin(theta2)+Rab*sin(theta3)+Rbc*sin(theta4)-Rco*sin(theta1); 

所以,你可以定義一個函數

function y = full_function(x) 
    y = [x(1)*cos(x(6))+x(2)*cos(x(7))+x(3)*cos(x(8))-x(4)*cos(x(5)); 
     x(1)*sin(x(6))+x(2)*sin(x(7))+x(3)*sin(x(8))-x(4)*sin(x(5))]; 
end 

轉換您的變量名稱爲x的組件和解決fsolve功能

x0 = zero(8,1); % Use a different initial guess if you know more about the problem 
x_full = fsolve(@full_function,x0); 

Rao = x_full(1); Rab = x_full(2); 
Rbc = x_full(3); Rco = x_full(4); 
theta1 = x_full(5); theta2 = x_full(6); 
theta3 = x_full(7); theta4 = x_full(8); 

但是等一下。爲什麼MATLAB解決所有8個變量?我們要指定其中的6個,並解決2.

在這種情況下,您可以根據full_function(僅佔用2個參數)定義一個新函數。

在這裏,我定義這裏,已知參數填入一個功能,但未知參數在新函數的參數來表示現在

constrained_function = @(x) full_function([x(1); Rab; Rbc; x(2); theta1; theta2; theta3; theta4]); 

fsolve只會試圖找到值RaoRco

所以,完整的代碼應該看起來像

function q41811094_fsolve_fixed_params() 
    % Unknown variables - Enter initial guess 
    Rao = 1; Rco = 1; 

    % Known variables - Enter known values 
    Rab = 1; Rbc = 1; 
    theta1 = 0; theta2 = 0; 
    theta3 = 0; theta4 = 0; 

    % Package guesses for unknown variables 
    x0 = [Rao; Rco]; 

    % Define new function with 6 defined values 
    constrained_function = @(x) full_function([x(1); Rab; Rbc; x(2); theta1; theta2; theta3; theta4]); 

    % Use fsolve to find 2 remaining values 
    x_constrained = fsolve(constrained_function, x0); 

    % Unpackage variable values 
    Rao = x_constrained(1) 
    Rco = x_constrained(2) 

end 

% Define function for all 8 parameters 
function y = full_function(x) 
    y = [x(1)*cos(x(6))+x(2)*cos(x(7))+x(3)*cos(x(8))-x(4)*cos(x(5)); 
     x(1)*sin(x(6))+x(2)*sin(x(7))+x(3)*sin(x(8))-x(4)*sin(x(5))]; 
end