How to plot combined surf and 2d plot
Show older comments
I need to plot the intensity of a beam at z=1m as shown in figure.

I wrote code to find the intensity at z=1m. But failed to plot this curve. Code attaching below.
clc;clear all;close all;
x0 = -.03:0.00015:.03;
y0 = x0;
[x,y] = meshgrid(x0) ;
z=1;
lambda=532*10^(-9);
wo = 0.01;
k = (2.*pi)./(lambda);
Pho=0.0750 ;
Gamma = ((1i.*k)./(2.*z)) + (1./(Pho.^2)) + (1./(wo.^2));
Gamma_star = subs(Gamma, 1i, -1i);
alpha = (Gamma_star) - 1./((Gamma.*Pho.^4));
beta = 1 + 1./((Pho.^2).*Gamma);
beta_s = -1 + 1./((Pho.^2).*Gamma);
C = (1./(4.*(z.^2))).*((k.^2)./(alpha.*Gamma)) ;
fx = ((1i.*k)./(2.*z)) + ((k.*k)./(4.*z.*z.*Gamma)) + ((k.*k.*beta.*beta)./(4.*z.*z.*alpha));
fy = ((-1i.*k)./(2.*z)) + ((k.*k)./(4.*z.*z.*Gamma)) + ((k.*k.*beta.*beta)./(4.*z.*z.*alpha));
X= C.*exp( -(fx.*x.^2) - (fy.*y.^2) ) ;
I= abs(double((vpa(X))))./max(abs(double((vpa(X)))));
% to plot curve
Z = peaks(x,y) ;
C = I;
surfc(x,y,Z,C)
colorbar
5 Comments
The pertinent code reduces to...
x0 = -.03:0.00015:.03;
y0 = x0;
[x,y] = meshgrid(x0) ;
....
Z = peaks(x,y) ;
surfc(x,y,Z)
Over such a small range, the surface is essentially just a plane as is shown by the resulting plot -- you certainly can't expect the full range of the peaks function to be reproduced in just one tiny little portion of the area; it isn't a Mandelbrot set...
hold on
[X,Y,Z1]=peaks(25); % get the default ranges used by peaks()
surfc(X,Y,Z1,'facecolor','none','linestyle',':') % plot all transparently with only dashed lines
zoom(2) % blow up so can see original area
Now you can see your little part plotted in persepective with the whole thing (zoomed by 2X, so it's really only one-fourth, half in each direction centered around the origin), but you see you've just computed and drawn a very tiny piece out of the whole.
What all the other code has to do with anything or its purpose is totally unclear, but what you see is what you asked for as far as the plot goes...
x=linspace(-8,8,5);
y=x;
[x,y] = meshgrid(x);
m =1;
Omega=0.1;
z=100 ; %propagation distance
sgm =1;
wo = 0.01;
lambda=532 *10^-9;
k=2*pi/lambda;
Pho=0.0162;
Gamma = ((1i.*k)./(2.*z)) + (1./(Pho.^2)) + (1./(wo.^2));
Gamma_star = ((1i.*k)./(2.*z)) + (1./(Pho.^2)) + (1./(wo.^2));
%Gamma_star = subs(Gamma, 1i, -1i);
alpha = (Gamma_star) - 1./((Gamma.*Pho.^4));
beta = 1 + 1./((Pho.^2).*Gamma);
beta_s=-1 + 1./((Pho.^2).*Gamma);
C = (1./(4.*(z.^2))).*((k.^2)./(alpha.*Gamma)) ;
fx=(( 1i.*k)./(2.*z)) + ((k.*k)./(4.*z.*z.*Gamma)) + ((k.*k.*beta.*beta)./(4.*z.*z.*alpha));
fy=((-1i.*k)./(2.*z)) + ((k.*k)./(4.*z.*z.*Gamma)) + ((k.*k.*beta.*beta)./(4.*z.*z.*alpha));
fx,fy
(fx.*x.^2),(fy.*y.^2)
X= C.*exp( -(fx.*x.^2) - (fy.*y.^2) );
V=abs(X)./max(abs(X));
I = (vpa(X));
I_numerical = abs(double(I))./max(abs(double(I))); % intensity at z=100m
%surfc(x,y,I_numerical)
%surfc(x,y,V)
V, I, I_numerical
%colorbar
We have no way to know what you're trying to model, but it appears the formulation is such that everything is NaN outside the one column of for which x=0 and the only finite value non-zero is for x,y=0.
Ensure you've actually written the equations correctly first, then examine the magnitudes of the exponential terms; as shown above, the exponential arguments being of magnitudes 10^6 and 10^7 is going to ensure they're going to underflow any reasonable numeric precision.
clc;clear all;close all;
x0 = -0.03:0.001:0.03;
y0 = x0;
[x,y] = meshgrid(x0) ;
z=1;
lambda=532*10^(-9);
wo = 0.01;
k = (2.*pi)./(lambda);
Pho=0.0750 ;
Gamma = ((1i.*k)./(2.*z)) + (1./(Pho.^2)) + (1./(wo.^2));
Gamma_star = subs(Gamma, 1i, -1i);
alpha = (Gamma_star) - 1./((Gamma.*Pho.^4));
beta = 1 + 1./((Pho.^2).*Gamma);
beta_s = -1 + 1./((Pho.^2).*Gamma);
C = (1./(4.*(z.^2))).*((k.^2)./(alpha.*Gamma)) ;
fx = ((1i.*k)./(2.*z)) + ((k.*k)./(4.*z.*z.*Gamma)) + ((k.*k.*beta.*beta)./(4.*z.*z.*alpha));
fy = ((-1i.*k)./(2.*z)) + ((k.*k)./(4.*z.*z.*Gamma)) + ((k.*k.*beta.*beta)./(4.*z.*z.*alpha));
X= C.*exp( -(fx.*x.^2) - (fy.*y.^2) ) ;
I= abs(double((vpa(X))))./max(abs(double((vpa(X)))));
plot(x0,I)
% to plot curve
%Z = peaks(x,y) ;
%C = I;
surfc(x,y,I)
colorbar
Why do you keep bringing in the builtin function peaks? It has nothing whatsoever to do with anything you're trying to do other than being an example function used in the documentation to illustrate the 3D plotting functions and/or meshgrid.
The problem is that when you try to evaluate over such a large range outside the range of validity of the numerics, you aren't getting any points in the specific region of interest and so everything other than the origin is returned as NaN.
Note that if you set the limits of the plot so large, the ROI is so small in comparison to the range of the axes you've set that the result again looks like just a single spike.
figure
surfc(x,y,I)
xlim([-8 8]),ylim([-8 8])
I don't know why you would want to use such a wide range that is some 100X the actual range. Maybe a log scaling might work, let's see, don't know that I've ever tried on a 3D...
figure
surfc(x,y,I)
xlim([-8 8]),ylim([-8 8])
hAx=gca;
hAx.XScale='log'; hAx.YScale='log';
Oh yeah, ya' can't do that with negative values...but it does show the positive side
You could set the limits to something more reasonable and still see something...
figure
surfc(x,y,I)
M=0.1;
xlim(M*[-1 1]),ylim(M*[-1 1])
Experiment with "M" -- to show anything else on the Z origin plane, you'll have to define a Z array over the larger range and then insert the finer grid in the middle...or use hold to keep the original figure and then plot the zero plane separately.
Accepted Answer
More Answers (0)
Categories
Find more on Annotations 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!














