Extracting Data from Figures and Vectors

Dear all,
I have the following code in which I want to extracxt y values (displacements) for 12ft, 24ft and 36ft along a 48ft beam.
clear, clc, close all
EI = 29000*13400; % Flexural rigidity
L = 48; % Length of Beam
P = 1; % Loading
R1y = 0.75; % Support reaction at support 1
R2y = 0.25; % Support reaction at support 2
x1 = 0:0.1:12; % X values for 0 < x < 12
dx1 = x1(2) - x1(1); % Step size
x2 = 12:0.1:48; % X values for 0 < x < 12
Mx1 = R1y*x1/(EI); % Moment function for x1
dx2 = x2(2) - x2(1); % Step size
Mx2 = (R1y*x2 - P*(x2-12))/(EI); % Moment function for x2
y1(1) = 0; % Initial condition for y1
b = 36;
z1(1) = P*b*(L^2 - b^2)/(6*L*EI); % Initial condition for z1 (slope)
% Compute displacement for 0 < x1 < 12
for i = 1:length(x1)-1
y1(i+1) = y1(i) + z1(i)*dx1;
z1(i+1) = z1(i) - Mx1(i)*dx1;
end
y2(1) = y1(end) % Initial condition for y2
z2(1) = z1(end) % Initial condition for z2 (slope)
% Compute displacement for 12 < x2 < 48
for i = 1:length(x2)-1
y2(i+1) = y2(i) + z2(i)*dx2;
z2(i+1) = z2(i) - Mx2(i)*dx2;
end
% Plot results
plot(x1,y1,x2,y2)
grid on
My y1 vector is 121 interations long and my y2 vector is 361 iterations long. How do I extract from the data the values at 12ft, 24ft and 36ft of the beam in the most efficient way? If for example I put y1(12) I do not get the value at 12ft along the 48ft beam, because I have made a step size 0.1 and not 1.
Many thanks in advance,
Scott

 Respuesta aceptada

The easiest way is to use the interp1 function —
clear, clc, close all
EI = 29000*13400; % Flexural rigidity
L = 48; % Length of Beam
P = 1; % Loading
R1y = 0.75; % Support reaction at support 1
R2y = 0.25; % Support reaction at support 2
x1 = 0:0.1:12; % X values for 0 < x < 12
dx1 = x1(2) - x1(1); % Step size
x2 = 12:0.1:48; % X values for 0 < x < 12
Mx1 = R1y*x1/(EI); % Moment function for x1
dx2 = x2(2) - x2(1); % Step size
Mx2 = (R1y*x2 - P*(x2-12))/(EI); % Moment function for x2
y1(1) = 0; % Initial condition for y1
b = 36;
z1(1) = P*b*(L^2 - b^2)/(6*L*EI); % Initial condition for z1 (slope)
% Compute displacement for 0 < x1 < 12
for i = 1:length(x1)-1
y1(i+1) = y1(i) + z1(i)*dx1;
z1(i+1) = z1(i) - Mx1(i)*dx1;
end
y2(1) = y1(end) % Initial condition for y2
y2 = 3.3489e-06
z2(1) = z1(end) % Initial condition for z2 (slope)
z2 = 1.8644e-07
% Compute displacement for 12 < x2 < 48
for i = 1:length(x2)-1
y2(i+1) = y2(i) + z2(i)*dx2;
z2(i+1) = z2(i) - Mx2(i)*dx2;
end
x2_vals = [12 24 36]; % Independent Variable Values
y2_vals = interp1(x2, y2, x2_vals) % Interpolated Dependent Variable Values
y2_vals = 1×3
1.0e-05 * 0.3349 0.4113 0.2645
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Plot results
plot(x1,y1,x2,y2)
hold on
plot(x2_vals, y2_vals, 'rs')
hold off
grid on
.

6 comentarios

Scott Banks
Scott Banks el 22 de En. de 2025
Briliant! Thank you, Star Strider.
Star Strider
Star Strider el 22 de En. de 2025
As always, my pleasure!
Hi Star Strider, I have come into a bit of difficulty with the Interp1 function.
I have the two vectors for displacement (y1 and y2) and I have combined them into one vector called "Y".
clear, clc, close all
EI = 29000*13400; % Flexural rigidity
L = 48; % Length of Beam
P = 1; % Loading
R1y = 0.75; % Support reaction at support 1
R2y = 0.25; % Support reaction at support 2
x1 = 0:0.1:12; % X values for 0 < x < 12
dx1 = x1(2) - x1(1); % Step size
x2 = 12:0.1:48; % X values for 0 < x < 12
Mx1 = R1y*x1/(EI); % Moment function for x1
dx2 = x2(2) - x2(1); % Step size
Mx2 = (R1y*x2 - P*(x2-12))/(EI); % Moment function for x2
y1(1) = 0; % Initial condition for y1
b = 36;
z1(1) = P*b*(L^2 - b^2)/(6*L*EI); % Initial condition for z1 (slope)
% Compute displacement for 0 < x1 < 12
for i = 1:length(x1)-1
y1(i+1) = y1(i) + z1(i)*dx1;
z1(i+1) = z1(i) - Mx1(i)*dx1;
end
y2(1) = y1(end); % Initial condition for y2
z2(1) = z1(end); % Initial condition for z2 (slope)
% Compute displacement for 12 < x2 < 48
for i = 1:length(x2)-1
y2(i+1) = y2(i) + z2(i)*dx2;
z2(i+1) = z2(i) - Mx2(i)*dx2;
end
X = [x1 x2];
Y = [y1 y2];
X_vals = [12,24,36];
Y_vals = interp1(X,Y,X_vals)
I keep getting an error message for the Interp1 function and I don't know why!
Apparently, you have duplicated values in ‘X’.
Using unique seems to have solved it, although you need to check those results to be sure.
Try this —
clear, clc, close all
EI = 29000*13400; % Flexural rigidity
L = 48; % Length of Beam
P = 1; % Loading
R1y = 0.75; % Support reaction at support 1
R2y = 0.25; % Support reaction at support 2
x1 = 0:0.1:12; % X values for 0 < x < 12
dx1 = x1(2) - x1(1); % Step size
x2 = 12:0.1:48; % X values for 0 < x < 12
Mx1 = R1y*x1/(EI); % Moment function for x1
dx2 = x2(2) - x2(1); % Step size
Mx2 = (R1y*x2 - P*(x2-12))/(EI); % Moment function for x2
y1(1) = 0; % Initial condition for y1
b = 36;
z1(1) = P*b*(L^2 - b^2)/(6*L*EI); % Initial condition for z1 (slope)
% Compute displacement for 0 < x1 < 12
for i = 1:length(x1)-1
y1(i+1) = y1(i) + z1(i)*dx1;
z1(i+1) = z1(i) - Mx1(i)*dx1;
end
y2(1) = y1(end); % Initial condition for y2
z2(1) = z1(end); % Initial condition for z2 (slope)
% Compute displacement for 12 < x2 < 48
for i = 1:length(x2)-1
y2(i+1) = y2(i) + z2(i)*dx2;
z2(i+1) = z2(i) - Mx2(i)*dx2;
end
X = [x1 x2];
Y = [y1 y2];
[Xu,ix] = unique(X);
Yu = Y(ix);
X_vals = [12,24,36];
Y_vals = interp1(Xu,Yu,X_vals)
Y_vals = 1×3
1.0e-05 * 0.3349 0.4113 0.2645
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Results = table(X_vals(:), Y_vals(:), VariableNames=["X Vals", "Y Vals"])
Results = 3x2 table
X Vals Y Vals ______ __________ 12 3.3489e-06 24 4.1132e-06 36 2.6449e-06
.
Scott Banks
Scott Banks el 22 de En. de 2025
That's fine. Thanks for that, Star Strider.
Star Strider
Star Strider el 23 de En. de 2025
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Centro de ayuda y File Exchange.

Preguntada:

el 22 de En. de 2025

Comentada:

el 23 de En. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by