0

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.

1 Answers1

0

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.

kmc
  • 116
  • 3
  • oh, sorry. i'm editing right now to add what i've tried. in some cases it overloads the system and gives a "not enough memory" error, in some cases i just get "not enough input arguments" error. – ewesninski Nov 03 '16 at 19:38
  • I see you're defining denneme but I don't see an end so I'm assuming you're not including that part... It looks like `denneme` takes two inputs and you're giving it one. That may be your problem. – kmc Nov 03 '16 at 20:08
  • I'm looking around and I think `fminsearch` actually requires your function to have only one input variable. You might need a loop that evaluates one of your input variables and runs `fminsearch` on that. It looks like you were trying to do that in your first `fminsearch` snippet up there, but in the loop, you're only getting `fun` where `i=size(A,1)`, since you're overwriting it every time, and when I tried it, using an anonymous function in the loop meant it read `A` and `i` as variables, not evaluating `A(i)`. – kmc Nov 03 '16 at 20:18
  • oh i just didn't copy the "end" for denneme, it has an end. problem is, i can use fminsearch from the command window. but i can't just implement it in the code. the first attempt at coding the problem had quite lot mistakes -- that's why i switched to the second one, to see if it'd be better. it returns the correct values, but i can't use fminsearch :/ – ewesninski Nov 03 '16 at 21:27
  • Ah, got it. So are you able to make it work in the command window with two input variables for f? That's the problem I'm seeing when I try to replicate it. You might try changing denneme to take a single input that's a 2-element array and then index it within the function and see if fminsearch works with that. – kmc Nov 04 '16 at 20:43
  • I know it's a bit late, but thanks! I somehow managed to have it working, but your comments helped me a lot in terms of fixing the code. Thanks so much :)) – ewesninski Nov 15 '16 at 13:45
  • Oh, I'm so glad! Sometimes the only thing that'll do is a rubber duckie. ;.) – kmc Nov 15 '16 at 19:03