Matlab 曲线到定点距离最短问题
t在[0, 4]之间,求曲线到(0, 0)的最短距离。 展开
You can use Matlab's symbolic package to get the minimum distance, you might need to use a little bit calculus
t = sym('t','real') % define a real symbol variable
x=5*t-10;
y=25*t.^2-120*t+144;
d = (x^2+y^2)^.5; % distance function
d1 = diff(d); % the differentiation
t1s = solve(d1); % let the differentiation equal to 0 to get the maxmin point
t1v = eval(t1s); % get the numerical evaluation
min_d = subs(d,t,t1v)
min_d =
1.3577
You can use the following code to generate a figure of illustrating the curve and the distance:
t = [0:0.001:4];
x=5*t-10;
y=25*t.^2-120*t+144;
figure(1);
hold on;
plot(x,y,'b');
axis([0 4 0 4],'square');
x=5*t-10;
d=(x.^2+y.^2).^.5;
figure(1);
hold on;
plot(x,d,'g');
t=t1v;
x=5*t-10;
y=25*t.^2-120*t+144;
plot(x,y,'r.');
plot([0 x],[0 y],'r');
plot([x x],[0 4],'--');
And one more question, how to determine x, y at which the curve is the closest to the origin(0,0)? Thank you~
when you have the parameter t, you can simply substitute it into the formula to obtain x and y, in my code:
t=t1v;
x=5*t-10;
y=25*t.^2-120*t+144;
And the point is a red point on the figure.
2024-08-12 广告
t=0:0.01:4;x=5*t-10;y=25*t.^2-120*t+144;plot(x,y)
figure
plot(x,y)
hold on
syms x(t) y(t)
x=5*t-10;
y=25*t.^2-120*t+144;
kf=-diff(x)/diff(y);%法线的斜率
%设曲线上(x,y)点及(0,0)在法线上,则法线的斜率也等于下式
kk=(y-0)/(x-0);
%解方程求交点的t值
t1=solve(kf==kk,'MaxDegree',3,'Real',true);
t2=double(t1);
x1=double(subs(x,t,t2));
y1=double(subs(y,t,t2));
d=hypot(x1,y1);
fprintf('原点到曲线的最短距离是 %f\n',d)
plot(x1,y1,'r*')
plot(0,0,'r*')
plot([0,x1],[0,y1])
hold off
axis equal
axis([-1,+5,-1,10])
matlab有个直接求两点间的距离的函数;你在循环比较;
或者你把他看成线性规划问题,你应该懂得;
再则 用lingo 也行;实在不懂的话就写c语言把 干脆点也行 。具体函数还是你自己来熟练操作把
广告 您可能关注的内容 |