简体   繁体   English

在MatLab中绘制符号函数

[英]Plotting symbolic function in MatLab

I've some problems in plotting a symbolic function in MatLab: for example when I try to plot the function f with ezplot, where: 我在MatLab中绘制符号函数时遇到一些问题:例如,当我尝试使用ezplot绘制函数f时,其中:

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

I get the following error: 我收到以下错误:

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));

I've tried to convert the symbolic function f in the char form but it returns an analogous error: 我试图将符号函数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{:});

Thanks for any help! 谢谢你的帮助!

You must have some problem with you function definition. 您的函数定义必须有一些问题。 Perhaps x has been defined incorrectly? 也许x定义不正确?

The following works, at least in Matlab 2010b. 以下工作,至少在Matlab 2010b中有效。 It defines f as a symbolic function of the symbolic variable x : 它定义f为符号变量x符号函数

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

The following is also valid. 以下内容也有效。 It defines f as a string : 它把f定义为一个字符串

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

What if you define your function as an anonymous function: 如果将函数定义为匿名函数怎么办:

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

figure
ezplot(myfun)

在此处输入图片说明

I really don't know why the ezplot command doesn't work with my Matlab 2012b, so I had to go with a brutal solution like this :( 我真的不知道为什么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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM