Help finding when the object is closest to the origin
이전 댓글 표시
I need help writing a program to determine the time the object is closest to the origin (0,0). I also have to determine the minimum distance.
The x,y coordinates are given as follows:
x(t)=5t-10 & y(t)=25t^2-120t+144
How would I find it. I've tried really hard and can't figure it out. I need to find it by:
a) using a for loop
b)without using a for loop
It would be so grateful if someone could help me.
답변 (3개)
Walter Roberson
2012년 4월 1일
1 개 추천
The time of closest approach is an irrational number, so you will not be able to find it through a numeric solution.
There is a way to use a "for" loop to solve it symbolically, but the "for" loop would have such a minor role that it would not be worth mentioning in the problem description.
댓글 수: 5
Steven
2012년 4월 1일
Image Analyst
2012년 4월 1일
Well just have a loop over t. Inside calculate x and y for that t, and then the distance. Then compare the distance to the min distance and if it's less, keep track of that t and that distance as the new min distance. Give it a shot. (You do understand that we don't just want to write the code for your homework and hand it over to you, don't you? You gotta do your part.)
Steven
2012년 4월 1일
Walter Roberson
2012년 4월 1일
You will not be able to construct a numeric solution to this problem. The time of the minimum approach is
(1/30)*(-(108+6*sqrt(330))^(2/3)+6+72*(108+6*sqrt(330))^(1/3))/(108+6*sqrt(330))^(1/3)
which is an irrational number and so is infinitely long and so cannot be represented in finite length in any "based" number system that uses only rational number bases.
The closest you would be able to get with a numeric solution is the IEEE 754 Double Precision number which most closely approximates the irrational time of closest approach. That finite double precision number will, however, *not* be the time of closest approach, merely an approximation to that time. Your statement of the problem requires the time of closest approach, not an _approximation_ of that time.
Steven
2012년 4월 1일
Image Analyst
2012년 4월 1일
Perhaps this hint will help you. It plots the data (without for loop) so you can see what's going on.
% Create t axis.
t= (0:0.1:3)';
% Get parameterized version of x and y.
x=5*t-10;
y=25*t.^2-120*t+144;
% Calculate distance of (x,y) from (0,0)
distanceFromOrigin = sqrt(x.^2 + y.^2);
% Plot the curves.
subplot(1, 2, 1);
plot(x,y, 'bo-');
axis equal;
grid on;
fontSize = 20;
% xlim([-10 10]);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
title('x vs. y, with time=0 at the left edge of the graph', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
subplot(1, 2, 2);
plot(t, distanceFromOrigin, 'bo-');
grid on;
xlabel('t', 'FontSize', fontSize);
ylabel('Distance', 'FontSize', fontSize);
title('Distance from (0,0) vs. time', 'FontSize', fontSize);
% Now calculate the mins.
댓글 수: 8
Steven
2012년 4월 1일
Image Analyst
2012년 4월 1일
For a numerical solution, you can use the min function. It returns both the min value and the index where it occurs. If you want an analytical solution, you can use basic calculus to take the derivative of the distance with respect to t. Just plug in the equations for x and y as functions of t into the distance equation I gave you and take the dreivative and set it equal to zero. You've taken calculus, right?
Walter Roberson
2012년 4월 1일
Yup, calculus is the only way to go to get the solution. Unfortunately there just isn't much use for a "for" loop in that situation. You could "for" from 1 to 3 for the 3 analytic roots, but there wouldn't be much point in that.
Steven
2012년 4월 1일
Walter Roberson
2012년 4월 2일
Time is the easier part. you construct the distance expression in terms of time, differentiate, solve for the zeros; that gives you the times at which the distances are extrema or saddle points. Use common sense or calculus to figure out which of the roots represent times of the minima. Once you have those times, you substitute them into the distance formula to get the distances at the minima.
Image Analyst
2012년 4월 2일
Steven, the for loop method is trivial. It's just like one of the very first exercises you ever learned in any programming classes - just looping to find the min or max. To review, it's code like this:
minDistance = inf;
minT = 0;
for t = 0 : 0.0001 : 3
x=5*t-10;
y=25*t.^2-120*t+144;
distance = sqrt(x^2 + y^2);
if distance < minDistance
minDistance = distance;
minT = t;
end
end
fprintf('The minimum distance of %.2f occurs at t = %.3f\n', minDistance, minT);
Keep in mind that the for loop gives an approximate solution because it's a numerical solution using quantized variables, not an exact analytical solution like you'd get with calculus.
Jacob
2014년 3월 18일
I understand how to calculate this with the for loop, however am confused on how to do this without the for loop? help?
Image Analyst
2014년 3월 18일
You have the min distance:
distanceFromOrigin = sqrt(x.^2 + y.^2);
This is an array so find the min value and location
[minDistance, indexOfMin] = min(distanceFromOrigin);
Now get the time at that index. The index where the min distance occurs will be the same for x, y, and t.
timeAtTheMinDistance = t(indexOfMin);
Sean de Wolski
2014년 3월 18일
You could use one of the optimization functions to find the minimum time. For example:
fx = @(t)5*t-10;
fy = @(t)25*(t.^2)-(120*t)+144;
tmin = 0;
tmax = 10;
[x,fval] = fmincon(@(t)hypot(fx(t),fy(t)),5,[],[],[],[],tmin,tmax)
Yields
x =
2.2330
fval =
1.3577
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!