Clear Filters
Clear Filters

how to plot 3D curves with for loop

21 views (last 30 days)
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.

Accepted Answer

Roger Stafford
Roger Stafford on 18 Jul 2014
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'.

More Answers (2)

Thierry
Thierry on 21 Jul 2014
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 on 21 Jul 2014
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])

Categories

Find more on 2-D and 3-D Plots 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!