用MATLAB怎么算一个点到其他七个点最短距离
clear;
clc;
x=rand(7,1);
y=rand(7,1);
dist=@(var) sum(sqrt((var(1)-x).^2+(var(2)-y).^2));%var(1)=x;var(2)=y
var0=rand(2,1);
[var,minDistance,exitflag]=fminunc(dist,var0)
plot(x,y,'o','markerfacecolor','r','markersize',6);
hold on;
plot(var(1),var(2),'p','markerfacecolor','g')
for i=1:7
plot([var(1),x(i)],[var(2),y(i)],':');
end
已知七个点是(10.10 ,14.02)
(9.31, 13.42)
(8.34 ,10.08)
(7.52 ,11.11)
(8.16 ,10.34)
(7.55 ,10.0)
(6.18 ,10.48)怎么改代码
没学过MATLAB。。。
clear;
clc;
x=[10.10,9.31,8.34,7.52,8.16,7.55,6.18];
y=[14.02,13.42,10.08,11.11,10.34,10.0,10.48];
dist=@(var) sum(sqrt((var(1)-x).^2+(var(2)-y).^2));%var(1)=x;var(2)=y
var0=rand(2,1);
[var,minDistance,exitflag]=fminunc(dist,var0)
plot(x,y,'o','markerfacecolor','r','markersize',6);
hold on;
plot(var(1),var(2),'p','markerfacecolor','g')
for i=1:7
plot([var(1),x(i)],[var(2),y(i)],':');
end
2024-08-07 广告
graphshortestpath
代码怎么写?可以具体写一下吗
这要看情况,
例:
求有向图中从顶点到顶点的最短路:
输入如下程序:G = [0 8 2 0 0 0; 0 0 0 5 0 0; 0 6 0 3 8 0; 0 0 0 0 0 9; 0 5 0 6 0 7; 0 0 0 0 0 0]; %图G的邻接矩阵
SG= sparse(G); %矩阵G的稀疏矩阵
[dist,path]=graphshortestpath(SG,1,5) %有向图G的从点1到点5的最短路%绘出有向图G和最短路
%到这里就出结果了,下面是画图
BGobj = biograph(SG,[],'ShowArrows','on','ShowWeights','on')h=view(BGobj);set(h.Nodes(path),'Color',[1 0.4 0.4]);
edges = getedgesbynodeid (h, get(h.Nodes(path),'ID'));
set(edges,'LineColor', [1 0 0]);
set(edges, 'LineWidth',1.5);
输出如下结果:
>> dist = 10
path = 1 3 5
Biograph object with 6 nodes and 10 edges.