【问题标题】:How to make a symbolic function in MATLAB based on user input如何根据用户输入在MATLAB中制作符号函数
【发布时间】:2017-11-29 15:47:08
【问题描述】:

我正在尝试根据用户输入的参数在 MATLAB 中创建一个符号函数,然后使用 fminsearch(fun, x0) 最小化该函数,这似乎只允许符号函数。除了 sym2poly() 之外,我似乎找不到基于用户输入生成任意符号函数的方法,这仅在我想生成多项式函数时才有效。有什么想法吗?

【问题讨论】:

    标签: matlab symbolic-math minimization


    【解决方案1】:

    我认为str2func 是您正在寻找的:

    % this is actually your user input, it could be taken,
    % for example, using inputdlg function
    user_in = inputdlg('Enter your function:','Function'); % '2*x + 4'
    
    % the user input is transformed into a function handle 
    % that can be passed to fminsearch
    fh = str2func(['@(x) ' user_in]);
    
    % the function created from the user input is evaluated 
    x = fminsearch(fh,x0);
    

    您也可以让 used 定义输入参数(但我认为 fminsearch 没有必要这样做):

    str = '@(x,y) 2*x + 4*y + 1';
    fh = str2func(str);
    

    更多信息:

    【讨论】:

    • 谢谢!这不是我的想法,但它仍然运作良好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多