I need to create a 3d plot. I have created a 5x24 matrix called Mneg that contains all the info needed.I need to plot the 3rd row(as z) vs the 1st (as x) and 2nd row (as y). How can I do this? Thanks in advance! Cheers

1 view (last 30 days)
Beam.sigmaZ = 731;% um
Beam.espread = 0.2; % percent
Beam.energy = 0.116; % GeV
N = 10000;
Z = Beam.sigmaZ * randn(N,1);
E = Beam.energy * (1 + Beam.espread * randn(N,1) / 100);
f_S=3e+9; % Hz
lamda_S = (3e+8)/f_S;% m
kRF_S = 2*pi/lamda_S;% 1/m
kRF_X=4*kRF_S;
L=[];
E0=0.20;
phi_S=[10:10:89];
phi_X= [120:10:199];
for phi=phi_S(1):(phi_S(2)-phi_S(1)):phi_S(length(phi_S))
eV_S = (E0-Beam.energy)/cos(phi);
R65_S = -(kRF_S*eV_S*sin(phi))/E0;
for phix=phi_X(1):(phi_X(2)-phi_X(1)):phi_X(length(phi_X))
eV_X = -eV_S*cos(phi)/16*cos(phix);
R65_X = -(kRF_X*eV_X*sin(phix))/E0;
h=R65_S+R65_X;
R56= -1/h;
L=[L,[phi;phix;R56;eV_S;eV_X]];
end
end
[r,c]=find(L(3,:) < 0);
Mneg=L(:,c);
  2 Comments
Tasos Charitonidis
Tasos Charitonidis on 5 Jul 2014
Thanks for the quick response. I was also thinking of asking for an easy way to generate the same plot having E0 as parameter. So if we change E0 to E0=0.2:0.05:0.5; we 'd have to have a statement running from i:length(E0)and then plot Z vs X,Y for each energy in the same 3dplot, in different colors..
Tasos Charitonidis
Tasos Charitonidis on 5 Jul 2014
Thanks for the quick response. I was also thinking of asking for an easy way to generate the same plot having E0 as parameter. So if we change E0 to E0=0.2:0.05:0.5; we 'd have to have a statement running from i:length(E0)and then plot Z vs X,Y for each energy in the same 3dplot, in different colors..

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 5 Jul 2014
The plot3 function will do what you want:
figure(1)
plot3(Mneg(1,:), Mneg(2,:), Mneg(3,:))
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
  6 Comments
Tasos Charitonidis
Tasos Charitonidis on 11 Jul 2014
Edited: Tasos Charitonidis on 11 Jul 2014
May I address the following:1)I am facing problems in plotting for different energies(again).I'd appreciate it if you could run it & have a look. 2)Having some minor modifications for eV_S,eV_X,R65,R56 I am trying to 3dplot.How would it be possible to get a surface like meshgrid?
Beam.sigmaZ = 731;% um
Beam.espread = 0.2; % percent
Beam.energy = 0.116; % GeV
N = 10000; Z = Beam.sigmaZ * randn(N,1);
E = Beam.energy * (1 + Beam.espread * randn(N,1) / 100);
f_S=3e+9; % Hz
lamda_S = (3e+8)/f_S;% m
kRF_S = 2*pi/lamda_S;% 1/m
kRF_X=4*kRF_S;
L=[];
% E0=0.20;
phi_S=[10:10:89];
phi_X= [120:10:199];
E0=0.25:0.15:0.5;
for k1 = 1:length(E0)
for phi=phi_S(1):(phi_S(2)-phi_S(1)):phi_S(length(phi_S))
phi_rad = phi*pi/180.0;
energy_gain = (E0(k1)-Beam.energy);
eV_S = 16/15 * energy_gain / cos(phi_rad);
for phix=phi_X(1):(phi_X(2)-phi_X(1)):phi_X(length(phi_X))
phix_rad = phix*pi/180.0;
eV_X = -1/15 * energy_gain/cos(phix_rad);
%eV_X = -eV_S*cos(phi_rad)/16*cos(phix_rad);
R65 = kRF_S*eV_S*(sin(phi_rad)-cos(phi_rad)*tan(phix_rad))/4*E0(k1);
R56= -1/R65;
L=[L,[phi;phix;R56;eV_S;eV_X]];
end end [r,c]=find(L(3,:) < 0);
Mneg=L(:,c);
MnegPlt(:,:,k1) = Mneg(1:3,(1:length(Mneg)+(k1-1)*length(Mneg)));
end
legstr{1} = sprintf('E0 = %.2f',E0(1));
cv = ['rgcmyk'] figure(1) plot3(MnegPlt(1,:,1), MnegPlt(2,:,1), MnegPlt(3,:,1))
hold on
for k1 = 2:length(E0)
plot3(MnegPlt(1,:,k1), MnegPlt(2,:,k1), MnegPlt(3,:,k1), cv(k1-1))
legstr{k1} = sprintf('E0 = %.2f',E0(k1));
end
hold off
grid on
legend(legstr)
xlabel('X')
ylabel('Y')
zlabel('Z')
Star Strider
Star Strider on 11 Jul 2014
You changed my code and I couldn’t get it to run without some significant repairs.
This works:
Beam.sigmaZ = 731;% um
Beam.espread = 0.2; % percent
Beam.energy = 0.116; % GeV
N = 10000;
Z = Beam.sigmaZ * randn(N,1);
E = Beam.energy * (1 + Beam.espread * randn(N,1) / 100);
f_S=3e+9; % Hz
lamda_S = (3e+8)/f_S;% m
kRF_S = 2*pi/lamda_S;% 1/m
kRF_X=4*kRF_S;
L=[];
% E0=0.20;
phi_S=[10:10:89];
phi_X= [120:10:199];
E0=0.25:0.15:0.5;
for k1 = 1:length(E0)
for phi=phi_S(1):(phi_S(2)-phi_S(1)):phi_S(length(phi_S))
phi_rad = phi*pi/180.0;
energy_gain = (E0(k1)-Beam.energy);
eV_S = 16/15 * energy_gain / cos(phi_rad);
for phix=phi_X(1):(phi_X(2)-phi_X(1)):phi_X(length(phi_X))
phix_rad = phix*pi/180.0;
eV_X = -1/15 * energy_gain/cos(phix_rad);
%eV_X = -eV_S*cos(phi_rad)/16*cos(phix_rad);
R65 = kRF_S*eV_S*(sin(phi_rad)-cos(phi_rad)*tan(phix_rad))/4*E0(k1);
R56= -1/R65;
L=[L,[phi;phix;R56;eV_S;eV_X]];
end
end
[r,c]=find(L(3,:) < 0);
Mneg=L(:,c);
LMneg(k1) = size(Mneg,2);
MnegPlt(:,:,k1) = Mneg(1:3,(1:LMneg(1))+(k1-1)*LMneg(1));
end
legstr{1} = sprintf('E0 = %.2f',E0(1));
cv = ['rgcmyk']
figure(1)
plot3(MnegPlt(1,:,1), MnegPlt(2,:,1), MnegPlt(3,:,1))
hold on
for k1 = 2:length(E0)
plot3(MnegPlt(1,:,k1), MnegPlt(2,:,k1), MnegPlt(3,:,k1), cv(k1-1))
legstr{k1} = sprintf('E0 = %.2f',E0(k1));
end
hold off
grid on
legend(legstr)
xlabel('X')
ylabel('Y')
zlabel('Z')
It might be possible to plot your curves as surfaces using interp2 but not until you eliminate the connecting lines that create a ‘zig-zag’ effect in your code. The easiest way to do that might be to insert a ‘NaN’ vector at the end of each iteration of MNeg or L and then separate the data for various values of E0 to create two surfaces. I’ll think about it and work on it when I have time, since I have to understand how your code works and how it generates L and Mneg. Since you already know how your code works, explore these possibilities on your own in the meantime.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!