I am trying to implement a program using fsolve
in matlab. I want the program to be able to compare n elements pairwise and where each comparison is a row in a function vector F of which fsolve
is then applied to.
So far I have written the program for 3 elements (comparing 1 and 2,1 and 3, 2 and 3), writing each function explicitly which works as wanted. But I have now tried constructing the function vector in a loop inside a matlab function. However this does not seem to work since the function is called in every iteration of fsolve
and therefore the values change in a unexpected way. Any advise on what could be done?
And the main program looks as follows:
A = [1 1;-1 -1;-1 1;1 -1];
U =[0 0];
Ustart= [6 7];
f= @(Uest)TimeDiffs(U,A,Ustart);
And the function TimeDiffs looks as follow:
function F = TimeDiffs(U,A,Uest)
F=[];
funcnum=1;
for q= 1:length(A)
for p= q+1:length(A)
td= norm(U-A(q,:))-norm(U-A(p,:));
F(funcnum)= norm(Uest-A(q,:))-norm(Uest-A(p,:))-td;
funcnum=funcnum+1;
end
end
end
There seems to be an issue when using a loop in a function that is used in fsolve
. Has anyone had any similiar experience?