Solving and Plotting Arrays

I am trying to find and plot the values of xSol and ySol by using x and y arrays as follow:
clc
clear all
% DME COORDINATES
x1= 2; y1= 10; % DME 1
x2= 15; y2= 30; % DME 2
x3= 20; y3= 3; % DME 3
% ASSUMED POSITION OF OBJECT
x = [10,25,31,35,40];
y = [20,30,41,49,56];
% DISTANCE OF OBJECT TO DMEs
r1 = sqrt((x-x1).^2+(y-y1).^2);
r2 = sqrt((x-x2).^2+(y-y2).^2);
r3 = sqrt((x-x3).^2+(y-y3).^2);
% EQUATIONS
syms x y
eqn1 = 2*x*(x2-x1)+2*y*(y2-y1) == (r1.^2) - (r2.^2) + (x2.^2) - (x1.^2) + (y2.^2) - (y1.^2);
eqn2 = 2*x*(x3-x1)+2*y*(y3-y1) == (r1.^2) - (r3.^2) + (x3.^2) - (x1.^2) + (y3.^2) - (y1.^2);
sol = solve([eqn1, eqn2], [x, y]);
i=length(x);
j=length(y);
for k=1:i
xSol = sol.x(k);
end
Index exceeds the number of array elements. Index must not exceed 0.

Error in indexing (line 1075)
R_tilde = builtin('subsref',L_tilde,Idx);
for m=1:j
ySol = sol.y(m);
end
plot(xSol,ySol,'LineWidth',4)
But it gives me error saying index exceeds. Kindly help!

5 commentaires

Jeffrey Clark
Jeffrey Clark le 4 Juin 2022
I don't have the Symbolic Math Toolbox, but the documentation indictates If solve returns an empty object, then no solutions exist. So the sol.x and sol.y would be empty. You should use the length of sol.x and sol.y for i and j, not the length of x and y.
There is no solution —
% DME COORDINATES
x1= 2; y1= 10; % DME 1
x2= 15; y2= 30; % DME 2
x3= 20; y3= 3; % DME 3
% ASSUMED POSITION OF OBJECT
x = [10,25,31,35,40];
y = [20,30,41,49,56];
% DISTANCE OF OBJECT TO DMEs
r1 = sqrt((x-x1).^2+(y-y1).^2);
r2 = sqrt((x-x2).^2+(y-y2).^2);
r3 = sqrt((x-x3).^2+(y-y3).^2);
% EQUATIONS
syms x y
eqn1 = 2*x*(x2-x1)+2*y*(y2-y1) == (r1.^2) - (r2.^2) + (x2.^2) - (x1.^2) + (y2.^2) - (y1.^2)
eqn1 = 
eqn2 = 2*x*(x3-x1)+2*y*(y3-y1) == (r1.^2) - (r3.^2) + (x3.^2) - (x1.^2) + (y3.^2) - (y1.^2)
eqn2 = 
[A1,b1] = equationsToMatrix(eqn1)
A1 = 
b1 = 
[A2,b2] = equationsToMatrix(eqn2)
A2 = 
b2 = 
A = [A1;A2]
A = 
b = [b1;b2]
b = 
[x,RankA] = linsolve(A,b)
Warning: Solution does not exist because the system is inconsistent.
x = 
RankA = 
2
.
Curious
Curious le 4 Juin 2022
Well, great.. Thanks alot Star Strider
Star Strider
Star Strider le 4 Juin 2022
My pleasure!
I just wish I could have found a solution for you!
Curious
Curious le 4 Juin 2022
I don't need a solution, having no solution is just a case. I am making a program that can solve this system of equations on different positions of x,y and plot those points. Well, at some points the systems may say it has no solution;)

Connectez-vous pour commenter.

Réponses (0)

Question posée :

le 4 Juin 2022

Commenté :

le 4 Juin 2022

Community Treasure Hunt

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

Start Hunting!

Translated by