Why are my line plots are coming out improperly?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
The plot lines generated from the code below are coming out in improper way. I have attached an image file of the plot as well. The line generated from h4 and h8 are coming in an odd manner. Can I know why is it coming out in such a way?
clc;clear;close all;
%for head on collision
L= 4;
D= 3.860;
lam= 0.397;
% lamda will go from 0.4 m/sec (water, 0.25, 0.5, 0.75, 1) then 1m/sec
% (same thing) and finally 1.65 m/sec (same thing)
lam_org= [0.394 0.2091 0.284 0.404 0.374 0.423 0.358 0.523 0.493 0.567 0.359 0.424 0.296 0.445 0.447];
%lam_15_times = [0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397 0.397];
lam_l = log(lam_org);
psi_e= (2^0.33)/(2-lam);
Oh= 10^(-3)*[1.679 2.457 2.679 2.922 3.398 1.597 2.1782 2.505 2.958 1.487 3.129 1.934 2.478 2.728 2.893];
md_ho = [1.0690 2.02 1.3474 1.3580 1.3966 1.6701 1.4901 1.669 1.476 2.848 1.3871 2.8008 3.0724 2.3614 2.7726];
L_o = log(md_ho);
We = [8.518 9.903 16.473 22.68 26.214 58.84 78.77 117.76 138.37 184.842 193.155 272.054 327.49 443.05 615.34];
We_log = log(We);
%for 2mm offset
md_2= [1.0431 1.1251 0.9519 0.9691 0.9380 1.5003 1.2390 1.3260 1.2584 2.2003 1.2260 1.9754 2.125 1.6842 2.098];
L_2 = log(md_2);
We= [8.518 9.903 16.473 22.68 26.214 58.84 78.77 117.76 138.37 184.842 193.155 272.054 327.49 443.05 615.34];
%for 3mm offset
md_3= [0.9819 0.9916 1.0444 0.9938 0.9807 1.3555 1.2052 1.227 1.1849 1.747 1.2106 1.5462 1.6876 1.5860 1.726];
L_3 = log(md_3);
We= [8.518 9.903 16.473 22.68 26.214 58.84 78.77 117.76 138.37 184.842 193.155 272.054 327.49 443.05 615.34];
%for 4mm offset
md_4= [1.015 0.9875 1.0682 1.0763 1.0021 1.295 1.119 1.1112 1.125 1.681 1.1667 1.6932 1.862 1.677 1.6443 ];
L_4 = log(md_4);
We= [8.518 9.903 16.473 22.68 26.214 58.84 78.77 117.76 138.37 184.842 193.155 272.054 327.49 443.05 615.34];
for i= 1:length(We)
psi_m(i)= 0.9759*((1+L/D)^(-0.7594))*We(i).^0.2283; % Graham et al.
end
for j=1:length(We)
psi2_m(j)= (D+L)*We(j).^0.25; % Clanet et al.
end
for k= 1:length(We)
for l= 1:length(Oh)
psi_m3(k,l)= 0.8353*(psi_e^0.3774)*(We(k)^0.0571)*(Oh(l)^(-0.0430)); % Li et al.
end
end
for m= 1: length(We)
for n= 1: length(lam_org)
psi_m_4(m,n)= 0.6046*(We(m)^0.1453)*(lam_org(n)^(-0.1062)); % my correlation
end
end
%h1= plot(We,md_ho,'s','MarkerFaceColor','red','MarkerEdgeColor','black','MarkerSize',9); hold on
%h5= plot(We,md_2,'o','MarkerFaceColor','red','MarkerEdgeColor','black','MarkerSize',9); hold on
%h6= plot(We,md_3,'<','MarkerFaceColor','green','MarkerEdgeColor','black','MarkerSize',9); hold on
h7= plot(We,md_4,'d','MarkerFaceColor','red','MarkerEdgeColor','black','MarkerSize',9); hold on
pcoeff= polyfit(We,md_4,2)
%pcoeff2= polyfit(We,md_ho,2)
%axis([0 625 0 0.5]);
yp= polyval(pcoeff,We);
%yp2= polyval(pcoeff2,We);
h2= plot(We,yp,'k','LineWidth',1.2); hold on
h3= plot(We,psi_m, 'b','LineWidth', 1.5); hold on % (psi_m is the Graham-Farhangi's correlated maximum deformation)
%plot(We,psi2_m,'LineStyle','--','Color','g','LineWidth',1.5); %(Clanet et al's correlation)
h4= plot(We,psi_m3,'LineStyle','-.','Color','#0B0'); hold on %(Li et al's correlation)
h8= plot(We,psi_m_4,'LineStyle','-','LineWidth',1.2,'Color','magenta');
%p = polyfit(We,md_ho, 1);
%px = [min(We) max(We)];
%py = polyval(p, px);
%hold on
%plot(px, py, 'LineWidth', 2);
legend([h7(1), h2(1), h3(1),h4(1), h8(1)], 'Experimental data for \lambda = 0.397 (4mm offset)','Fitted curve', 'Graham et al.','Li et al.', 'my correlation');
xlabel('We','FontSize',14,'FontWeight','normal');ylabel('\psi_{max}','FontSize',16,'FontWeight','bold');

9 commentaires
Réponse acceptée
dpb
le 18 Avr 2023
Déplacé(e) : dpb
le 18 Avr 2023
Well, there's no "problem" with the nested loop other than apparently it isn't what you intended... :) It iterates through each value of the Oh or lam_org for each We in turn.
If the intent is instead to use the i_th value of each We and Oh at the same time such that both are changing simultaneously but the combinations of I,J for I~=J aren't computed, then you don't need (or want) the nested loop but just a single loop -- or, in MATLAB don't need the loop at all;, just write the vectorized expressions.
...
for k= 1:length(We)
psi_m3(k)= 0.8353*(psi_e^0.3774)*(We(k)^0.0571)*(Oh(k)^(-0.0430)); % Li et al.
psi_m_4(k)= 0.6046*(We(k)^0.1453)*(lam_org(k)^(-0.1062)); % my correlation
end
where I've moved them both into a single loop since iterating over same thing for both.
"The MATLAB way" to do the above is to use the "dot" operators and vectorize the expressions...
psi_m3 = 0.8353*(psi_e^0.3774).*(We.^0.0571).*(Oh.^(-0.0430)); % Li et al.
psi_m_4= 0.6046*(We.^0.1453).*lam_org.^(-0.1062); % my correlation
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements 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!