在MATLAB中绘制连续变量。

huangapple go评论48阅读模式
英文:

Plotting continuous variable in MATLAB

问题

我正在使用 fplot() 来绘制表达式 exp(-t)sin(5t) 在范围 (-2,2) 上。我尝试了 fplot(exp(-t)*sin(5*t),[2,2]) 但它并没有按照我想要的方式绘制。我该怎么做?

英文:

I am using fplot() to plot the expression exp(-t)sin(5t) over the range of (-2,2). I did fplot(exp(-t)*sin(5*t),[2,2]) but it doesn't plot the way I wanted. What should I do?

答案1

得分: 3

您遇到了一个错误:

> 无效的表达式。检查是否缺少乘法运算符、缺少或不平衡的分隔符或其他语法错误。

请听取错误提示 - 您缺少乘法运算符!我们稍微改进一下代码...

fplot(exp(-t).*sin(5.*t),[2,2])

现在您会得到一个新的错误:

> 无法识别的函数或变量 't'。

再次听取错误提示,您没有定义 t,而 fplot 期望您传递一个函数(来自文档):

> fplot(f) 绘制由 函数 y = f(x) 定义的曲线。

我们可以将表达式变成一个函数,并稍微改进代码...

fplot(@(t)exp(-t).*sin(5.*t),[2,2])

现在您得到一个空白的图形??嗯,再次查看文档,我们看到:

> fplot(f,xinterval) 在指定的区间上绘制曲线。将区间指定为形式为 [xmin xmax] 的两个元素的向量。

在您的示例中,[xmin xmax] 目前都是 2。我们不能在一个范围为空的情况下绘制图形,所以选择其他值。现在我们有可工作的代码:

fplot(@(t)exp(-t).*sin(5.*t),[-2,2])

在MATLAB中绘制连续变量。

英文:

You're getting an error:

>Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error.

Listen to the error - you are missing multiplication operators! We improve the code a little...

fplot(exp(-t).*sin(5.*t),[2,2])

Now you get a new error:

>Unrecognized function or variable 't'.

Again listen to the error, you haven't defined t, and fplot expects you to pass a function (from the docs)

>fplot(f) plots the curve defined by the function y = f(x)

We can make the expression into a function and improve the code a little more...

fplot(@(t)exp(-t).*sin(5.*t),[2,2])

Now you get a blank figure?? Ah looking at the docs again, we see

>fplot(f,xinterval) plots over the specified interval. Specify the interval as a two-element vector of the form [xmin xmax].

Where [xmin xmax] are currently both 2 in your example. We can't plot over a range of nothing, so choose something else. Now we have working code:

fplot(@(t)exp(-t).*sin(5.*t),[-2,2])

在MATLAB中绘制连续变量。

huangapple
  • 本文由 发表于 2023年7月12日 22:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定