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

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)

 Respuesta aceptada

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 comentarios

Nice, thanks a lot.
How about something like this
P = [-1000,-5000,-10000,-50000,-100000];
b = [0;500;1000;1500;2000];
c = [0,0.5,1,1.5,2];
b(4) = P;
Unable to perform assignment because the left and right sides have a different number of elements.
plot(c,b)
Please explain what you are trying to do with the above code.
I have matrix equations like Ax = b and b is a 21*1 zeros but I must change the value in b(7) to be -1000,-5000,-10000,-50000,-100000 and solve the equations, so I kinda want to have all the solutions to be plotted in one plot.
If you did not understand what I mean I can copy the full code here.
Yes, please copy and paste the full code here.
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.
That's good to know.
You are welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 9 de Ag. de 2023

Comentada:

el 11 de Ag. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by