Plot a for-loop with if-statements inside
Afficher commentaires plus anciens
I have written three different functions for three different intervals on the variable r. I wrote the following code:
a = 1;
b = 2;
pv = 5*10^(-3);
Q = pv*((4*pi*b^3)/3) - pv*((4*pi*a^3)/3);
epsilon = 8.8541878176*10^(-12);
r = 0:0.01:10
hold on
for r = 0:0.01:10
if r<a
E = -(Q/(4*pi*epsilon*r^2));
elseif r>b
E = (Q/(4*pi*epsilon*r^2));
else a<r<b
E = (pv*r)/(3*epsilon) - (pv*a^3)/(3*r^2*epsilon);
end
plot(E,r,'r');
end
hold off
Now, this code does not yield a visable plot, it's just empy. I figured maybe I need to store E of every iteration of the loop in a matrix, but have found no way of solving it.
Réponse acceptée
Plus de réponses (1)
In this case you don't even need a loop:
a = 1;
b = 2;
pv = 5*10^(-3);
Q = pv*((4*pi*b^3)/3) - pv*((4*pi*a^3)/3);
epsilon = 8.8541878176*10^(-12);
r = 0:0.01:10;
E=NaN(size(r));
L1=r<a;
E(L1)=-(Q./(4*pi*epsilon*r(L1).^2));
L2=r>b;
E(L2)= (Q./(4*pi*epsilon*r(L2).^2));
L3=~(L1&L2);
E(L3)=(pv*r(L3))/(3*epsilon) - (pv*a^3)./(3*r(L3).^2*epsilon);
plot(E,r,'r');xlim([-20e11 2e11])
Catégories
En savoir plus sur Waveform Generation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


