MATLAB: Linear equation with multiple values for a constant

simultaneous equations

Hi Everyone,
I am trying to solve these 2 equation simultaneously, Dx+4y=2, Dx+2y=4 for 2 values of D which are 1 and 3. I have written the code but it only gives 1 value. Help will be great. Thanks
>> D=[1 2];
for k=1:length(D)
Z=solve([D(k)*x+4*y==2,D(k)*x+2*y==4],[x,y])
end
Z =
x: [1x1 sym]
y: [1x1 sym]
Z =
x: [1x1 sym]
y: [1x1 sym]
>> Z.x
ans =
3

Best Answer

It will give both of them for both values of ā€˜Dā€™ if you ask it to:
syms x y
D=[1 2];
for k=1:length(D)
Z{k} = solve([D(k)*x+4*y==2,D(k)*x+2*y==4],[x,y]);
end
D1 = [Z{1}.x; Z{1}.y]
D2 = [Z{2}.x; Z{2}.y]
D1 =
6
-1
D2 =
3
-1