Jump to content

MATLAB Question(2): minimum value..


mooeypoo

Recommended Posts

Okay I'm CERTAIN this is an idiotic question, but I'm stuck, so.. help.

 

The question is as follows:

 

The (x,y) coordinates of a certain object as a function of time t are given by

[math]x(t)=5t-10[/math]

[math]y(t)=25t^2-120t+144[/math]

 

for 0<=t<=4.

Write a program to determine the time at which the object is the closest to the origin at (0,0). Determine also the minimum distance.

 

Do this in two ways:

a. By using a for loop.

b. By not using a for loop.

 

This isn't a hard question for part A.. this is what I did:

 

disp('--(a)-- With For Loop:');
%calculate max value (to compare to):
t=4; x=5*t-10; y=25*t^2-120*t+144;
minDist=sqrt(x^2+y^2);
for t=0:0.1:4
   x=5*t-10;
   y=25*t^2-120*t+144;
   %calculate distance:
   Dist = sqrt(y^2 + x^2);
   if Dist<minDist
       minDist=Dist;
       minT=t;
       minX=x;
       minY=y;
   end
end
Xminimum=5*minT-10;
Yminimum=25*minT^2-120*minT+144;

TimeForMinimum=minT
MinimumDistance=minDist

 

But I'm getting stuck at part b, where I'm nto supposed to use "for" loop. I started by representing the equation with x in terms of y:

 

disp('--(b)-- Without For Loop:');
% represent t in terms of y(x):
t = [0:0.1:4];
x = 5.*t-10;
y = 25.*((x+10)./5).^2 - 120.*(x+10)./5 + 144;
%plot(x,y)
minY=min(y)

 

The Y minimum value is set up great, but now I am in trouble as to how to find the X value when Y is minY....

 

egh. I'm not supposed to use loop, and I'm not sure how to get the index of the X value that gives minY... so I dont know how to extract 'minX'...

 

Anyone? help...

 

Thanks!!

 

~moo

 

Okay I updated part B a bit. First, I simplified y(x) equation, then used 'roots' to find the values of x..

 

My problem now is that this method does not bring the same answer as the other method (using 'for' loop).

 

disp('--(b)-- Without For Loop:');
% represent t in terms of y(x):
t = [0:0.1:4];
x = 5.*t-10;
y=x.^2-4*x+4;
minY=min(y)
newD=4-minY;
%new equation: 0 = x^2 - 4x + newD
rootsvec=[1 -4 newD];
minX=roots(rootsvec);
%chose the minimum value of the two
minX=min(abs(minX))
%calculate minimum distance:
distance=sqrt(minX^2 + minY^2);
%display results:
disp('X/Y Values closest to origin:');
Xmin=minX
Ymin=minY
distance=distance

 

these are the outputs:

--(a)-- With For Loop:

TimeForMinimum =

   2.2000


MinimumDistance =

   1.4142

--(b)-- Without For Loop:

minY =

    0


minX =

    2

X/Y Values closest to origin:

Xmin =

    2


Ymin =

    0


distance =

    2

 

Notice, part A answers and part B answers don't correlate.. I don't know why... :\

 

help?

 

update:

%%
disp('--(b)-- Without For Loop:');
% represent t in terms of y(x):
t = [0:0.1:4];
x = 5.*t-10;
y = x.^2-4*x+4;
dist= (x.^2+y.^2).^(1/2);
R=min(dist)
%draw a new equation (circle) with radius R
circX=[-2*R:0.1:2*R]
circY=(R.^2-circX.^2).^(1/2);
%look for intersections:

 

That's where I'm stuck now.. intersections.

 

The roots are messing me up, and i can't come out with a polynomial = 0

if I get that, I can solve it...

Link to comment
Share on other sites

Oookay here's the solution.

 

Thanks to all who helped in the SFN Chatroom, you guys have been awesome! :)

 

Here's the full algorithm:

disp('--(b)-- Without For Loop:');
% represent t in terms of y(x):
t = [0:0.1:4];
x = 5.*t-10; y = x.^2-4*x+4;
dist= (x.^2+y.^2).^(1/2);
R=min(dist)
% draw a new equation (circle) with radius R
  circX=[-R:0.1:R];
  circY=(R.^2-circX.^2).^(1/2);
  circY2=-circY;

figure;
subplot(2,2,1);plot(x,y);
   axis([min(x) max(x) min(y) max(y)]);
   title('[P#20-b] Fig 1: Y(x) function');
subplot(2,2,2);plot(circX,circY,'r',circX,circY2,'r');
   axis([-(max(circX)+.1) (max(circX)+.1) -(max(circY)+.1) (max(circY)+.1)]);
   title('[P#20-b] Fig 2: Circle (Radius = Min Distance)')
  % look for intersections:
   % 0= x^4-4x^3+21x^2-32x+(16-R^2)
newD=16-R^2;
rootsvec=[1 -4 21 -32 newD];
xVal=[roots(rootsvec)]';
yVal=xVal.^2-4*xVal+4;
DistVals=(xVal.^2+yVal.^2).^(1/2);
%find the index of min(DistVals) inside DistVals:
AnswerIndex=find(DistVals==min(DistVals));
minX = xVal(AnswerIndex)
minY = yVal(AnswerIndex)
%draw the 'line of distance':
DistX=[0:0.1:minY];
DistY=DistX;
%add this to the figure:
subplot(2,3,5);plot(x,y,'b',circX,circY,'r');
   hold on;plot(minX,minY,'mo');text(minX+.1,minY+.1,'Shortest Distance from Origin');
   hold on;plot(DistX,DistY,'m--');
   axis([0 (max(circX)+.1) 0 (max(circY)+.1)]);
   title('[P#20-b] Intercepts of Fig 1 and Fig 2');

 

Basically, I found the minimum distance from the origin (which i already knew how to do) and then created another equation of a circle with radius equalling the minimum distance. Equating the two equations (y(x) and the cirlce) gave me the points that match, which gave me the corresponding X/Y values.

 

I also made sure the algorithm outputs the graphs so it's understood what I did there with the circle and the y(x) equation.

 

Thanks again to all who helped. Apparently, this is above and beyond what the professor asked, so I am getting extra credit for this (well.. I *did* bust my behind on it ;) ) so I guess it was worth it.

 

 

~moo

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.