User input changes circle radius and velocity

조회 수: 12 (최근 30일)
Olivia Rose
Olivia Rose 2022년 3월 26일
답변: VBBV 2022년 3월 26일
I want to make this so that when a user inputs a new radius, the circle changes accordingly, but I'm not sure how to go about it.
v=input('Please provide a particle velocity: \n');
r=input('Please provide a radius: \n');
x0 = 0;
y = 3;
for time=-7:0
x = x0(1,:) + v*time;
plot(x, y,'ro'); % particle moving along the x axis
axis([-3 3 -7 3])
drawnow();
pause(.5)
end
t = linspace(0,2*pi);
x = cos(t);
y = sin(t);
plot(3*x,3*y) % current plot circle - radius 10
axis equal
hold on
for t0 = t
h = plot(3*sin(t0),3*cos(t0),'or'); % particle moving around the circle
pause(0.3)
delete(h)
end
hold off

답변 (2개)

KSSV
KSSV 2022년 3월 26일
You are using the radius while calculating the coordinates of circle.
v=input('Please provide a particle velocity: \n');
r=input('Please provide a radius: \n');
x0 = 0;
y = 3;
for time=-7:0
x = x0(1,:) + v*time;
plot(x, y,'ro'); % particle moving along the x axis
axis([-3 3 -7 3])
drawnow();
pause(.5)
end
t = linspace(0,2*pi);
x = r*cos(t); % use the radius here
y = r*sin(t); % use the radius here
plot(x,y) % current plot circle - radius 10
axis equal
hold on
for t0 = t
h = plot(3*sin(t0),3*cos(t0),'or'); % particle moving around the circle
pause(0.3)
delete(h)
end
hold off

VBBV
VBBV 2022년 3월 26일
v=10;%input('Please provide a particle velocity: \n');
r=5; %input('Please provide a radius: \n'); e.g. radius here
x0 = -2; % offset
y0 = 3; % offset
t = linspace(0,2*pi);
x = x0 + r*cos(t); % use the radius here with an offset
y = y0 + r*sin(t); % use the radius here with an offset
plot(x,y) % current plot circle
x = r*cos(t); % use the radius here without an offset
y = r*sin(t); % use the radius here without an offset
hold on
plot(x,y,'r')
you can also try using an offset distance for centre to plot

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by