How can I write a program and plot a 3-D graph?

There are 60 points in a plane. (15 points in the X-axis and 4 points in the Y-axis,respectively). The plane is divided into elements with various size. In the first element there are 20 points (X - from 1 to 5 and Y - form 1 to 4), the second - 16 points (X - from 6 to 9 and Y - form 1 to 4), the third (X - from 10 to 12 and Y - form 1 to 4), the fourth (X - from 13 to 14 and Y - form 1 to 4), the last ( X - 15 , Y from 1 to 4). I need to sum up all the points in each element and show the result in 3-D graph.
%To sum all the points in the first element
for i=1:5
for j=1:4
x(i)=i;
y(j)=j;
z(i,j)=x(i)+y(j);
end
end
%To sum all the points in the second element
for i=6:9
for j=1:4
x(i)=i;
y(j)=j;
z(i,j)=x(i)+y(j);
end
end
Finally, I need to show the results one element after another in 3-D plot. So, I have to write many for loops and I don't want it. Kindly help with your suggestions please. Sorry for my poor English.

 Accepted Answer

x=1:5;y=1:4; % Example
[x1,y1]=meshgrid(x,y)
z=x1+y1
mesh(x1,y1,z)
%or
plot3(x1(:),y1(:),z(:))

4 Comments

Thank you very much for your suggestion, sir. I really appreciate it. What i understand is that you have written the program as an example just for the sum of the points in the first element. In order to get the sums of the other elements, if I truthfully understand, I need to repeat as follow:
x=1:5;y=1:4;
[x1,y1]=meshgrid(x,y)
z=x1+y1
x=6:9;y=1:4;
[x2,y2]=meshgrid(x,y)
z2=x2+y2
x=10:12;y=1:4;
[x3,y3]=meshgrid(x,y)
z3=x3+y3
x=13:14;y=1:4;
[x4,y4]=meshgrid(x,y)
z4=x4+y4
x=15:15;y=1:4;
[x5,y5]=meshgrid(x,y)
z5=x5+y5
I would like to show the results of z, z2, z3, z4, z5 in the same plot. So I tried like this
plot3(z(:),z2(:),z3(:),z4(:),z5(:))
Then, an error has occurred ("Vectors must be the same lengths."). Is it possible to show all the Z's with different lengths in a 3-D plot? Is there any way to shorten the program? Could you kindly help me again, plz?
plot3(x1(:),y1(:),z1(:),x2(:),y2(:),z2(:),x3(:),y3(:),z3(:),x4(:),y4(:),z4(:),x5(:),y5(:),z5(:))
Thank you so much again Sir. I got It... :)
Is there any possible way to shorten that program, Sir? Because I have a similar program as follow:
clear clc
global F1 Fi A Re Im Devi z Multiplygg gg2 A2 V1 S2 Uc2 Us2 Rz2 Rz Us Uc URe UIm UReout UImout UReout2 UImout2 Gauss NcountMax NcountMin DiagramVV DiagramHH thita2 Kv2 p2 DiagVV DiagHH C2 Gausss2 VV
lx=0.2; ly=0.2; a=21.244; H=70; K0=0.072; c=3*10^8; sigmah=2.62*10^(-3);
wavelength=8.6*10^(-3); k=(2*pi)/wavelength; n1=1; n2=1.733; g1=2.2*(1-exp(-0.2*k*sigmah)); x1=3.5+((1/pi)*(tan(10*(1.65-(k*sigmah))))^(-1)); pulsewidth=2*10^(-9); freq=35*10^9; Power=1; Gain=598.1; RCS=g1; tu=2*10^(-9);
Rmax = 140.6983; Rmin = 73.2282; tdMax=2*(Rmax)/(3*10^8); tdMin=2*(Rmin)/(3*10^8); df=(500*10^6); tsampling=1/(5*df); NcountMax=tdMax/tsampling; NcountMin=tdMin/tsampling;
nx=500; ny=4;
aa=normrnd(0,sigmah,nx,ny);
UReout=0; UImout=0; UReout2=0; UImout2=0;
for i=1:5 for j=1:4
for kk=1:NcountMax
x(i)=i*lx;
y(j)=j*ly;
h=aa(i,j);
z(i,j)=0+h;
R1(i,j)=sqrt((a+x(i))^2+(y(j))^2);
R2(i,j)=sqrt(H^2+(R1(i,j))^2);
Rz(i,j)=R2(i,j)-(z(i,j)*R2(i,j)/H);
td=2*Rz(i,j)/c;
thita(i,j)=(acos((H-z(i,j))/Rz(i,j)));
Kv(i,j)=(((n1*cos(thita(i,j)))-(n2*sqrt(1-(((n1/n2)*sin(thita(i,j)))^2))))/((n1*cos(thita(i,j)))+(n2*sqrt(1-(((n1/n2)*sin(thita(i,j)))^2)))))^2;
Kh(i,j)=(((n1*sqrt(1-((n1/n2*sin(thita(i,j)))^2)))-((n2*cos(thita(i,j)))))/(((n1*sqrt(1-((n1/n2*sin(thita(i,j)))^2)))+((n2*cos(thita(i,j)))))))^2;
p(i,j)=(1-((((2*thita(i,j))/pi)^(1/(3*K0)))*(exp(-0.4*k*sigmah))))^2;
DiagramVV(i,j)=((g1*((cos(thita(i,j)))^x1))/sqrt(p(i,j)))*(Kv(i,j)+Kh(i,j));
DiagramHH(i,j)=p(i,j)*DiagramVV(i,j);
A=sqrt((2*Power*(wavelength)^2*(Gain)^2*RCS)/(4*pi)^3)*(1/(Rz(i,j))^2);
Gauss(kk)=exp(-pi*(((kk*tsampling)-td)/tu)^2);
hc(kk)=Gauss(kk)*cos(2*pi*freq*((kk*tsampling)-td));
hs(kk)=Gauss(kk)*sin(2*pi*freq*((kk*tsampling)-td));
V1=A*DiagramHH(i,j)*Gauss(kk);
C=cos(2*pi*freq*((kk*tsampling)-td));
S=sin(2*pi*freq*((kk*tsampling)-td));
Uc=V1*C; Us=V1*S; Ucos(kk)=Uc; Usin(kk)=Us;
R12(i,j)=sqrt((0.8+a+x(i))^2+(y(j))^2); R22(i,j)=sqrt(H^2+(R12(i,j))^2); Rz2(i,j)=R22(i,j)-(z(i,j)*R22(i,j)/H);
td2=2*Rz2(i,j)/c;
thita2(i,j)=(acos((H-z(i,j))/Rz2(i,j)));
Kv2(i,j)=(((n1*cos(thita2(i,j)))-(n2*sqrt(1-(((n1/n2)*sin(thita2(i,j)))^2))))/((n1*cos(thita2(i,j)))+(n2*sqrt(1-(((n1/n2)*sin(thita2(i,j)))^2)))))^2;
Kh2(i,j)=(((n1*sqrt(1-((n1/n2*sin(thita2(i,j)))^2)))-((n2*cos(thita2(i,j)))))/(((n1*sqrt(1-((n1/n2*sin(thita2(i,j)))^2)))+((n2*cos(thita2(i,j)))))))^2;
p2(i,j)=(1-((((2*thita2(i,j))/pi)^(1/(3*K0)))*(exp(-0.4*k*sigmah))))^2;
DiagVV(i,j)=((g1*((cos(thita2(i,j)))^x1))/sqrt(p2(i,j)))*(Kv2(i,j)+Kh2(i,j));
DiagHH(i,j)=p2(i,j)*DiagVV(i,j);
A2=sqrt((2*Power*(wavelength)^2*(Gain)^2*RCS)/(4*pi)^3)*(1/(Rz2(i,j))^2);
Gausss2(kk)=exp(-pi*(((kk*tsampling)-td2)/tu)^2);
hc2(kk)=Gausss2(kk)*cos(2*pi*freq*((kk*tsampling)-td2));
hs2(kk)=Gausss2(kk)*sin(2*pi*freq*((kk*tsampling)-td2));
VV=A2*DiagHH(i,j)*Gausss2(kk);
C2=cos(2*pi*freq*((kk*tsampling)-td2));
S2=sin(2*pi*freq*((kk*tsampling)-td2));
Uc2=VV*C2; Us2=VV*S2;
Ucos2(kk)=Uc2; Usin2(kk)=Us2;
end
Uout1=conv(Ucos,hc);
Uout2=conv(Usin,hc);
Uout3=conv(Ucos,hs);
Uout4=conv(Usin,hs);
URe=Uout1-Uout4;
UIm=Uout2+Uout3;
Uout21=conv(Ucos2,hc2); Uout22=conv(Usin2,hc2);
Uout23=conv(Ucos2,hs2);
Uout24=conv(Usin2,hs2);
URe2=Uout21-Uout24;
UIm2=Uout22+Uout23;
UReout=UReout+URe;
UImout=UImout+UIm;
UReout2=UReout2+URe2; UImout2=UImout2+UIm2;
gg1=URe+(sqrt(-1)*UIm); gg2=URe2-(sqrt(-1)*UIm2);
Multiplygg=gg1.*gg2; Summ=sum(Multiplygg);
Re=real(Summ); Im=imag(Summ);
Devi=Im./Re; Fi=(180/pi).*atan(Devi);
F1(i,j)=Fi;
end
end
I have written it just for the points in the first element. There will be 2000 points (500 points in the X-axis and 4 points in the Y-axis,respectively). For all elements I need to repeat many times.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics 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!