How to have multiple comet functions all in one graph?

14 views (last 30 days)
Hi guys! I'm working on a school project but I am having trouble combining multiple comet functions into one graph. The assignment asks: A family with 10 kids is an ensemble – each child is unique and has its own “destiny”. You will have a family of simulations –each simulation will take 2,500 steps. Each simulation will have 2,500 steps. Start with X = 100. This is the code I have so far, but I am stuck on how to have create 10 unique multiple comets/graphs all fitted into 1 figure. I'm not sure if this is even possible, but I wanted the assignment to look clean. And if it is possible, how can I also incorporate a legend to identify which trail is which child? (I stopped the code at child 2 just to save space for this question-- but in my matlab it goes to 10)
%% System Clearing
clc;
clear all;
close all;
%% Walking/step Conditions
stepsL=1; %stepping to the left
stepsR=1; %stepping to the right
%% Individual Walking for-loop (CHILD 1)
for n = 100: 2500 % starting at 100
x = rand ;
if (x >= 0.5)
stepsL = stepsL + 1; % if moving left
else
stepsR = stepsR + 1; % if moving right
end
location(n) = stepsL - stepsR; % location tracker for direction
end
comet(location)
title('Child 1')
xlabel('number of steps')
%% Individual Walking for-loop (CHILD 2)
for n = 100: 2500 % starting at 100
x = rand ;
if (x >= 0.5)
stepsL = stepsL + 1; % if moving left
else
stepsR = stepsR + 1; % if moving right
end
location(n) = stepsL - stepsR; % location tracker for direction
end
comet(location)
title('Child 2')
xlabel('number of steps')
  1 Comment
Kylie Ellis
Kylie Ellis on 15 Apr 2020
also, did I make it start at x=100 correctly? I'm not sure if I did it right-- I'm a beginner at MATLAB

Sign in to comment.

Answers (1)

KSSV
KSSV on 15 Apr 2020
th = linspace(0,2*pi,10^3) ;
x0 = cos(th) ;
y0 = sin(th) ;
x = th ;
y = [x0 ; y0] ;
for i = 1:length(th)
plot(x(1:i),y(:,1:i))
axis([min(x) max(x) -1 1])
drawnow
endfor
  4 Comments
Kylie Ellis
Kylie Ellis on 15 Apr 2020
What is "th" and where did you get those values for linspace? Also why are you using cos and sin?
KSSV
KSSV on 15 Apr 2020
I have taken it for demo....you should extend it for your case.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!