MATLAB Answers

How to call fminsearch on my function

1 view (last 30 days)
Francis Dunne
Francis Dunne on 3 Apr 2021
Commented: Walter Roberson on 4 Apr 2021
Hello everyone,
Currently working on a class project and could really use some help using fminsearch. I have modeled a refrigeration cycle in matlab. Now I need to optimize the code I have written to find minmum values as a part of a new assignment. The function I want to optimize is
function[W,X,LH2]=refrig(PR,PC)
however within this code there are multiple components that get called from other scripts. When I try to use
x0 = [100,0.4]
fminsearch('refrig',x0)
I get an output saying that I don't have enough input arguments because of the lack of input arguments for parts of the function which calls other scripts. However when I just run the code as
refrig(100,0.4)
it runs just fine with no errors. How can I call fminsearch for a function like this?

Answers (1)

John D'Errico
John D'Errico on 3 Apr 2021
Um, you misunderstand the problem. Your function has TWO arguments. PR and PC, both of which you want to be optimized. And while you think this is how fminsearch will work, you would be wrong.
fminsearch is passed a VECTOR of length 2 as the initial parameters. It will then pass in a VECTOR of length 2 to your funcrtion, NOT two distinct arguments. Your function needs to recognize that.
So you can either unpack that vector into PR and PC in your function, you can write a wrapper function around it. Something like this:
wrapper = @(PRPC) refrig(PRPC(1),PRPC(2));
Now you will use fminsearch on the function wrapper. It will then call refrig, splitting the parameters into TWO separate arguments.
Note that fminsearch will minimize the first output of refrig, thus W. It could care less about the other outputs.
  4 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by