Error when using lsqnonlin in for loop
古いコメントを表示
Hello, I am having some issuen when trying to implement the lsqnonlin iteration into a for loop.
I have the 3d coordinates of four known points, and also four known distances between each point to a fifth unknown point.
As I have a time-series of data I wanted to create a function that loops through all the data:
function Estimated_Position = trilaterate_new(p1, p2, p3, p4, distances, initial_guess)
% p1-p4 = 3D coordinated for the four known points, each the size of 100x3 (ex [x1 y1 z1; x2, y2, z2]
% distances = Known distance from each point to the fifth unknown point (ex. [d1 d2 d3 d4])
% initial_guess = initial guess of the position of point 5 (ex. [0 0 0])
options = optimoptions(@lsqnonlin,'Display','off');
for i = 1:100
objective_function = @(p5) [
sqrt((p1(i,1) - p5(1))^2 + (p1(i,2) - p5(2))^2 + (p1(i,3) - p5(3))^2) - distances(i,1);
sqrt((p2(i,1) - p5(1))^2 + (p2(i,2) - p5(2))^2 + (p2(i,3) - p5(3))^2) - distances(i,2);
sqrt((p3(i,1) - p5(1))^2 + (p3(i,2) - p5(2))^2 + (p3(i,3) - p5(3))^2) - distances(i,3);
sqrt((p4(i,1) - p5(1))^2 + (p4(i,2) - p5(2))^2 + (p4(i,3) - p5(3))^2) - distances(i,4)];
Estimated_Position(i,:) = lsqnonlin(objective_function, initial_guess, [], [], options);
end
end
The issue occurs when i=2 and I get down to execute lsqnonlin. I then receieve the error "Index in position 1 exceeds array bounds. Index must not exceed 1."
If anyone has any suguestions, please let me know
3 件のコメント
Torsten
2024 年 1 月 15 日
Execute the code
function Estimated_Position = trilaterate_new(p1, p2, p3, p4, distances, initial_guess)
% p1-p4 = 3D coordinated for the four known points, each the size of 100x3 (ex [x1 y1 z1; x2, y2, z2]
% distances = Known distance from each point to the fifth unknown point (ex. [d1 d2 d3 d4])
% initial_guess = initial guess of the position of point 5 (ex. [0 0 0])
size(p1)
size(p2)
size(p3)
size(p4)
size(distances)
size(initial_guess)
end
and tell us what MATLAB reports about the sizes.
Ellen Ryckenberg
2024 年 1 月 15 日
i=1;
N=10;
p1=rand(N,3); p2=rand(N,3); p3=rand(N,3); p4=rand(N,3); distances=rand(N,4);
initial_guess=ones(3,1);
for i=1:4
objective_function = @(p5) [
sqrt((p1(i,1) - p5(1))^2 + (p1(i,2) - p5(2))^2 + (p1(i,3) - p5(3))^2) - distances(i,1);
sqrt((p2(i,1) - p5(1))^2 + (p2(i,2) - p5(2))^2 + (p2(i,3) - p5(3))^2) - distances(i,2);
sqrt((p3(i,1) - p5(1))^2 + (p3(i,2) - p5(2))^2 + (p3(i,3) - p5(3))^2) - distances(i,3);
sqrt((p4(i,1) - p5(1))^2 + (p4(i,2) - p5(2))^2 + (p4(i,3) - p5(3))^2) - distances(i,4)];
Estimated_Position(i,:) = lsqnonlin(objective_function, initial_guess);
end
Estimated_Position
Seems to work ok with made up data; something must be in what isn't being shown...
NOTA BENE: The array for Estimated_Position is not preallocated; perhaps your code sets an initial single vector?
clear Estimated_Position
Estimated_Position=zeros(1,3); % preset an initial vector
i=1;
N=10;
p1=rand(N,3); p2=rand(N,3); p3=rand(N,3); p4=rand(N,3); distances=rand(N,4);
initial_guess=ones(3,1);
for i=1:4
objective_function = @(p5) [
sqrt((p1(i,1) - p5(1))^2 + (p1(i,2) - p5(2))^2 + (p1(i,3) - p5(3))^2) - distances(i,1);
sqrt((p2(i,1) - p5(1))^2 + (p2(i,2) - p5(2))^2 + (p2(i,3) - p5(3))^2) - distances(i,2);
sqrt((p3(i,1) - p5(1))^2 + (p3(i,2) - p5(2))^2 + (p3(i,3) - p5(3))^2) - distances(i,3);
sqrt((p4(i,1) - p5(1))^2 + (p4(i,2) - p5(2))^2 + (p4(i,3) - p5(3))^2) - distances(i,4)];
Estimated_Position(i,:) = lsqnonlin(objective_function, initial_guess);
end
Estimated_Position
Well, no, that didn't cause a bounds error, either.
We'll have to see the actual code that created the error, can't reproduce it from the snippet given, sorry...
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!