Constraints on meshgrid. Remove points outside function

I need help constraining the entries in my meshgrid. So far the entries take values on the entire x-axis and y-axis.
I have a figure consisting of an upper curve and a lower curve. Any point outside the curves should be equal to nan and not be coloured.
M = 3749.41; %MeV
m1 = 3728.4; %MeV
m2 = 0.5109989461; %MeV
m3 = 0.5109989461; %MeV
lambda = @(x,y,z) (x-y-z).^2-4*y*z; %Triangle function
s = M^2; %Mass of the decaying particle squared
s1 = linspace((m1+m2).^2,(sqrt(s)-m3)^2,1000); %s1 = s12
s2 = linspace((m2+m3).^2,(sqrt(s)-m1)^2,1000); %s2 = s23
s3 = linspace((m1+m3).^2,(sqrt(s)-m2)^2,1000); %s3 = s31
s11 = @(s2) m1^2+m2^2-1./(2*s2).*((s2-s+m1^2).*(s2+m2^2-m3^2)-lambda(s2,s,m1^2).^(1/2).*lambda(s2,m2^2,m3^2).^(1/2)); %upper half of boundary curve
s12 = @(s2) m1^2+m2^2-1./(2*s2).*((s2-s+m1^2).*(s2+m2^2-m3^2)+lambda(s2,s,m1^2).^(1/2).*lambda(s2,m2^2,m3^2).^(1/2)); %lower half of boundary curve
hold on
plot(s2,s11(s2),'r','LineWidth',2)
plot(s2,s12(s2),'r','LineWidth',2)
mr = 0.0167*1000; %Pole mass in MeV
gamma01 = 0.002*1000; %Decay width in MeV
A1 =@(a) sqrt(a)./(mr^2-a-1i*gamma01*sqrt(a)); %Breit-Wigner
T0 = 1;
Matrixelement = @(b) T0.*A1(b);
costheta = @(s1) 2.*(sqrt(s1)-min(sqrt(s1)))./(max(sqrt(s1))-min(sqrt(s1)))-1;
lpol = @(s1) legendreP(1,costheta(s1));
A1 =@(a) sqrt(a)./(mr^2-a-1i*gamma01*sqrt(a));
[x,y] = meshgrid(s2,[s11(s2),s12(s2)]);
hold on
mscale1 = @(x) (Matrixelement(s2) - min(Matrixelement(s2)))./( max(Matrixelement(s2)) - min(Matrixelement(s2))); %normalize
mscale2 = @(a) (Matrixelement(s2) - min(Matrixelement(s2)))./( max(Matrixelement(s2)) - min(Matrixelement(s2))); %normalize
z = (mscale1(x).*conj(mscale1(x))).*costheta(y).^2; %abs(1/2.*(3.*costheta(y).^2-1)); %remember spin
contourf(x,real(y),real(z),'edgecolor','none')
plot(s2,s11(s2),'r','LineWidth',2)
plot(s2,s12(s2),'r','LineWidth',2)
colorbar
All I need is to make the meshgrid only to take values inside the curve.

 Akzeptierte Antwort

Matt J
Matt J am 16 Mai 2020
Just apply logical indexing on the appropriate region and set it to NaN,
bad=(y<s11(s2)) | (y>s12(s2));
z(bad)=nan;

5 Kommentare

Thank you for your answer. I appreciate it. Is there a way to apply this in
[x,y] = meshgrid(s2,[s11(s2),s12(s2)]);
Instead of removing it? Maybe with inpolygon
Matt J
Matt J am 17 Mai 2020
Bearbeitet: Matt J am 17 Mai 2020
You could remove the unwanted x,y prior to the calculation of z and just work with the list of valid values. That might be more efficient:
[xrect,yrect] = meshgrid(s2,[s11(s2),s12(s2)]);
[x,y]=deal(xrect,yrect);
x(bad)=[];
y(bad)=[];
z=...
For plotting purposes, however, you would have to re-insert the calculated z into a rectangular array,
zrect=nan(size(xrect));
zrect(~bad)=z;
contourf(xrect,real(yrect),real(zrect),'edgecolor','none')
Like this?
[xrect,yrect] = meshgrid(s2,[s11(s2),s12(s2)]);
[x,y]=deal(xrect,yrect);
bad=(y<s11(s2)) | (y>s12(s2));
x(bad)=[];
y(bad)=[];
mscale1 = @(x) (Matrixelement(s2) - min(Matrixelement(s2)))./( max(Matrixelement(s2)) - min(Matrixelement(s2))); %normalize
z = abs(Matrixelement(x)).^2+costheta(y); %abs(1/2.*(3.*costheta(y).^2-1)); %remember spin
zrect=nan(size(xrect));
zrect(~bad)=z;
contourf(xrect,real(yrect),real(zrect),'edgecolor','none')
There seems to be only one value of zrect not equal to nan now. Am I missing something?
Check to make sure you’ve constructed the logical array “bad” correctly.
I changed it to
bad=(y>=s11(s2)) | (y<=s12(s2));
And that fixed the problem. Thank you so much! I really appreciate it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by