【问题标题】:Plotting symbolic function in MatLab在 MatLab 中绘制符号函数
【发布时间】:2014-08-28 16:46:22
【问题描述】:

我在 MatLab 中绘制符号函数时遇到了一些问题:例如,当我尝试使用 ezplot 绘制函数 f 时,其中:

f = 9/2 - ((2*x)/5 - 2/5)*(x/3 - 17/6) - x

我收到以下错误:

Error using findstr
Inputs must be character arrays.

Error in ezplot>ezplot1 (line 442)
    if (isa(f, 'inline') && ~isempty(findstr(char(f), '=')))

Error in ezplot (line 145)
                [hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});

Error in sym/ezplot (line 61)
   h = ezplot(fhandle(f));

我尝试将符号函数 f 转换为 char 形式,但它返回一个类似的错误:

Error using findstr
Inputs must be character arrays.

Error in ezplot>ezplot1 (line 442)
    if (isa(f, 'inline') && ~isempty(findstr(char(f), '=')))

Error in ezplot (line 145)
                [hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});

感谢您的帮助!

【问题讨论】:

    标签: matlab plot


    【解决方案1】:

    你的函数定义一定有问题。也许x 定义不正确?

    以下工作,至少在 Matlab 2010b 中。它将f 定义为符号变量x符号函数

    >> clear all
    >> syms x
    >> f = 9/2 - ((2*x)/5 - 2/5)*(x/3 - 17/6) - x;
    >> ezplot(f)
    

    以下也是有效的。它将f 定义为字符串

    >> clear all
    >> f = '9/2 - ((2*x)/5 - 2/5)*(x/3 - 17/6) - x';
    >> ezplot(f)
    

    【讨论】:

    • 我在我的程序中将 f 和变量 x 定义为 syms!
    • 好吧,我认为这是黑魔法,但我重复了很多次,清除代码前后的工作区,但它仍然不起作用!
    【解决方案2】:

    如果您将函数定义为匿名函数会怎样:

    myfun = @(x)  4.5 - (((2*x)/5 - 2/5)*(x/3 - 17/6) - x);
    
    figure
    ezplot(myfun)
    

    【讨论】:

    • 我不能使用这个表达式,因为我有一个给定的符号表达式向量,就像我之前发布的那样。
    • 哦,我明白了,对不起。
    【解决方案3】:

    我真的不知道为什么 ezplot 命令不适用于我的 Matlab 2012b,所以我不得不采用这样的残酷解决方案 :(

    syms x
    f = 9/2 - ((2*x)/5 - 2/5)*(x/3 - 17/6) - x;
    
    k = 0.1;
    x_p = 0:k:10;
    y_p = subs(f,x,x_p);
    plot(x_p,y_p)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多