Is there a way for reducing my code lines in a code for ploting
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
mahdi
am 9 Aug. 2023
Kommentiert: Dyuman Joshi
am 11 Aug. 2023
So I have this code for plotting and I wanna know that if there is a quicker way or a code with lesser lines like with a for loop or something can do the same thing for me.
And also there is a warning message that comes up and it doesnt show all my legend entries and I dont know why. (Warning: Ignoring extra legend entries.)
Es = 200*10^9; % Young's modulus of the solid of the steel(Pa)
Beta = 0;
h = 0.02; % Thickness(m)
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
xlabel('z')
ylabel('E(z)')
title('Porousity factor: Beta')
legend('0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1')
hold on
Beta = 0.1;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.2;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.3;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.4;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.5;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.6;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.7;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.8;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 0.9;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
Beta = 1;
e1 = 1-((1-Beta).^2);
z = -h/2:0.001:h/2;
Ez = (Es*(1-(e1.*(cos((pi*z)/h)))));
plot(z,Ez)
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 9 Aug. 2023
Bearbeitet: Dyuman Joshi
am 9 Aug. 2023
Store your data in array and use indices to access the data -
Es = 200*10^9; % Young's modulus of the solid of the steel(Pa)
h = 0.02; % Thickness(m)
%Define Beta as an array
Beta = (0:10)/10;
%Corresponding value of e
e = 1-((1-Beta).^2);
%As z does not depend on the loop index, bring it out of the loop
%This avoids calculating z over and over again.
z = -h/2:0.001:h/2;
figure()
for k=1:numel(Beta)
Ez = (Es*(1-(e(k).*(cos((pi*z)/h)))));
plot(z,Ez)
hold on
end
xlabel('z')
ylabel('E(z)')
title('Porousity factor: Beta')
legend('0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1','location','best')
As you learn things about MATLAB, you will find that vectorization is a powerful tool, as MATLAB is optimized for operations involving arrays. The above for loop can be vectorized as well -
e0 = e';
%What you will get below is an array, where each column will be
%Ez value calculated for corresonding value in beta
Ez0 = (Es*(1-(e0*(cos((pi*z)/h)))));
%Make a new figure to plot and observe the results
figure()
%When first input is a vector, and second input is a non-vector array
%plot() will plot each curve corresponding to each column of the second input
plot(z,Ez0)
As for the warning - You have plotted only 1 curve before using legend(), but you have included more than 1 names, so the legend() ignores the rest of the names, which is what the warning says as well.
7 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!