The fminsearch is not working for my matlab but it is for others, is there something wrong with my settings and how can I change it?

5 views (last 30 days)
My code is below and the only thing that wont work is the
Result = fminsearch(@FindError,guess)
but it works on others matlab
function saltwater_inclass
% Define an initial guess = [F3; xw3; xs3]
guess = [13; 0.7; 0.3];
Result = fminsearch(@FindError,guess)
FinalError = FindError(Result)
function TotalError = FindError(guess)
% Define all of our flowrates (kg/min)
F1 = 10;
F2 = 1;
F3 = guess(1);
% Define the mass fractions of each stream
% Write each mass fraction as a vertical array (;)
% Specify that the first composition is the mass fraction of
% water, and the second item is the mass fraction of salt
% X = [water; salt]
X1 = [0.5; 0.5];
X2 = [0.9; 0.1];
X3 = [guess(2); guess(3)];
% Write our Error Statements
% Mass balance around the mixer
Error1 = F1.*X1 + F2.*X2 - F3.*X3;
% Sum of the mass fractions must equal 1
Error2 = 1 - sum(X3);
% Sum of the squared error
TotalError = Error1'*Error1 + Error2'*Error2;
end
end
  4 Comments
Niklas Willner
Niklas Willner on 3 Nov 2021
Whenever I run that exact program an error comes up saying that there is too many "Too many input arguments". Yet when someone else does it in their matlab there is no error what's so ever.
Walter Roberson
Walter Roberson on 3 Nov 2021
Experiment with
restoredefaultpath; rehash toolboxcache
if your code works after that, then you have a third-party function on your path that is interfering with a Mathworks function.
(If your code still does not work, then you could still potentially have a function in the same directory that is interfering.)

Sign in to comment.

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 3 Nov 2021
You may have another function file called FindError with different number of input args that might be overshaddowing this on.
Try to run this code, after running:
>> clear all
and changing the current directory to a diferent one.
Good luck
  1 Comment
Walter Roberson
Walter Roberson on 3 Nov 2021
In this particular case it would not matter. FindError is a nested function, and nested functions have priorities over functions in the path.
The only case that has higher priority is functions that have been import'd.

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!