필터 지우기
필터 지우기

How can I show this shape moving along the trajectory?

조회 수: 2 (최근 30일)
Mikey
Mikey 2014년 5월 10일
답변: Eugene 2014년 5월 12일
This only generates the projectile at the end of the trajectory. The shape should start at (0,20) and end at (20,100)and scale from 100% to 25%
function v = problem1
% coordinates of square under one variable square = [0, 4, 4, 0;0, 0, 4, 4];
% generate animation window figure
xmin = -5;
xmax = 25;
ymin = -5;
ymax = 125;
axis equal
axis([xmin xmax ymin ymax])
hold on
%setting your trajectory
t = linspace(0,20,500); % independent variable of your function
trajectory = 1/4*t.^2; % represents y in y = t^2
scale = .99;
for i = 500
% object moving along projectile
h = fill(square(1,:) + t(i),square(2,:) + trajectory(i),'r');
hold on;
% scales the figure from 100% to 25%
fill(scale*square(1,:) + t(i), scale*square(2,:) + trajectory(i), 'r');
hold on;
% do not change below 2 lines, should follow your fill command
v.x(i,:)=get(h,'xdata')';
v.y(i,:)=get(h,'ydata')';
pause(.3)
end
end
  댓글 수: 1
Jan
Jan 2014년 5월 10일
편집: Jan 2014년 5월 10일
Please describe the problems you have with the shown code.
What does "scales the figure from 100% to 25%" mean?

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

답변 (1개)

Eugene
Eugene 2014년 5월 12일
1. your for loop definition should be
i=1:500
2. you didn't have an algorithm to change the value of scale.
3. scale will not work to shrink the size of the square (i'm assuming that is what you're tyring to do) because the original coordinates do not put the centroid of the square at zero.
Try this:
square = [-2, 2, 2, -2;-2, -2, 2, 2];
figure;
xmin = -5;
xmax = 25;
ymin = -5;
ymax = 125;
axis equal
axis([xmin xmax ymin ymax])
hold on
N = 50;
t = linspace(0,20,N);
trajectory = 1/4*t.^2;
scale = linspace(1,0.25,N);
for i = 1:N
% object moving along projectile
h = fill(square(1,:) + t(i),square(2,:) + trajectory(i),'r');
hold on;
% scales the figure from 100% to 25%
fill(scale(i)*square(1,:) + t(i), scale(i)*square(2,:) + trajectory(i), 'r');
hold on;
pause(0.3)
end
Otherwise if you don't want to have a persistent image then just change the x and y data for the filled object.
square = [-2, 2, 2, -2;-2, -2, 2, 2];
figure;
xmin = -5;
xmax = 25;
ymin = -5;
ymax = 125;
axis equal
axis([xmin xmax ymin ymax])
hold on
N = 50;
t = linspace(0,20,N);
trajectory = 1/4*t.^2;
scale = linspace(1,0.25,N);
h1 = fill(square(1,:) + t(1),square(2,:) + trajectory(1),'r');
h2 = fill(scale(1)*square(1,:) + t(1), scale(1)*square(2,:) + trajectory(1), 'r');
for i = 2:N
% object moving along projectile
set(h1,'XData',square(1,:) + t(i),'YData',square(2,:) + trajectory(i));
hold on;
% scales the figure from 100% to 25%
set(h2,'XData',scale(i)*square(1,:) + t(i),'YData',scale(i)*square(2,:) + trajectory(i));
hold on;
pause(0.3)
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by