I want to plot many points in a single pointcare sphere.
    8 次查看(过去 30 天)
  
       显示 更早的评论
    
This is the complete code i'm using.
close all
%%
X=linspace(-5,5,500);
Y=X; N=500;
[x,y]=meshgrid(X,Y);
%%
r = (x.^2 + y.^2); rho = sqrt(r);
w0 = 3 ;     
l = 500*10^-9; 
GB = exp(-(r)./w0^2);
%%
ExI = GB.*1 ;
EyI = GB.*0 ;
%%
    pi180 = pi/180 ;             
    EPhase = 0 ;   EPhaseD = EPhase*pi180 ;         
    Phi_d = 45*pi/180 ;
    a1 = exp(1i*Phi_d) ;  
    b1 = exp(1i*EPhaseD) ;   b2 = exp(-1i*EPhaseD) ;
    for Theta_ERD = 0*pi180:40*pi180:180*pi180
    S_Theta_ER = sin(Theta_ERD) ; C_Theta_ER = cos(Theta_ERD);
    S2_Theta_ER = S_Theta_ER^2 ; C2_Theta_ER = C_Theta_ER^2;
    ER11 = C2_Theta_ER + a1*S2_Theta_ER ;
    ER12 = (1-a1)*S_Theta_ER*C_Theta_ER*b2 ;
    ER21 = (1-a1)*S_Theta_ER*C_Theta_ER*b1 ;
    ER22 = S2_Theta_ER + a1*C2_Theta_ER ;
    Ex = (ER11.*ExI + ER12.*EyI) ;
    Ey = (ER21.*ExI + ER22.*EyI) ;
%%
S0=(conj(Ex).*Ex)+(conj(Ey).*Ey);
S1=(conj(Ex).*Ex)-(conj(Ey).*Ey);
S2=2.*real(conj(Ex).*Ey);
S3=2.*imag(conj(Ex).*Ey);
threshold = .01;
ns0=sqrt((S1.^2)+(S2.^2)+(S3.^2));
ns1=zeros(size(ns0,1),size(ns0,2));
ns2=zeros(size(ns0,1),size(ns0,2));
ns3=zeros(size(ns0,1),size(ns0,2));
ss=zeros(size(ns0,1),size(ns0,2));
for m = 1:size(ns0,1)
    for n = 1:size(ns0,2)
        if ns0(m,n)> threshold
            ns3(m,n) = ns3(m,n)+(S3(m,n)/ns0(m,n));
            ns1(m,n) = ns1(m,n)+(S1(m,n)/ns0(m,n));
            ns2(m,n) = ns2(m,n)+(S2(m,n)/ns0(m,n));                           
        end
    end
end
%% Stokes parameters
f= 3.5;
avgfact=2^f; %averaging factor
s0=imresize(ns0,1/avgfact);
s1=imresize(ns1,1/avgfact);
s2=imresize(ns2,1/avgfact);
s3=imresize(ns3,1/avgfact);
threshold=.1;
k=1;
for i=1:size(s0,1)
    for j=1:size(s0,2)
        if s0(i,j)>threshold
            stk(k,1)=s1(i,j);
            stk(k,2)=s2(i,j);
            stk(k,3)=s3(i,j);
            stk(k,4)=sqrt(s1(i,j)^2+s2(i,j)^2+s3(i,j)^2);
            k=k+1;
        end
    end
end
H=figure;
[X,Y,Z] = sphere(100);
axis square
box on
r=.999;
surf(r*X,r*Y,r*Z,'EdgeColor','none','FaceColor',[.6 .6 .6],'FaceAlpha', 0.8);
line([-1.3 1.3],[0 0],[0 0],'LineStyle','-','LineWidth',2,'Color',[0 0 0])
line([0 0],[-1.3 1.3],[0 0],'LineStyle','-','LineWidth',2,'Color',[0 0 0])
line([0 0],[0 0],[-1.3 1.3],'LineStyle','-','LineWidth',2,'Color',[0 0 0])
text(1.3,-.1,-.1,'S1','FontSize',15);text(0,1.3,0,'S2','FontSize',15);text(0,0,1.3,'S3','FontSize',15)
axis([-1 1 -1 1 -1 1])
view(122,53)
hold all
phi=-pi:.01:pi;
theta=0;
r3=.315;
r2=.72;
r1=.94;
r0=1;
sx=cos(phi);
sy=sin(phi);
sz=cos(phi.*0-pi/2);
plot3(sx,sy,sz,'k','LineWidth',1.5);
plot3(sx,sz,sy,'k','LineWidth',1.5);
plot3(sz,sy,sx,'k','LineWidth',1.5);
[p q]=size(stk);
for i=1:p
    H=plot3(stk(i,1),stk(i,2),stk(i,3),'mo');
    hold all
    set(H,'markersize',10,'markeredgecolor','g','markerfacecolor','y','color','m','linewidth',1.2)
end
    end    
Now all the points are ploted seperatly in individual pointcare sphere. I want all points to be plotted in a single sphere. And if possible joint the points with a line.
Any help is appriciated.
0 个评论
采纳的回答
  Subhajyoti
 2024-11-30
        In the given code snippet, 'H=figure' is declared within the loop which is restricting the scope of the figure within the iteration. Hence, for every iteration, a new figure is create, resulting in separate figures.
You can declare the figure outside the loop, which will extend it's scope to the entire code, thus retain the current plots when adding new plots. Here, in the following implementation, I have adjusted the same to get the desired output.
X=linspace(-5,5,500);
Y=X; N=500;
[x,y]=meshgrid(X,Y);
r = (x.^2 + y.^2); rho = sqrt(r);
w0 = 3 ;     
l = 500*10^-9; 
GB = exp(-(r)./w0^2);
ExI = GB.*1 ;
EyI = GB.*0 ;
pi180 = pi/180 ;             
EPhase = 0 ;   EPhaseD = EPhase*pi180 ;         
Phi_d = 45*pi/180 ;
a1 = exp(1i*Phi_d) ;  
b1 = exp(1i*EPhaseD) ;   b2 = exp(-1i*EPhaseD) ;
H=figure;
for Theta_ERD = 0*pi180:40*pi180:180*pi180
    S_Theta_ER = sin(Theta_ERD) ; C_Theta_ER = cos(Theta_ERD);
    S2_Theta_ER = S_Theta_ER^2 ; C2_Theta_ER = C_Theta_ER^2;
    ER11 = C2_Theta_ER + a1*S2_Theta_ER ;
    ER12 = (1-a1)*S_Theta_ER*C_Theta_ER*b2 ;
    ER21 = (1-a1)*S_Theta_ER*C_Theta_ER*b1 ;
    ER22 = S2_Theta_ER + a1*C2_Theta_ER ;
    Ex = (ER11.*ExI + ER12.*EyI) ;
    Ey = (ER21.*ExI + ER22.*EyI) ;
    S0=(conj(Ex).*Ex)+(conj(Ey).*Ey);
    S1=(conj(Ex).*Ex)-(conj(Ey).*Ey);
    S2=2.*real(conj(Ex).*Ey);
    S3=2.*imag(conj(Ex).*Ey);
    threshold = .01;
    ns0=sqrt((S1.^2)+(S2.^2)+(S3.^2));
    ns1=zeros(size(ns0,1),size(ns0,2));
    ns2=zeros(size(ns0,1),size(ns0,2));
    ns3=zeros(size(ns0,1),size(ns0,2));
    ss=zeros(size(ns0,1),size(ns0,2));
    for m = 1:size(ns0,1)
        for n = 1:size(ns0,2)
            if ns0(m,n)> threshold
                ns3(m,n) = ns3(m,n)+(S3(m,n)/ns0(m,n));
                ns1(m,n) = ns1(m,n)+(S1(m,n)/ns0(m,n));
                ns2(m,n) = ns2(m,n)+(S2(m,n)/ns0(m,n));                           
            end
        end
    end
    % Stokes parameters
    f= 3.5;
    avgfact=2^f; %averaging factor
    s0=imresize(ns0,1/avgfact);
    s1=imresize(ns1,1/avgfact);
    s2=imresize(ns2,1/avgfact);
    s3=imresize(ns3,1/avgfact);
    threshold=.1;
    k=1;
    for i=1:size(s0,1)
        for j=1:size(s0,2)
            if s0(i,j)>threshold
                stk(k,1)=s1(i,j);
                stk(k,2)=s2(i,j);
                stk(k,3)=s3(i,j);
                stk(k,4)=sqrt(s1(i,j)^2+s2(i,j)^2+s3(i,j)^2);
                k=k+1;
            end
        end
    end
    [X,Y,Z] = sphere(100);
    axis square
    box on
    r=.999;
    surf(r*X,r*Y,r*Z,'EdgeColor','none','FaceColor',[.6 .6 .6],'FaceAlpha', 0.8);
    line([-1.3 1.3],[0 0],[0 0],'LineStyle','-','LineWidth',2,'Color',[0 0 0])
    line([0 0],[-1.3 1.3],[0 0],'LineStyle','-','LineWidth',2,'Color',[0 0 0])
    line([0 0],[0 0],[-1.3 1.3],'LineStyle','-','LineWidth',2,'Color',[0 0 0])
    text(1.3,-.1,-.1,'S1','FontSize',15);text(0,1.3,0,'S2','FontSize',15);text(0,0,1.3,'S3','FontSize',15)
    axis([-1 1 -1 1 -1 1])
    hold on
    view(122,53)
    phi=-pi:.01:pi;
    theta=0;
    r3=.315;
    r2=.72;
    r1=.94;
    r0=1;
    sx=cos(phi);
    sy=sin(phi);
    sz=cos(phi.*0-pi/2);
    hold on
    plot3(sx,sy,sz,'k','LineWidth',1.5);
    hold on
    plot3(sx,sz,sy,'k','LineWidth',1.5);
    hold on
    plot3(sz,sy,sx,'k','LineWidth',1.5);
    [p q]=size(stk);
    for i=1:p
        hold on
        H=plot3(stk(i,1),stk(i,2),stk(i,3),'mo');
        set(H,'markersize',10,'markeredgecolor','g','markerfacecolor','y','color','m','linewidth',1.2)
    end
end
hold off
Additonally, you can refer to the following MathWorks Documentation to know more about the 'Scope of Variables' in MATLAB:
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!