matrix simultaneous equations returning 0s
Show older comments
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
Torsten
on 16 Sep 2022
We can't test your code because other variables are missing.
Walter Roberson
on 16 Sep 2022
xM(:,(i-1)*ny+j)=[x(i),y(j)];
AxM = Ax - xM(1, :);
The first iteration of the loop, xM starts as all zero and has a column written into. It then uses the entire rows including the column just written and all the zeros.
The second iteration, an additional column is written into, and you then use the entire row including all of the remaining zeros and the two columns written into so far. And so on.
Is that your intention, to fill in one more column each time and use the accumulated values?
Edward Roberts
on 17 Sep 2022
Walter Roberson
on 17 Sep 2022
I cannot tell which variables in that correspond to which variables in your code. In terms of those equations, I cannot tell which variables you are trying to solve for.
Edward Roberts
on 17 Sep 2022
Walter Roberson
on 17 Sep 2022
In your F1 and so on formula, what variables are being solved for? When I try to match against your code, everything appears to be constant or input or calculated from input.
It would help if you could annotate your F1 etc. formula showing the size of each variable and whether it is known or to be solved for.
Edward Roberts
on 17 Sep 2022
Walter Roberson
on 17 Sep 2022
okay, good luck with that.
Accepted Answer
More Answers (1)
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
on 17 Sep 2022
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!