在 MATLAB 中调用 mldivide 时出现矩阵维数错误

发布于 2024-10-27 07:23:47 字数 984 浏览 3 评论 0原文

我在运行代码时收到此错误:

<块引用>

使用 ==> 时出错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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-11-03 07:23:47

如果您的目标是将公式应用于向量 R 中的每个单独值,那么您应该使用 逐元素算术运算符 .*./.^ 而不是矩阵运算符 */^

您的错误可能发生在第一次调用函数 knum 时,特别是当您尝试计算 2*pi*a/R 时。由于 2*pi*a 是单个标量值,因此在尝试执行 使用行向量R进行矩阵右除 /真正奇怪的是错误消息:

??? Error using ==> mldivide
Matrix dimensions must agree.

这意味着您正在使用矩阵除法运算符\,但显然您没有使用。我在 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 compute 2*pi*a/R. Since 2*pi*a is a single scalar value, you get an error when trying to perform matrix right division / using the row vector R. The really weird thing is the error message:

??? Error using ==> mldivide
Matrix dimensions must agree.

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.

孤千羽 2024-11-03 07:23:47

我没有符号数学工具箱,但您的问题似乎是您正在使用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 using ezplot(). Alternatively you need to evaluate your symbolic expression for certain input values to create an array of numbers that plot can deal with - but you can't use double() for that since it wouldn't know what numbers to plug into your variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文