在 MATLAB 中调用 mldivide 时出现矩阵维数错误
我在运行代码时收到此错误:
<块引用>使用 ==> 时出错mldivide 矩阵维度必须一致。
这是我的代码:
%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
如何解决此错误?
编辑:我使用了 .每个方程中的运算符,我更改了 R=4:40 的限制
I am getting this error while running my code:
Error using ==> mldivide Matrix dimensions must agree.
Here is my code :
%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')
and here are my functions :
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
How can I resolve this error?
EDIT: I used . operator in every equation and i changed the limits of R=4:40
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是将公式应用于向量
R
中的每个单独值,那么您应该使用 逐元素算术运算符.*
、./
和.^
而不是矩阵运算符*
、/
和^
。您的错误可能发生在第一次调用函数
knum
时,特别是当您尝试计算2*pi*a/R
时。由于2*pi*a
是单个标量值,因此在尝试执行 使用行向量R
进行矩阵右除/
。 真正奇怪的是错误消息:这意味着您正在使用矩阵左除法运算符
\
,但显然您没有使用。我在 MATLAB R2010b 中对此进行了测试,并且我的消息中出现了相同的错误函数名称。我认为这可能只是错误消息中的拼写错误,我已向 MATLAB 人员发送了一条注释,请他们查看并清除它。If your goal is to apply your formulas to each individual value in the vector
R
then you should be performing all of your computations using the element-wise arithmetic operators.*
,./
, and.^
instead of the matrix operators*
,/
, and^
.Your error is probably occurring in the first call to your function
knum
, specifically when you try to compute2*pi*a/R
. Since2*pi*a
is a single scalar value, you get an error when trying to perform matrix right division/
using the row vectorR
. The really weird thing is the error message:which implies you are using the matrix left division operator
\
, which you clearly aren't. I tested this in MATLAB R2010b and I get the same incorrect function name appearing in my message. I think this may just be a typo in the error message, and I've dropped a note to the MATLAB folks to take a look at it and clear it up.我没有符号数学工具箱,但您的问题似乎是您正在使用plot,这是一个可以处理数字数组的函数,并向其提供符号计算的结果。查看 Matlab 帮助,其中主题 创建绘图符号函数建议使用
ezplot()
。或者,您需要评估某些输入值的符号表达式,以创建plot
可以处理的数字数组 - 但您不能使用 double() ,因为它不知道什么数字插入你的变量。I don't have the Symbolic Math Toolbox, but your problem seems to be that you are using
plot
, a function which can deal with arrays of numbers, and feeding it the result of a symbolic calculation. Have a look at the Matlab Help, where the Topic Creating Plots of Symbolic Functions suggests usingezplot()
. Alternatively you need to evaluate your symbolic expression for certain input values to create an array of numbers thatplot
can deal with - but you can't use double() for that since it wouldn't know what numbers to plug into your variables.