Matlab 如何使用两个方程和一系列预定义变量求解多变量
我得到了2个方程,共有8个变量,但将定义6个。6个变量的列表不会每次都保持不变。我想求解变化的未知变量。我被告知尝试使用fsolve 方程式如下:Matlab 如何使用两个方程和一系列预定义变量求解多变量,matlab,Matlab,我得到了2个方程,共有8个变量,但将定义6个。6个变量的列表不会每次都保持不变。我想求解变化的未知变量。我被告知尝试使用fsolve 方程式如下: 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是变量,所有θ也是变量 根据Mathworks文档(),fsolve至少需
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是变量,所有θ也是变量 根据Mathworks文档(),
fsolve
至少需要两个参数:fun
和x0
fun
是一个函数,在求解时产生一个零向量
x0
是解决方案的初始猜测
换句话说,如果
x = fsolve(fun,x0)
然后
在您的案例中,什么是乐趣
和x
从你的问题来看,我不清楚什么是x
,但它应该是一个包含两个未知变量的2x1向量。(为了回答这个问题,我假设您希望为Rao
和Rco
求解,并在更新问题时进行编辑。)
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_函数
定义一个新函数,该函数只接受两个参数
在这里,我定义了一个函数,其中填充了已知参数,但未知参数用新函数参数表示
constrained_function = @(x) full_function([x(1); Rab; Rbc; x(2); theta1; theta2; theta3; theta4]);
现在,fsolve
将只尝试查找Rao
和Rco
的值
因此,完整的代码应该如下所示
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
到目前为止,您尝试了什么?我让用户输入已知变量并留下未知空格,然后将2个方程放入一个函数中,并将该函数调用到fsolver中,但得到了错误未定义的函数或变量。我也不完全确定如何使用fsolve或start来编写脚本。这两个等式在我看来是一样的(除了第二个等式末尾的
]
?
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