MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中调用mldivide时矩阵维度错误 (https://www.labfans.com/bbs/showthread.php?t=26744)

poster 2019-12-14 20:46

在MATLAB中调用mldivide时矩阵维度错误
 
运行代码时出现此错误:
[INDENT][INDENT]使用==> mldivide时出错矩阵尺寸必须一致。

[/INDENT][/INDENT]这是我的代码:

%make the plots of phase and group velocity vs discreteness of the grid c=1; a=input('Please enter the ratio cdt/dx : ') figure(1) R=2:40; plot(R,phase_vel(R,a)/c) xlabel('R=l/dx') ylabel('u_phase/c') %figure(2) %plot(group_vel(R,a),R,0,40) %xlabel('R=l/dx') %ylabel('u_group/c') 这是我的功能:

function phase_velocity = phase_vel(R,a) %numerical phase velocity of the discrete wave c=1; phase_velocity=(2*pi*c)/(R*knum(R,a)); end function group_velocity =group_vel(R,a ) %numerical group velocity of the discrete wave c=1; group_velocity=(a*sin(knum(R,a)))/(sin(2*pi*a/R)) end function knumber = knum(R,a) %This is the k wave number knumber=acos((1/a)^2*(cos(2*pi*a/R)-1)+1); end 如何解决此错误?

[B]编辑:[/B]我用过。每个等式中的算子,我改变了R = 4:40的极限



[B]回答:[/B]

如果您的目标是将公式应用于向量R中的每个单独的值,则应该使用[URL="http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html"]逐元素算术运算符[/URL] .* ,. ./和.^而不是矩阵运算符* , /来执行所有计算.*和^ 。

您的错误可能是在第一次调用函数knum时knum ,特别是当您尝试计算2*pi*a/R 。由于2*pi*a是单个标量值,因此在尝试执行[URL="http://www.mathworks.com/help/techdoc/ref/mrdivide.html"]矩阵右除法[/URL] /使用行向量R时会出现错误。 [I]真正[/I]奇怪的是错误消息:

??? Error using ==> mldivide Matrix dimensions must agree. 这意味着您使用的是矩阵[I]左[/I]除法运算符\ ,您显然不是。我在MATLAB R2010b中对此进行了测试,并在消息中出现了相同的错误函数名称。我认为这可能只是错误消息中的错别字,我已经给MATLAB人员留下了一条注释,以查看并清除它。



[url=https://stackoverflow.com/questions/5471458]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:20

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.