plot three dimensional surface for this code

2 views (last 30 days)
Habib
Habib on 14 Nov 2014
Commented: Habib on 15 Nov 2014
Hi every one.
I decided to plot three-dimensional surface for u3 but when i wrote mesh(r4,u3), the error of " Z must be a matrix not scalar or vector" is appeared. Now I want help to plot it, How can I do it?.please help me?
clear all
close all
clc
r_1=linspace(-1,1,100);
r_2=linspace(-1,1,100);
[r1,r2]=meshgrid(linspace(-1,1,100));
z2=4.2; % is the axial distance from the beam's narrowest point
z1=1;
L=z2-z1;
wvl=500; % is wavelength
k=2*pi./wvl;
w0=sqrt(wvl.*z1./pi); % is the waist size
% is the radius at which the field amplitude and intensity
% drop to 1/e and 1/e2 of their axial values, respectively
w1=w0.*sqrt(1+wvl.*z1./pi.*w0.^2);
w2=w0.*sqrt(1+wvl.*z2./pi.*w0.^2);
% is the radius of curvature of the beam's wavefronts
R1=z1.*(1+(pi.*w0.^2./wvl.*z1).^2);
R2=z2.*(1+(pi.*w0.^2./wvl.*z2).^2);
% mathematical form of Gaussian beam
u1=w0./w1.*exp((-1./w1.^2+1i.*pi./wvl.*R1).*(r1.^2+r2.^2)-1i.*k.*z1);
figure(1)
surf(r1,r2,real(u1))
K=zeros(1,length(r1));
u2=zeros(1,length(r1));
u3=zeros(1,length(r1));
r3=linspace(0,1,length(r1));
r4=linspace(0,1,length(r1));
for nn=1:100
for mm=1:100
K(nn,mm)=exp(-1i.*pi.*(r3(nn).^2+z1.^2+r4(mm).^2+z1.^2-2.*(r3(nn).*r4(mm)+z1.*z2))./(wvl.*L)-1i.*k.*L);
u2(nn,mm)=w0./w1.*exp((-1./w1.^2+1i.*pi./wvl.*R1).*(r3(nn).^2+r4(mm).^2)-1i.*k.*z1);
end
u3(nn)=trapz(r3,K(nn,:).*u1(nn,:));
end
figure(2)
mesh(r3,r4,u3)

Answers (1)

Chad Greene
Chad Greene on 14 Nov 2014
Indeed, r3, r4, and u3 are all 1x100 arrays. Change mesh(r3,r4,u3) to plot3(r3,r4,u3) to see for yourself. You'll have to find a way to make u3 into a 2D matrix representing a surface instead of a 1D array representing a line.

Community Treasure Hunt

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

Start Hunting!