Is there a way for reducing my code lines in a code for ploting
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Dyuman Joshi
2023년 8월 9일
편집: Dyuman Joshi
2023년 8월 9일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!