How to have multiple comet functions all in one graph?

조회 수: 10 (최근 30일)
Kylie Ellis
Kylie Ellis 2020년 4월 15일
댓글: KSSV 2020년 4월 15일
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
Kylie Ellis
Kylie Ellis 2020년 4월 15일
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

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2020년 4월 15일
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
Kylie Ellis
Kylie Ellis 2020년 4월 15일
What is "th" and where did you get those values for linspace? Also why are you using cos and sin?
KSSV
KSSV 2020년 4월 15일
I have taken it for demo....you should extend it for your case.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by