how to plot 3D curves with for loop
19 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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'.
0 Comments
More Answers (2)
See Also
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!