假设x,y,z是Matlab数组,画出函数e ^(- x)在区间[0 sin(x)。4π)

时间:2021-10-03 20:26:31
x=linspace(0, 2*pi, 100);
y=sin(x);
z=exp(-x);

Given that x, y, and z are already initialized, how do I write a function that plots exp(-x)sin(x) across the interval [0, 4pi] without additional calls to sin or exp? Just need some help getting started.

考虑到x、y和z已经初始化,我如何编写一个函数,在不调用sin或exp的情况下,将exp(-x)在区间[0,4]中绘制出来?只是需要一些帮助开始。

Thanks to @Rayryeng for getting me started. I believe the following command more closely satisfies the question's specifications.

感谢@Rayryeng让我开始。我相信下面的命令更能满足问题的规格。

plot(x+x, z.*z.*y)

1 个解决方案

#1


1  

Well, you've already created arrays for sin and exp stored in y and z respectively. These arrays were created on the same domain as x. You just need to multiply both arrays together element-wise and plot the graph. It's as simple as doing:

你已经为sin和exp创建了数组分别存储在y和z中。这些数组是在与x相同的域上创建的,您只需要将两个数组一起相乘,并绘制图形。就这么简单:

plot(x, z.*y);

Here, .* stands for element-wise multiplication. If you were to do z*y, MATLAB interprets this as matrix multiplication where z and y are interpreted to be matrices. This is obviously not what you want.

这里,*表示元素的乘法。如果你要做z*y, MATLAB将它解释为矩阵乘法,z和y被解释为矩阵。这显然不是你想要的。

However, your array of x only contains points from 0 to 2*pi. If you want to plot this from 0 to 4*pi, you have to modify your call to linspace:

然而,你的x数组只包含0到2*的点。如果你想从0到4*pi,你需要修改你对linspace的调用:

x=linspace(0, 4*pi, 100); %// Change
y=sin(x);
z=exp(-x);
plot(x, z.*y);

Now, x will contain 100 points between 0 to 4*pi. For more information on basic MATLAB operations, check out this link: http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html. What you have asked falls into the basic realms of array and matrix operations.

现在,x将包含100点在0到4*之间。有关基本的MATLAB操作的更多信息,请查看此链接:http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html。你所要求的是数组和矩阵运算的基本领域。

Edit

In the spirit of your question, we can't modify linspace. You did something clever where we can simply scale our values of x by 2 or adding with x so that we have points going from 0 to 2*pi to 0 to 4*pi. Also, if we scale our points by 2, this means that our input argument into the function must also be scaled by 2. So, the final function we need to plot is:

本着你的问题,我们不能修改linspace。你做了一些聪明的事情,我们可以简单地将x的值缩放到2或者加上x,这样我们就有了从0到2*到0到4*的点。同样,如果我们将点按2进行缩放,这意味着我们的输入参数也必须被缩放2。所以,我们要画的最后一个函数是

y = exp(-2x)*sin(2x)

Noting your hint, exp(-2x) = exp(-x-x) = exp(-x)exp(-x). Further, note that sin(2x) performs a compression by a factor of 2 (tip of the hat goes to knedlsepp for noticing my blunder). Due to the periodic nature of sin(x), we know that elements will repeat after 2*pi, and so if you want to go to 4*pi, simply subsample y by a factor of 2 and then append these same elements to a new vector. Therefore, our expression for the function simplifies to:

注意你的提示,exp(-2x) = exp(-x-x) = exp(-x)exp(-x)。此外,请注意,sin(2x)执行压缩的因子为2(由于注意到我的错误,这顶帽子的顶端被压在了膝盖上)。由于sin(x)的周期性质,我们知道元素会在2*pi之后重复,所以如果你想要到4*pi,简单地把y减去一个因子2然后将这些相同的元素附加到一个新的向量中。因此,函数的表达式简化为:

y = exp(-x)exp(-x)sin(2x)

This leads to the answer alluded to knedlsepp:

这就引出了关于knedlsepp的回答:

plot(x+x, z.*z.*[y(1:2:end) y(1:2:end)]);

As such, you should consider changing your edits to match this answer instead. It isn't quite right with respect to the sin(x) part in your code.

因此,您应该考虑更改您的编辑以匹配这个答案。对于代码中的sin(x)部分,它并不完全正确。

#1


1  

Well, you've already created arrays for sin and exp stored in y and z respectively. These arrays were created on the same domain as x. You just need to multiply both arrays together element-wise and plot the graph. It's as simple as doing:

你已经为sin和exp创建了数组分别存储在y和z中。这些数组是在与x相同的域上创建的,您只需要将两个数组一起相乘,并绘制图形。就这么简单:

plot(x, z.*y);

Here, .* stands for element-wise multiplication. If you were to do z*y, MATLAB interprets this as matrix multiplication where z and y are interpreted to be matrices. This is obviously not what you want.

这里,*表示元素的乘法。如果你要做z*y, MATLAB将它解释为矩阵乘法,z和y被解释为矩阵。这显然不是你想要的。

However, your array of x only contains points from 0 to 2*pi. If you want to plot this from 0 to 4*pi, you have to modify your call to linspace:

然而,你的x数组只包含0到2*的点。如果你想从0到4*pi,你需要修改你对linspace的调用:

x=linspace(0, 4*pi, 100); %// Change
y=sin(x);
z=exp(-x);
plot(x, z.*y);

Now, x will contain 100 points between 0 to 4*pi. For more information on basic MATLAB operations, check out this link: http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html. What you have asked falls into the basic realms of array and matrix operations.

现在,x将包含100点在0到4*之间。有关基本的MATLAB操作的更多信息,请查看此链接:http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html。你所要求的是数组和矩阵运算的基本领域。

Edit

In the spirit of your question, we can't modify linspace. You did something clever where we can simply scale our values of x by 2 or adding with x so that we have points going from 0 to 2*pi to 0 to 4*pi. Also, if we scale our points by 2, this means that our input argument into the function must also be scaled by 2. So, the final function we need to plot is:

本着你的问题,我们不能修改linspace。你做了一些聪明的事情,我们可以简单地将x的值缩放到2或者加上x,这样我们就有了从0到2*到0到4*的点。同样,如果我们将点按2进行缩放,这意味着我们的输入参数也必须被缩放2。所以,我们要画的最后一个函数是

y = exp(-2x)*sin(2x)

Noting your hint, exp(-2x) = exp(-x-x) = exp(-x)exp(-x). Further, note that sin(2x) performs a compression by a factor of 2 (tip of the hat goes to knedlsepp for noticing my blunder). Due to the periodic nature of sin(x), we know that elements will repeat after 2*pi, and so if you want to go to 4*pi, simply subsample y by a factor of 2 and then append these same elements to a new vector. Therefore, our expression for the function simplifies to:

注意你的提示,exp(-2x) = exp(-x-x) = exp(-x)exp(-x)。此外,请注意,sin(2x)执行压缩的因子为2(由于注意到我的错误,这顶帽子的顶端被压在了膝盖上)。由于sin(x)的周期性质,我们知道元素会在2*pi之后重复,所以如果你想要到4*pi,简单地把y减去一个因子2然后将这些相同的元素附加到一个新的向量中。因此,函数的表达式简化为:

y = exp(-x)exp(-x)sin(2x)

This leads to the answer alluded to knedlsepp:

这就引出了关于knedlsepp的回答:

plot(x+x, z.*z.*[y(1:2:end) y(1:2:end)]);

As such, you should consider changing your edits to match this answer instead. It isn't quite right with respect to the sin(x) part in your code.

因此,您应该考虑更改您的编辑以匹配这个答案。对于代码中的sin(x)部分,它并不完全正确。