Trying to plot a vertical line that stops at the intersection of a curved line in Matlab.
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Michael Thiel
le 26 Déc 2022
Commenté : Star Strider
le 27 Déc 2022
Here is my code that i used to plot my data and it works! On the plot, i have 2 red lines and a black curve. I need help figuring out how to get the red lines to stop at the intersection of the black curved line.
clc
close all
clearvars
phi =0:10:80;
intstep = (phi(2)-phi(1))/10; %interpolation step
phiint = phi(1):intstep:phi(end); %intepolated phi, to get a smoother curve
GZ = [0 0.3475 0.7315 1.2363 1.6167 1.6898 1.3167 0.8303 0.1829;
0 0.2195 0.4828 0.8705 1.1448 1.1302 0.6840 0.1426 -0.5377;
0 0.2304 0.5596 0.9949 1.1924 1.1046 0.7462 0.2743 -0.1829;
0 0.1034 0.3094 0.6291 0.7222 0.5442 0.1126 -0.4131 -0.9033];
KG = 10.97;
m_disp = 1.0e+04 *2.0118; %mass displacement
cargo = 1.0e+03 * [3.3 1.1 2.3059]; %weigth cargo
cg = [13.2 10.8 12.12]; %VCG cargo
GZ(5,:)=GZ(4,:)-0.597*cosd(phi);
GGprim=(1100*10.92)/m_disp(end);
leg_1 = {'-k'};
figure
for n=1:size(GZ(5,:),1)
GZint(n,:)=interp1(phi,GZ(5,:),phiint,'spline'); %interpolate to get a smoother curve
plot(phiint,GZint(n,:),leg_1{n})
hold on
leg_1{n} = strcat('KG = ', num2str(KG(n)), 'm \Delta = ', num2str(m_disp(n)) , ' MT');
end
%plot([0,60],[0,0],'linewidth',2)
%plot([57.4,57.4],[-0.6,-0.08068],'linewidth',2)
xline(57.3,'color','red','linewidth',2);
yline(0,'color','red','linewidth',2);
grid on
ylabel('GZ [m]','Interpreter','latex')
title('Figure 2b')
xlabel( ['Angle of Heel, ' '$\varphi\circ$'],'Interpreter','latex')
xlim([0 60]) %Hint if you graph doesn't fit within these axis something is wrong
ylim([-0.6 0.3])
legend(leg_1, 'location','northwest')
0 commentaires
Réponse acceptée
Star Strider
le 26 Déc 2022
Try something like this —
% clc
% close all
% clearvars
phi =0:10:80;
intstep = (phi(2)-phi(1))/10; %interpolation step
phiint = phi(1):intstep:phi(end); %intepolated phi, to get a smoother curve
GZ = [0 0.3475 0.7315 1.2363 1.6167 1.6898 1.3167 0.8303 0.1829;
0 0.2195 0.4828 0.8705 1.1448 1.1302 0.6840 0.1426 -0.5377;
0 0.2304 0.5596 0.9949 1.1924 1.1046 0.7462 0.2743 -0.1829;
0 0.1034 0.3094 0.6291 0.7222 0.5442 0.1126 -0.4131 -0.9033];
KG = 10.97;
m_disp = 1.0e+04 *2.0118; %mass displacement
cargo = 1.0e+03 * [3.3 1.1 2.3059]; %weigth cargo
cg = [13.2 10.8 12.12]; %VCG cargo
GZ(5,:)=GZ(4,:)-0.597*cosd(phi);
GGprim=(1100*10.92)/m_disp(end);
leg_1 = {'-k'};
figure
for n=1:size(GZ(5,:),1)
GZint(n,:)=interp1(phi,GZ(5,:),phiint,'spline'); %interpolate to get a smoother curve
plot(phiint,GZint(n,:),leg_1{n})
hold on
leg_1{n} = strcat('KG = ', num2str(KG(n)), 'm \Delta = ', num2str(m_disp(n)) , ' MT');
end
%plot([0,60],[0,0],'linewidth',2)
%plot([57.4,57.4],[-0.6,-0.08068],'linewidth',2)
% xline(57.3,'color','red','linewidth',2);
% yline(0,'color','red','linewidth',2);
ixx = find(phiint>57.3,1,'first')+(-1:1).'; % Index Range For Vertical Line
ixy = find(diff(sign(GZint)))+(-1:1).'; % Index Range For Horizontal Line
y1 = interp1(phiint(ixx),GZint(ixx),57.3); % Y-Limit Of Vertical Line
x1 = interp1(GZint(ixy(:,2)),phiint(ixy),0); % X-Limit Of Horizontal Line
plot([1 1]*57.3, [min(ylim) y1], '-r', 'LineWidth',2) % Plot Vertical Line
plot([min(xlim) x1(2)], [0 0], '-r', 'LineWidth',2) % Plot Horizontal Line
grid on
ylabel('GZ [m]','Interpreter','latex')
title('Figure 2b')
xlabel( ['Angle of Heel, ' '$\varphi\circ$'],'Interpreter','latex')
xlim([0 60]) %Hint if you graph doesn't fit within these axis something is wrong
ylim([-0.6 0.3])
legend(leg_1, 'location','northwest')
I’m not certain which intersection you want for the horizontal line. This extends it to the second intersection, to stop at the first intersection change the ‘x1’ subscript to 1 in the plot call using it (’Plot Horizontal Line’).
.
4 commentaires
Star Strider
le 27 Déc 2022
That has to do more with the plot function and the chosen line width than the precision of the calculations. One option could be to ‘fudge’ ‘x1(1)’ by multiplying it by a small constant —
% clc
% close all
% clearvars
phi =0:10:80;
intstep = (phi(2)-phi(1))/10; %interpolation step
phiint = phi(1):intstep:phi(end); %intepolated phi, to get a smoother curve
GZ = [0 0.3475 0.7315 1.2363 1.6167 1.6898 1.3167 0.8303 0.1829;
0 0.2195 0.4828 0.8705 1.1448 1.1302 0.6840 0.1426 -0.5377;
0 0.2304 0.5596 0.9949 1.1924 1.1046 0.7462 0.2743 -0.1829;
0 0.1034 0.3094 0.6291 0.7222 0.5442 0.1126 -0.4131 -0.9033];
KG = 10.97;
m_disp = 1.0e+04 *2.0118; %mass displacement
cargo = 1.0e+03 * [3.3 1.1 2.3059]; %weigth cargo
cg = [13.2 10.8 12.12]; %VCG cargo
GZ(5,:)=GZ(4,:)-0.597*cosd(phi);
GGprim=(1100*10.92)/m_disp(end);
leg_1 = {'-k'};
figure
for n=1:size(GZ(5,:),1)
GZint(n,:)=interp1(phi,GZ(5,:),phiint,'spline'); %interpolate to get a smoother curve
plot(phiint,GZint(n,:),leg_1{n})
hold on
leg_1{n} = strcat('KG = ', num2str(KG(n)), 'm \Delta = ', num2str(m_disp(n)) , ' MT');
end
%plot([0,60],[0,0],'linewidth',2)
%plot([57.4,57.4],[-0.6,-0.08068],'linewidth',2)
% xline(57.3,'color','red','linewidth',2);
% yline(0,'color','red','linewidth',2);
ixx = find(phiint>57.3,1,'first')+(-1:1).'; % Index Range For Vertical Line
ixy = find(diff(sign(GZint)))+(-1:1).'; % Index Range For Horizontal Line
y1 = interp1(phiint(ixx),GZint(ixx),57.3); % Y-Limit Of Vertical Line
x1 = interp1(GZint(ixy(:,2)),phiint(ixy),0); % X-Limit Of Horizontal Line
plot([1 1]*57.3, [min(ylim) y1], '-r', 'LineWidth',2) % Plot Vertical Line
plot([x1(1)*1.02 x1(2)], [0 0], '-r', 'LineWidth',2) % Plot Horizontal Line
dx1 = diff(x1) % Difference Between 'x1(1)' And 'x1(2)'
grid on
ylabel('GZ [m]','Interpreter','latex')
title('Figure 2b')
xlabel( ['Angle of Heel, ' '$\varphi\circ$'],'Interpreter','latex')
xlim([0 60]) %Hint if you graph doesn't fit within these axis something is wrong
ylim([-0.6 0.3])
legend(leg_1, 'location','northwest')
I chose the constant (1.02) arbitrarily, and it appears to work. Change it to give the result you want.
.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



