Is there a way for reducing my code lines in a code for ploting

1 回表示 (過去 30 日間)
mahdi
mahdi 2023 年 8 月 9 日
コメント済み: Dyuman Joshi 2023 年 8 月 11 日
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')
Warning: Ignoring extra legend entries.
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)

採用された回答

Dyuman Joshi
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 件のコメント
mahdi
mahdi 2023 年 8 月 10 日
編集済み: mahdi 2023 年 8 月 10 日
I figured my last comment's answer too and here it is:
P = [-1000,-5000,-10000,-50000,-100000];
b = [0;500;1000;1500;2000];
c = [0,0.5,1,1.5,2];
figure(1)
for l=1:numel(P)
b(4)=P(l);
plot(c,b)
hold on
end
Again thanks for your help.
Dyuman Joshi
Dyuman Joshi 2023 年 8 月 11 日
That's good to know.
You are welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by