如何解决为不同数字编写的两个方程和两个未知数代码?
例如:
aX + bY = c * d
x + y = c
c = 3
d = 30
用于a
,b
用于不同的
a = [0:1:30]
b = [60:1:90]
代表a=1
b=60
,a=1
b=61
,..... a=30
b=90
。
数字a
和b
是常数,X
和Y
未知。
答案 0 :(得分:2)
示例:
%// all a and b you're interested in
As = [(0:30)' (60:90)'];
%// the solution vector is always the same
b = [90; 3];
%// solve all systems using mldivide
for ii = size(As,1):-1:1
XY(ii,:) = [As(ii,:); [1 1]]\b; end
但是,如果你更聪明,你就已经认识到了
x = c - y
⇒ a(c-y) = c·d - by
⇒ y = c·(a-d)/(a-b)
因此,你已编码
a = 0:30; c = 3;
b = 60:90; d = 30;
y = c*(a-d)./(a-b);
x = c - y;