plotting in a for loop

2 views (last 30 days)
Christopher
Christopher on 3 Jun 2013
Hello, I am trying to plot in a while loop and I am having trouble figuring out how to set this up. I am calling a function that I have written to vary the x component of the vector from -20 to 20 in increments of 5. The code for the function and the file I am using are shown below.
Code:
x1=-1;
y1=0;
z1=0;
x2=1;
y2=0;
z2=0;
x=-25;
y=5;
z=0;
while x<20
x=x+5
vortexsegment(x1,y1,z1,x2,y2,z2,x,y,z);
end
Function:
function[Vx,Vy,Vz]=vortexsegment(x1,y1,z1,x2,y2,z2,x,y,z);
S=[x1,y1,z1]; % defining points for start
E=[x2,y2,z2]; % defining points for end
R=[x,y,z]; % defining points for point of interst
A=S-R; % Finding vector a
B=E-R; % Finding vector b
Z1=cross(A,B);
Z2=dot(A,B);
MagA=norm(A);
MagB=norm(B);
q=(Z1./(dot(Z1,Z1))).*(MagA+MagB).*(1-(Z2./(MagA.*MagB)));
V=(1./(4.*3.14)).*q
Vx=V(1,1)
Vy=V(1,2)
Vz=V(1,3)
Thanks

Accepted Answer

Image Analyst
Image Analyst on 3 Jun 2013
Did you try to plot Vx?
while x < 20
x=x+5
[Vx, Vy, Vz] = vortexsegment(x1,y1,z1,x2,y2,z2,x,y,z);
plot(Vx, 'b-', 'LineWidth', 3);
grid on;
drawnow;
end
  2 Comments
Christopher
Christopher on 3 Jun 2013
That seems to do the trick. Thank you! What is it called when you use [Vx, Vy, Vz] = vortexsegment(x1,y1...etc)? I haven't seen that before? I am still somewhat of a newb with matlab.
Thanks again
Image Analyst
Image Analyst on 3 Jun 2013
That's called accepting the return arguments into variables in the calling routine, or something like that. vortexsegment() calculates them and sends them back to the caller, but unless the calling routine accepts them into variables, they are thrown away.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!