【问题标题】:How can I plot the summation of several variables?如何绘制多个变量的总和?
【发布时间】:2018-11-30 22:46:06
【问题描述】:

我正在尝试可视化一个方程式,a*sin(n*pi*x*t),其中:

  • a 是一个常量
  • n 是形状模式 (1,2,...)
  • t是时候了
  • x是位移

这是我的代码:

syms n t x

a=1

S=symsum(a*sin(n*pi*x*t),n,1,10)

plot(t,S)

我收到了这个错误:

Error using plot
A numeric or double convertible argument is expected

Error in Untitled (line 8)
plot(t,subs(S,x,t))

我能做什么?

【问题讨论】:

  • 你需要解释得更好。你的问题真的不清楚你想要什么或你的变量是什么。 minimal reproducible example 对于其他人比较和调试很重要。
  • 您不能绘制符号方程,这很明显。它没有数值,对吧?

标签: matlab plot sum visualization symbolic-math


【解决方案1】:

您不必为此使用符号变量,这里有一个如何做到这一点的示例:

%% Definitions:
a = cat(3,1,-1,3);  % size(a) == [1,1,3]
n = cat(4,1,2,3,4); % size(a) == [1,1,1,4]
%% Grid for x,t
x_lims = [-2  2]; x_resolution = 50; X = linspace(x_lims(1),x_lims(2),x_resolution);
t_lims = [ 0 20]; t_resolution = 20; T = linspace(t_lims(1),t_lims(2),t_resolution);
[XX,TT] = meshgrid(X,T);
ZZ = a .* sin(pi * n .* XX .* TT); % size(ZZ) == [200,100,3,4]; Also, see note at the end
%% Plot
nA = numel(a); nN = numel(n);
figure(); subplot(nA, nN, 1);
for indA = 1:nA
  for indN = 1:nN
    subplot(nA, nN, (indA-1)*nN + indN);
    surf(XX,TT,ZZ(:,:,indA,indN),'EdgeColor','none');
    view([90,90]);
  end
end

产量:

在每个图中,t 在水平轴上,x 在垂直轴上,不同的 a 值是子图的行,不同的 n 值是列。

请注意,我在代码中使用了隐式扩展;如果您有旧的 MATLAB 版本,则必须使用 bsxfun

【讨论】:

  • 错误使用 .* 矩阵尺寸必须一致。 Exam29 中的错误(第 16 行)ZZ = a .* sin(pi * n .* XX .* TT);
  • @Hossein 正如我的笔记所说,您必须使用相当旧的 MATLAB 版本。在您看到.* 的地方需要将其更改为bsxfun(@times,operand1,operand2),所以基本上类似于bsxfun(@times,a,sin(pi * bsxfun(@times,n,....) )。如果您无法自行解决此问题,请给我留下另一条评论。
【解决方案2】:

您可以使用plot 来绘制符号变量。

解决此问题的一个简单方法是使用ezplot 而不是plot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 2021-08-31
    • 2018-10-31
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 2023-04-04
    相关资源
    最近更新 更多