matrix simultaneous equations returning 0s
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Torsten
2022-9-17
编辑:Torsten
2022-9-17
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
FM
3 个评论
Walter Roberson
2022-9-18
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.
更多回答(1 个)
Walter Roberson
2022-9-16
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!