1

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?

Seb
  • 117
  • 1
  • 6
  • what is your expected output you want to generate in TimeDiffs? – m.s. Apr 17 '15 at 22:51
  • A vector with each different nonlinear function (function = 0) on each row, as required by fsolve. @m.s. – Seb Apr 17 '15 at 22:55
  • 1
    Looks like you need to generate unique permutations of the set `[1,2,...n]`, right? If yes, have a look at http://stackoverflow.com/questions/13109710/finding-unique-permutations-efficiently, otherwise you might need to specify your problem more clearly. – m.s. Apr 17 '15 at 23:53
  • Yes sorry I could have been more clear. Finding a method for the permutations has not been the problem, however it seems as if the loops used don't do well in `fsolve`. I have updated the question with my current implementation. @m.s. – Seb Apr 18 '15 at 20:23

0 Answers0