LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   matlab graphing (https://www.linuxquestions.org/questions/programming-9/matlab-graphing-387590/)

mshinska 11-29-2005 10:25 AM

matlab graphing
 
I am writing a code to plot a graph of everystep i've taken throughout the code. Right now it is only plotting one or two points.

clc
echo off
clear all


atHome = 0;

stepsToTry = 5000;

x = 0;
y = 0;
dx = 0;
dy = 0;
disp('Will Waldo make it home?')
disp(' ')
disp(' ')
i = 0;

while ( i <= stepsToTry & atHome == 0)
angle = 2*pi*rand();
dx = cos(angle);
dy = sin(angle);
while (x + dx < -10 | x + dx > 10 | y + dy < -10 | y + dy > 10)
angle = 2*pi*rand();
dx = cos(angle);
dy = sin(angle);
end

x = x+dx;
y = y+dy;

if ( (x>=8) & (x<=10) & (y>=8) & (y<=10) )
atHome = 1;
end
i = i + 1;
end

if (atHome==1)
disp('Waldo made it home in this many steps')
disp(i)
disp(' ')
disp(' ')
else
disp('Waldo is still lost after taking this many steps')
disp(stepsToTry)
disp(' ')
disp(' ')
disp('Please play again and try to get Waldo home!')
disp(' ')
disp(' ')
end
hold on
plot(x,y,'b')
xlabel('x-axis')
ylabel('y-axis')
title('\fontsize{20}Waldo''s Path Home')
hold off

Thanks in advance for any help that you can give.

schneidz 11-29-2005 11:51 AM

if i understand your question you want to graph multiple plots in your m-file. try:

Code:

subplot(m,n,p)
_____________________

else if it is doing 1 or 2 traverses thru the while then 'atHome' is being arbitrarily set to 1.

hth

butters64 11-29-2005 08:25 PM

Your plotting code:

Code:

hold on
plot(x,y,'b')
xlabel('x-axis')
ylabel('y-axis')
title('\fontsize{20}Waldo''s Path Home')
hold off

It looks like x and y are scalar variables, so this will only plot one point. I assume you want to plot a trail of these points?

Have you tried removing "hold off"? Each plot command after "hold off" will automatically clear the plot.

Hope this helps.

schneidz 12-06-2005 10:02 PM

x isnt accumulating so either make it a vector or move the code butters posted inside the while.

also using code tags will make your code easier to read.

try putting 'why' before your end the while

mshinska 12-07-2005 08:22 AM

thanks
 
thanks for the help everyone i got ut to work.


All times are GMT -5. The time now is 07:41 PM.