How to call fminsearch on my function

16 visualizaciones (últimos 30 días)
Francis Dunne
Francis Dunne el 3 de Abr. de 2021
Comentada: Walter Roberson el 4 de Abr. de 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?

Respuestas (1)

John D'Errico
John D'Errico el 3 de Abr. de 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 comentarios
Francis Dunne
Francis Dunne el 3 de Abr. de 2021
Ok. The last bit I don't understand is what do assign x0 as? Still just a vector of length two?
Walter Roberson
Walter Roberson el 4 de Abr. de 2021
Yes, a vector of length 2.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by