How can I get fsolve to not display duplicate answers in a loop?

1 view (last 30 days)
Luis Mendez on 10 Aug 2015
Answered: Nitin Khola on 12 Aug 2015
Working on a simultaneous NLE solver where the constants are obtained experimentally and a new x,y need to be solved for each constant, on a typical run there are over 200 constants so matlab will really speed things up for me. I have gotten this far by researching and consulting books, the two values fsolve gives me are duplicates, I cant figure out why and I've spent a lot of time researching it.
f = f(x,a,b)
F(1) =(x(1)/(2+x(1)))*((x(1)-x(2))/2+x(1))-a*((1-x(1))/(2+x(1)));
F(2) =((x(2)/(2+x(1))^2)-b*((x(1)-x(2))/(2+x(1)))*((1-x(2))/(2+x(1))));
end
clc
clear all
filename = 'K Values.xlsx';
filename2 = 'results.xlsx';
options = optimset(optimset('fsolve'), 'TolFun', 1.0e-28, 'TolX',1.0e-28);
sizem=size(data);
rows_A=sizem(1);
cols_A=sizem(2);
e=zeros(length(cols_A), 2);
for i=1:1:rows_A
k(i)=data(i,1);
t(i)=data2(i,1);
a = k(i);
b = t(i);
x0=[0;0];
e(i, :)=fsolve(@(x) f(x,a,b),x0, options);
end
xlswrite(filename2,e,'A2:B200');

Nitin Khola on 12 Aug 2015
I understand that you wish to solve your system of equations for a set of values of parameters a and b.
I tried to solve the same equations using a smaller set of a and b. Since, the values for a and b were not provided, I assumed some values.
% equations
f = @(x,a,b)[(x(1)/(2+x(1)))*((x(1)-x(2))/2+x(1))-a*((1-x(1))/(2+x(1)));((x(2)/(2+x(1))^2)-b*((x(1)-x(2))/(2+x(1)))*((1-x(2))/(2+x(1))))];
% constants
a = [1 2 3];
b = [3 2 1];
options = optimset(optimset('fsolve'), 'TolFun', 1.0e-28, 'TolX',1.0e-28);
x0 = [0;0]; % initial guess
e = zeros(3,2); % preallocate, store each solution in a row
for i = 1:3
e(i,:)=fsolve(@(x) f(x,a(i),b(i)),x0, options)'; % fsolve returns a column here
end
% results
filename2 = 'results.xlsx';
xlswrite(filename2,e);
The output in this case looks like,
>> e
e =
0.5904 0.3832
0.7000 0.3859
0.7543 0.3084
As you can see, there are no duplicates. However, in your case since the a and b values are different, you might be facing a sensitivity issue there. In case, you can reproduce the issue you are facing with a smaller set of a and b, provide that script so that it can be better diagnosed. You might also want to look into the "stopping criteria details" to investigate the probable cause of the issue.