scrit can't error related to Sortrows and matrix

3 Ansichten (letzte 30 Tage)
Rigo ZOO
Rigo ZOO am 31 Jan. 2024
Bearbeitet: Bruno Luong am 1 Feb. 2024
I tried to run script below, but always have this error linked to sortrows, I checked sol matrice size and the number of column, but I don't understand the issue. If I hide the sol line the script will run, but with wrong results. Please any help to solve this issue?
Error using matlab.internal.math.sortrowsParseInputs Column sorting vector must contain integers with absolute value between 1 and the number of columns in the first argument. Error in sortrows (line 64) [col, nanflag, compareflag] = matlab.internal.math.sortrowsParseInputs(ismatrix(A),size(A,2),A,varargin{:}); Error in TLP (line 110) sol_sorted = sortrows(sol,8);
% Initialisation
clc
clear
format short g
format compact
% ------------------------------------------------------------------------
pi = 3.141592654; %
rho_Water = 1.025; % Water density
% Inputs Part 1: Design parameters Spar; Ballast; Truss cavle; Column Water height
% TOPSIDE PROPERTIES geometry %% Unit m %%
Length = 112;
Width = 58;
Height = 36;
MassT = 25000;
VCG = 16;
Rxx = 23;
Ryy = 45;
Rzz = 51;
% DECK PROPERTIES geometry %% Unit m %%
Deck_Length = 112;
Deck_Width = 58;
Deck_Clearance = 18;
% Tendons PROPERTIES geometry %% Unit m %%
Ltd = 400;
% MASS FACTOR %% Unit T/m3 %%
% Region Factor
Nm = 8; % Number of mooring
Tm = 400; % Tendon lenght
% MASS FACTOR %% Unit T/m3 %%
% Region Factor
rhoNd = 0.25;
rhoPT = 0.2;
rhoCL = 0.15;
rhoTD = 0.2;
% Metacentric height
MetaC = 1;
% Inputs Part 2: Search Parameters (for a 2D Exhaustive Search) ; all parameters in m
Dc1 = 14; % Lower Total spar draft
Dc2 = 30; % Upper Total spar draft
tDc = 2; % Grid size in Draft direction
Hc1 = 25; % Lower Ballast External height
Hc2 = 50; % Upper Ballast External height
tHc = 5; % Grid Ballast External height
Hp1 = 5; % Lower Ballast External Diameter
Hp2 = 11; % Upper Ballast External Diameter
tHp = 1; % Grid Ballast External Diameter // tDb_out = 1
Mt1 = 5000; % Lower Ballast Internal Diameter //4
Mt2 = 12000; % Upper Ballast Internal Diameter
tMt = 1000; % Grid Ballast Internal Diameter
DeckH1 = 4; % Lower Ballast Internal Diameter //4
DeckH2 = 16; % Upper Ballast Internal Diameter
tDeck = 2;
Dtd1 = 0.1; % Lower Ballast Internal Diameter //4
Dtd2 = 0.8; % Upper Ballast Internal Diameter
tDtd = 0.1;
Dt1 = 20;
Dt2 = 30;
tDt = 5;
% ------------------------------------------------------------------------
% Exhaustive Search
sol=[]; % Start with an empty matrix of feasible solutions
data = []; % Start []
% data_feasible = []; % these are only used to show the search space and feasible/infeasible solutions
for Dc=Dc1:tDc:Dc2
for Hc=Hc1:tHc:Hc2
for Hp=Hp1:tHp:Hp2
for Mt=Mt1:tMt:Mt2
for Dtd=Dtd1:tDtd:Dtd2
for DeckH=DeckH1:tDeck:DeckH2
for Dt=Dt1:tDt:Dt2
data = [data; Dt, DeckH, Dtd, Mt, Hp, Hc, Dc]; %, Dts
Cs = Width - Dc;
Lp = Width - 2*Dc;
lpt = Hp;
Vc1 = pi*(Hc-Dt)*(Dc^2)/4;
Vc2 = pi*(Dt-Hp)*(Dc^2)/4;
Vn = pi*Hp*(Dc^2)/4;
Vp = Hp*lpt*Lp;
Vtd = pi*Tm*(Dtd^2)/4;
V = 4*(Vc2 + Vn + Vp) + Nm*Vtd ;
OB = (4*Vc2*(-(Dt-Hp)/2) + 4*Vn*(-(Dt-Hp)-Hp/2) + 4*Vp*(-(Dt-Hp)-Hp/2) + Nm*Vtd*(-(Dt-Hp)-Hp-Tm/2))/V ;
W = MassT + Mt + 4*(Vc1+Vc2)*rhoCL + 4*Vn*rhoNd + 4*Vp*rhoPT + Nm*Vtd*rhoTD;
OG = (((Hc-Dt)+DeckH+Height/2)*MassT + ((Hc-Dt)+DeckH/2)*Mt + (((Hc-Dt)/2)*Vc1*rhoCL*4) + ((-4*(Dt-Hp)/2)*Vc2*rhoCL) + ((-4*(Dt-Hp)+Hp/2)*Vn*rhoNd) + ((-4*(Dt-Hp)+Hp/2)*Vp*rhoPT) + ((-Nm*(Dt-Hp)+Hp+Tm/2)*Vtd*rhoTD))/W ;
GM = OB - OG + 4.*pi.*((Dc).^4./(64.*V));
Fb = W./0.75;
T = W./3;
To = T./Nm;
% Dt = 4*((Fb/(4*rho_Water))-(Width - 2*Dc)*Hp)/(pi*Dc.^2);
if (GM>MetaC)%&& Dc < Cs && Dc>lpt && Dt>Hp && Hc>Dt
% data_feasible=[data_feasible; Dc, Hc, Hp, Mt, Dtd, DeckH, Dt];
W = MassT + Mt + 4.*(Vc1+Vc2).*rhoCL + 4.*Vn.*rhoNd + 4.*Vp.*rhoPT + Nm.*Vtd.*rhoTD ;
% TMass = MassT + Mt + 4*(Vc1+Vc2)*rhoCL + 4*Vn*rhoNd + 4*Vp*rhoPT + Nm*Vtd*rhoTD ;
sol=[sol; Dt, DeckH, Dtd, Mt, Hp, Hc, Dc, W]; %#ok<AGROW> % Store the feasible solution
end
end
end
end
end
end
end
end
% sort the matrix of feasible solutions based on the objective (here vol, the third column)
sol_sorted = sortrows(sol,8);
% Select the first solution (which is the optimum after sorting all solutions based on the objective)
opt_sol=sol_sorted(1,:);
% ------------------------------------------------------------

Antworten (2)

Steven Lord
Steven Lord am 31 Jan. 2024
% sort the matrix of feasible solutions based on the objective (here vol, the third column)
sol_sorted = sortrows(sol,8);
Does your sol matrix have at least 8 columns? From the error message it seems like the answer is no.
The fact that the comment refers to "the third column" makes that look even more suspicious.
  2 Kommentare
Rigo ZOO
Rigo ZOO am 31 Jan. 2024
when checked the matrix sol size I have
sizeOfMatrix =
0 0
Why this matrix is zero?
Bruno Luong
Bruno Luong am 31 Jan. 2024
I suspect the conditional test
if (GM>MetaC)%&& Dc < Cs && Dc>lpt && Dt>Hp && Hc>Dt
is never true so you actually never grow sol.
Learn to use debugger when develop codes.

Melden Sie sich an, um zu kommentieren.


Bruno Luong
Bruno Luong am 31 Jan. 2024
Replace initialization statements
sol=[]; % Start with an empty matrix of feasible solutions
data = [];
with
sol=zeros(0,8); % Start with an empty matrix of feasible solutions
data = zeros(0,7);
  4 Kommentare
Torsten
Torsten am 1 Feb. 2024
To repeat Bruno's answer:
I suspect the conditional test
if (GM>MetaC)%&& Dc < Cs && Dc>lpt && Dt>Hp && Hc>Dt
is never true so you actually never grow sol.
Learn to use debugger when develop codes.
The "sol" matrix remains empty in your code - thus also "sol_sorted(1,:)" does not exist.
Bruno Luong
Bruno Luong am 1 Feb. 2024
Bearbeitet: Bruno Luong am 1 Feb. 2024
@Rigo ZOObut I have the error mentioned below
Index in position 1 exceeds array bounds.
Please replace
opt_sol=sol_sorted(1,:);
by
if isempty(sol_sorted)
error('your exhaustive search is wrong and did not find a solution; you need to double check your code and the parameter values (MetaC)');
end
opt_sol=sol_sorted(1,:);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by