From f20e46f9b2c6431a330e6c4005043fab19e963f9 Mon Sep 17 00:00:00 2001
From: Mauco03 <marco.gaibotti@skywarder.eu>
Date: Wed, 19 Feb 2025 22:12:18 +0100
Subject: [PATCH] [stability-bugs][automatrices] Improved xcg selection

---
 autoMatricesProtub/mainAutoMatProtub.m | 27 ++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/autoMatricesProtub/mainAutoMatProtub.m b/autoMatricesProtub/mainAutoMatProtub.m
index 86cc083a..ad85f773 100644
--- a/autoMatricesProtub/mainAutoMatProtub.m
+++ b/autoMatricesProtub/mainAutoMatProtub.m
@@ -37,17 +37,24 @@ end
 %% CHOICE OF NUMBER OF XCG
 timeXcgTotal = rocket.motor.time;
 xcgTotal = rocket.xCg;
+nXcg = autoMatSettings.vars.Nxcg;
 
-% Decisional figure
-h = figure('Name', 'XCG DECISION', 'NumberTitle', 'off');
-plot(timeXcgTotal, xcgTotal); grid on; hold on
-title('Weighted XCG'); xlabel('Time [s]'); ylabel('XCG [m]');
-chosenXcg = xcgTotal(floor(linspace(1, length(xcgTotal), autoMatSettings.vars.Nxcg))); % xcg where to create the matrices
-xcgTime = timeXcgTotal(floor(linspace(1, length(timeXcgTotal), autoMatSettings.vars.Nxcg))); % times where we create the matrices
-plot(xcgTime, chosenXcg, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4);
-plot(xcgTime, chosenXcg, 'b--');
-legend('Xcg variation', 'Xcg wanted', 'Linear interpolation');
-hold off
+if nXcg == 1
+    chosenXcg = xcgTotal(1);
+else
+    % Decisional figure
+    h = figure('Name', 'XCG DECISION', 'NumberTitle', 'off');
+    plot(timeXcgTotal, xcgTotal); grid on; hold on
+    title('Weighted XCG'); xlabel('Time [s]'); ylabel('XCG [m]');
+
+    xcgTime = linspace(timeXcgTotal(1), timeXcgTotal(end), nXcg);   % times where we create the matrices
+    chosenXcg = interp1(timeXcgTotal, xcgTotal, xcgTime);           % xcg where to create the matrices
+
+    plot(xcgTime, chosenXcg, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4);
+    plot(xcgTime, chosenXcg, 'b--');
+    legend('Xcg variation', 'Xcg wanted', 'Linear interpolation');
+    hold off
+end
 
 % Questlog
 answer = questdlg({strcat("In the figure you can see the red dot where the xcg variation will be discretized."),...
-- 
GitLab