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. |
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 |
Your plotting code:
Code:
hold on Have you tried removing "hold off"? Each plot command after "hold off" will automatically clear the plot. Hope this helps. |
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 |
thanks
thanks for the help everyone i got ut to work.
|
All times are GMT -5. The time now is 07:41 PM. |