本文介绍了将常量传递给fminsearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将常量传递给fminsearch?就像,如果我有一个功能:

How can I pass a constant to fminsearch? So like, if I have a function:

f(x,y,z),如何进行固定值x的fminsearch?

f(x,y,z), how can I do an fminsearch with a fixed value of x?

fminsearch(@f, [0,0,0]);

我知道我可以编写一个新函数并对其进行fminsearch:

I know I can write a new function and do an fminsearch on it:

function returnValue = f2(y, z)

returnValue = f(5, y, z);

...

fminsearch(@f2, [0,0]);

我的要求是,我需要在不定义新功能的情况下执行此操作.谢谢!!!

My requirement, is I need to do this without defining a new function. Thanks!!!

推荐答案

您可以使用匿名函数:

fminsearch(@(x) f(5,x) , [0,0]);

还可以使用嵌套函数:

function MainFunc()
    z = 1;
    res = fminsearch(@f2, [0,0]);

    function out = f2(x,y)
        out = f(x,y,z);
    end
end

您还可以使用getappdata传递数据.

You can also use getappdata to pass around data.

这篇关于将常量传递给fminsearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 02:07