How to call fminsearch on my function

15 ビュー (過去 30 日間)
Francis Dunne
Francis Dunne 2021 年 4 月 3 日
コメント済み: Walter Roberson 2021 年 4 月 4 日
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?

回答 (1 件)

John D'Errico
John D'Errico 2021 年 4 月 3 日
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 件のコメント
Francis Dunne
Francis Dunne 2021 年 4 月 3 日
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 2021 年 4 月 4 日
Yes, a vector of length 2.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by