Search code examples
matlabfminsearch

Using fminsearch on Matlab for Smallest Circle


for my homework I am supposed to calculate the smallest circle. the first part required me to calculate the euclidean distance, and i managed that with the following code:

function euclidean = center(x, y)
maximaldist = 0;
rng(0, 'twister')
A= randi([0 10],10,2)
for i=1:size(A,1)
    euclidean=sqrt((x-A(i)).^2 + (y-A(i+size(A,1))).^2);
    if euclidean > maximaldist
        maximaldist = euclidean;
    end
end

for the second part i need to go on with using fminsearch, but for some reason i cannot implement it in the code (i can use it from the command window). can anyone help me on this?

EDIT: so what i've tried...

basically everything i could find.

for i=1:size(A,1)
fun=@(x) sqrt((x(1)-A(i)).^2 + (x(2)-A(i+size(A,1))).^2);
end
x0=[0 0];
fminsearch(fun, x0)
end

this was me going haywire with the fminsearch part, out of despair.

then i changed the code into this; so i could try the one as follows;

function euclidean = denneme(x,y)
%point_x=point(1);
%point_y=point(2);
rng(0, 'twister')
a= randi([0 10],10,1);
b= randi([0 10],10,1);
distance=sqrt((x-a).^2 + (y-b).^2);
euclidean=max(distance);
f= @(x) denneme(x(1),x(2));
fminsearch(f, [0 0])

but it says not enough input arguments. i've tried so many things and deleted so many of them so i cannot reach a lot of the trials.


Solution

  • It would help if you posted what you've tried already and described what problems you're running into.

    EDIT: Here, are you overloading the center function? I'm curious that that code ran.