Clear Filters
Clear Filters

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

3 views (last 30 days)
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))));
clear all
filename = 'K Values.xlsx';
filename2 = 'results.xlsx';
data = xlsread(filename,'A2:A200');
data2 = xlsread(filename,'B2:B200');
options = optimset(optimset('fsolve'), 'TolFun', 1.0e-28, 'TolX',1.0e-28);
e=zeros(length(cols_A), 2);
for i=1:1:rows_A
a = k(i);
b = t(i);
e(i, :)=fsolve(@(x) f(x,a,b),x0, options);

Answers (1)

Nitin Khola
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
% results
filename2 = 'results.xlsx';
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.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!