[英]How to plot this function in MATLAB?
I have a simple function below (I omitted the allocations, etc. for brevity) that I have been tryig to plot against it's x value for specific values of N and T but I keep getting a dimensions error. 我下面有一个简单的函数(为简便起见,省略了分配等),我一直尝试用它针对N和T的特定值针对它的x值进行绘制,但是我一直遇到尺寸错误。 I think that when I try to plot this I am defining an array for x and then plotting Psum(N', x, T') for certain N' and T' against these x, however MATLAB doesn't seem to like this.
我认为,当我尝试绘制此图形时,我正在为x定义一个数组,然后针对这些x绘制某些N'和T'的Psum(N',x,T'),但是MATLAB似乎并不喜欢这样。 Can someone give me some direction please.
有人可以给我一些指示吗。
function U = Psum(N, X, T)
for m = 1:N
A(1,m) = (1/(m*pi))*sin(m*pi*X)*T*exp(-(m^2)*(pi^2)*T);
% array terms of partial sum
end
M = -sum(A); % evaluate Nth partial sum
U = T*(1-X) + M; % output U(X,T) = T(1-X) + V(X,T)
end
I'm getting a similar error when I try to plot the following, I think there must be something wrong with my general approach 当我尝试绘制以下内容时,我遇到了类似的错误,我认为我的一般方法一定存在问题
syms x;
f = @(x)((x/(100*pi))*(exp(-(100^2)*(pi^2)*x)));
x = 0:0.1:10000;
plot(x,f(x),'r')
title('PartialSum convergence');
xlabel('T');
ylabel('a_n');
the error I get here reads: 我在这里得到的错误是:
Error using *
Inner matrix dimensions must agree.
Here's the analysis of why you're getting a dimension mismatch error. 这是为什么会出现尺寸不匹配错误的分析。 From this line:
从这一行:
A(1,m) = (1/(m*pi))*sin(m*pi*X)*T*exp(-(m^2)*(pi^2)*T)
The element A(1, m)
is supposed to be a scalar value in a two-dimensional matrix. 假设元素
A(1, m)
是二维矩阵中的标量值。 Now let's see what are the dimensions of each of the multiplicands: 现在,让我们看看每个被乘数的维数是多少:
(1/(m*pi))
is a scalar (that is, a 1×1 matrix). (1/(m*pi))
是标量(即1×1矩阵)。 sin(m*pi*X)
has the same dimensions as X
. sin(m*pi*X)
具有与X
相同的尺寸。 Let's assume its dimensions are q×n. exp(-(m^2)*(pi^2)*T)
has the same dimensions as T
, and is multiplied by T
. exp(-(m^2)*(pi^2)*T)
具有与T
相同的尺寸,并乘以T
Therefore T
must be a square matrix, so let's assume its dimensions are p×p. T
必须是一个方矩阵,因此我们假定其尺寸为p×p。 What we get is aq×n matrix multiplied by a square p×p matrix, and the result must be a scalar (that is, 1×1 matrix). 我们得到的是aq×n矩阵乘以一个正方形p×p矩阵,结果必须是标量(即1×1矩阵)。 This forces q=1 and n=p.
这使q = 1和n = p。
Now let's look at this line: 现在让我们看一下这一行:
U = T*(1-X) + M
We are forced to conclude that p=1, otherwise T
cannot be multiplied by X
from the right. 我们被迫得出p = 1的结论,否则
T
不能从右边乘以X
This means that your code forces T
and X
to be scalar! 这意味着您的代码强制
T
和X
为标量! No wonder you're getting a error :) 难怪你会得到一个错误:)
The remedy is simple: revise the computation in Psum
so that it can produce correct results for both a scalar X
and a vector X
. 补救方法很简单:修改
Psum
的计算,以便它可以对标量X
和向量X
产生正确的结果。 A possible fix would be adding another loop to iterate over all values of X
: 一个可能的解决方案是添加另一个循环来迭代
X
所有值:
function U = Psum(N, X, T)
U = zeros(size(X));
for k = 1:numel(X) %// Iterate over all values of X
for m = 1:N
A(1,m) = (1/(m*pi))*sin(m*pi*X(k))*T*exp(-(m^2)*(pi^2)*T);
%// array terms of partial sum
end
M = -sum(A); % evaluate Nth partial sum
U(k) = T*(1-X(k)) + M; % output U(X,T) = T(1-X) + V(X,T)
end
end
The output of this function has the same dimensions as X
. 该函数的输出具有与
X
相同的尺寸。
By the way, did you verify that Psum
produces that correct result for scalar inputs? 顺便说一句,您是否已验证
Psum
对标量输入产生了正确的结果?
I don't fully understand what you are trying to accomplish, but just an observation for you: if your input X is a vector, line 3 can not be computed correctly 我不完全理解您要完成的工作,只是对您的观察:如果您的输入X是向量,则无法正确计算第3行
A(1,m) = (1/(m*pi))*sin(m*pi*X)*T*exp(-(m^2)*(pi^2)*T);
because the right hand side of the equation give you a vector, but the right hand side A(1,m)
is one element, not vector. 因为方程式的右侧为您提供了一个向量,但是右侧
A(1,m)
是一个元素,而不是向量。 so you have dimension mismatch. 因此您的尺寸不匹配。 Hope this helps!
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.