how to plot 3D curves with for loop

조회 수: 21 (최근 30일)
Thierry
Thierry 2014년 7월 18일
답변: Thierry 2014년 7월 21일
Hello! I'm kind of beginner with matlab, however I need to plot a 3D curve. My problem is that I use a for loop to get the points because I don't see another way to proceed to avoid a problem with vectors dimensions. Here is what I do:
clear all; close all; clc;
U=400.0;
S=120000;
Phi=acos(1.0);
P=S*cos(Phi);
Q=S*sin(Phi);
I=S/(U*sqrt(3));
Zn=(U^2)/S;
delta=20.0;
for v=0:1:20
Xd=(1/Zn)*repmat(v,100,1); %abscisses
Xq=(1/Zn)*repmat(v',1,100); %ordonnées
[Xd,Xq]=meshgrid(v,v);
E1=sqrt(((U+Xq*I*sin(Phi))^2)+(Xq*I*cos(Phi))^2);
Eaf=(1/(2-Xq))*(E1+U*cos(delta)-(Xq/Xd)*U*cos(delta));
F=(-(3/2)*(U*Eaf/Xd)*sin(delta)+((U*U*(Xd-Xq))/(2*Xd*Xq))*sin(2*delta))-P;
%%%Tracé de la surface associée%%%
figure(1)
surf(Xd,Xq,F)
axis([3 6 1 6 0 10000000000]) %définition des limites des axes x,y et z.
end
And what I get is as you can see a picture with nothing on it. So what do you think I could do to solve it? Thank you for your answers.

채택된 답변

Roger Stafford
Roger Stafford 2014년 7월 18일
In the 'for' command you write:
for v=0:1:20
which means that on each trip through the loop 'v' will be a scalar quantity - that is, a single number. When you do
[Xd,Xq]=meshgrid(v,v);
that destroys the Xd and Xq you computed just before that and creates scalar values for Xd and Xq instead, both equal to the scalar v. Therefore the command
surf(Xd,Xq,F)
has only one point which is not a very effective surface plot. Moreover, since you are not activating the "hold on" command, each plot overwrites the previous one.
If you want to plot a surface using 'surf' (which is different from plotting a 3D curve) you should not be using the for-loop. Instead just write
v = 0:20;
[Xd,Xq] = meshgrid(v,v); % <-- You can also use just meshgrid(v)
% Calculate F using Xd and Xq (which will be 41 x 41 matrices)
surf(Xd,Xq,F)
If on the other hand you really do want to plot a 3D curve, you should be using the 'plot3' command, not 'surf', and you should generate F in terms of a single vector, presumably 'v'.

추가 답변 (2개)

Thierry
Thierry 2014년 7월 21일
I know but if v is not a scalar quantity, then I have the error "Matrix dimensions must agree" by the way that's the same error that I get when trying to operate your suggested changes , try this code :
clear all; close all; clc;
U=400.0; S=120000; Phi=acos(1.0); P=S*cos(Phi); Q=S*sin(Phi);
I=S/(U*sqrt(3)); Zn=(U^2)/S; delta=20.0;
v=0:20; [Xd,Xq]=meshgrid(v);
E1=sqrt(((U+Xq*I*sin(Phi))^2)+(Xq*I*cos(Phi))^2); Eaf=(1/(2-Xq))*(E1+U*cos(delta)-(Xq/Xd)*U*cos(delta)); F=(-(3/2)*(U*Eaf/Xd)*sin(delta)+((U*U*(Xd-Xq))/(2*Xd*Xq))*sin(2*delta))-P;
%%%drawing of the surface%%% figure(1) surf(Xd,Xq,F) axis([3 6 1 6 0 10000000000]) %définition des limites des axes x,y et z.

Thierry
Thierry 2014년 7월 21일
I found the problem.. that was just about using the matrix ops here is the code that works:
clear all; close all; clc; U=400.0; S=120000; Phi=acos(1.0); P=S*cos(Phi); Q=S*sin(Phi);
I=S/(U*sqrt(3)); Zn=(U^2)/S; delta=20.0;
%%%Formulation de l'équation%%% v=0:10; %Xd=repmat(v,100,1); %abscisses %Xq=repmat(v',1,100); %ordonnées [Xd,Xq]=meshgrid(v); %autre possibilité pour créer X,Y
E1=sqrt(((U+Xq.*I*sin(Phi))^2)+(Xq.*I*cos(Phi))^2); Eaf=(1./(2-Xq)).*(E1+U.*cos(delta)-(Xq./Xd).*U*cos(delta)); F=(-(3/2)*(U*Eaf./Xd).*sin(delta)+((U*U*(Xd-Xq))./(2*Xd.*Xq)).*sin(2*delta))-P;
%F=exp(-0.5*(Xd.^2+Xq.^2))/(2*pi); %valeurs de la fonctions aux points (X,Y) contour(Xd,Xq,F,20) %représentation de 20 courbes de niveau colorbar %affichage du code couleur des valeurs de F
%%%Tracé de la surface associée%%% figure; surf(Xd,Xq,F) axis([3 20 1 20 0 1000000]) %définition des limites des axes x,y et z. figure surfl(Xd,Xq,F); shading interp; colormap('copper'); axis([3 20 1 20 0 1000000])

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by