Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Matlab shortest distance program from one point to several points
Matlab shortest distance program from one point to several points
First, write. The functions of the m file are as follows:

Function f=myfun(x)

f=3*sqrt((x( 1)-3)^2+(x(2)-4)^2)+ 1 1*sqrt((x( 1)- 1)^2+(x(2)-7)^2)+5*sqrt((x( 1)-9)^2+(x(2)-3)^2);

end

% viewpoint (x, y) is a two-dimensional vector such as x =;;

[x,fval]=fminsearch(@myfun,x0)

%x0 is the initial value and can be set freely.

%myfun is the objective function.

%fval: Returns the function value of the objective function at the optimal solution X point.

%x is the minimum value.

The final running result is shown in the following figure: