matrix simultaneous equations returning 0s

1 view (last 30 days)
Code should return FM values as 2x104 matrix with x and y values but it keeps returning 0s as x values. I think it's because instead of returning values for each square set in equation, it's returning the whole sum of both xA and xB over the solution rather than breaking it up.
Ax = 0;
Ay = 0.25;
Bx = 0;
By = 3.25;
Py = 30 + 3.5118*7;
x=0.5:0.5:4; nx=length(x);
y=-1:0.5:5; ny=length(y);
xM=zeros(2,nx*ny);
for i=1:nx
for j=1:ny
xM(:,(i-1)*ny+j)=[x(i),y(j)];
AxM = Ax - xM(1, :);
AyM = Ay - xM(2, :);
BxM = Bx - xM(1, :);
ByM = By - xM(2, :);
MA = sqrt(AxM.^2 + AyM.^2);
MB = sqrt(BxM.^2 + ByM.^2);
xA = AxM./MA;
xB = BxM./MB;
yA = AyM./MA;
yB = ByM./MB;
A3 = [xA xB; yA yB];
B3 = [0;Py];
FM = A3.\B3;
end
end
please help, edited to have all values
  8 Comments
Edward Roberts
Edward Roberts on 17 Sep 2022
F1 and F2 are being solved for in my attempted equation

Sign in to comment.

Accepted Answer

Torsten
Torsten on 17 Sep 2022
Edited: Torsten on 17 Sep 2022
Ax = 0;
Ay = 0.25;
Bx = 0;
By = 3.25;
Py = 30 + 3.5118*7;
x=0.5:0.5:4; nx=length(x);
y=-1:0.5:5; ny=length(y);
xM=zeros(2,nx*ny);
FM=zeros(2,nx*ny);
for i=1:nx
for j=1:ny
xM(:,(i-1)*ny+j)=[x(i);y(j)];
AxM = Ax - xM(1, (i-1)*ny+j);
AyM = Ay - xM(2, (i-1)*ny+j);
BxM = Bx - xM(1, (i-1)*ny+j);
ByM = By - xM(2, (i-1)*ny+j);
MA = sqrt(AxM.^2 + AyM.^2);
MB = sqrt(BxM.^2 + ByM.^2);
xA = AxM./MA;
xB = BxM./MB;
yA = AyM./MA;
yB = ByM./MB;
A3 = [xA xB; yA yB];
B3 = [0;Py];
fm = A3\B3;
FM(1,(i-1)*ny+j) = fm(1);
FM(2,(i-1)*ny+j) = fm(2);
end
end
xM
xM = 2×104
0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.5000 1.5000 1.5000 1.5000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 -1.0000 -0.5000 0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 -1.0000 -0.5000 0 0.5000
FM
FM = 2×104
-24.4947 -16.4000 -10.1709 -10.1709 -16.4000 -24.4947 -33.1139 -41.9356 -50.8543 -59.8268 -68.8321 -77.8586 -86.8999 -29.1249 -22.7427 -18.7542 -18.7542 -22.7428 -29.1249 -36.6716 -44.7980 -53.2394 -61.8670 -70.6125 -79.4370 -88.3169 -35.5253 -30.5126 -27.6677 -27.6677 77.8586 68.8321 59.8268 50.8543 41.9356 33.1139 24.4947 16.4000 10.1709 10.1709 16.4000 24.4947 33.1139 79.4370 70.6125 61.8670 53.2394 44.7980 36.6716 29.1249 22.7427 18.7542 18.7542 22.7428 29.1249 36.6716 82.0002 73.4841 65.1253 56.9932
  3 Comments
Edward Roberts
Edward Roberts on 18 Sep 2022
This worked perfectly, thank you so much. I apologise if my incompetence with MATLAB is frustrating to deal with.
Walter Roberson
Walter Roberson on 18 Sep 2022
We help teach MATLAB, so not knowing MATLAB is not inherently a problem. But when we ask for information multiple times and the information is not provided, we might give up.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 16 Sep 2022
FM = A3.\B3;
You overwrite all of FM each iteration. The final result will be what was assigned for the final i j combination.
If that is deliberate then notice that you could omit everything except the assignment to xM inside of the loop and move everything else in the loop to after the loop since you overwrite all of those variables anyway.
  1 Comment
Edward Roberts
Edward Roberts on 17 Sep 2022
I'm trying to do a simultaneous equation solve with the variables being matrices.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!