MATLAB - 具有自变量的矩阵函数?

发布于 2024-10-16 01:58:02 字数 381 浏览 1 评论 0原文

我正在尝试创建一个函数,该函数返回一个包含变量“l”的矩阵,该变量是一个独立变量,稍后将被扫描以绘制绘图。

我会根据用户输入(包括“n”和“d”)计算“phi”,然后使用“n”、“d”和“phi”来查找“a”、“b”、“c”,和“d”来创建矩阵“m”。该矩阵“m”将是“l”的函数。

phi = 2*pi*n*d/l;
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

与 C++ 和 Python 相比,我真的不喜欢 MATLAB 的编码风格……你们会如何实现这个功能?

摘要:我想要一个返回矩阵的函数,该矩阵包含稍后要扫描的自变量。

I'm trying create a function that returns a matrix containing a variable "l" which is an independent variable to be swept for a plot later on.

I would calculate "phi" based on user inputs which include "n" and "d", then I would use "n", "d", and "phi" to find "a", "b", "c", and "d" to create a matrix "m" with. This matrix "m" will be a function of "l".

phi = 2*pi*n*d/l;
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

I'm really not enjoying MATLAB's coding style as compared to C++ and Python... How would you guys implement this functionality?

Summary: I want a function that returns a matrix which contains an independent variable to be swept for a plot later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清音悠歌 2024-10-23 01:58:02

你可以有两种选择。

1) 创建一个函数,返回基于 n,d,l 的矩阵

BuildM = @(n,d,l)[cos((2*pi*d*n)/l),(sin((2*pi*d*n)/l)*i)/n;n*sin((2*pi*d*n)/l)*i,cos((2*pi*d*n)/l)];

BuildM(4,2,100)  %ans=[0.8763,0.1204i;1.9270i,0.8763]

2) 使用符号工具箱(如果可能)

syms n,d,l
phi = 2*pi*n*d/l;
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

subs(m,{'n','d','l'},{4,2,100})  %ans=[0.8763,0.1204i;1.9270i,0.8763]

You can two options.

1) Create a function which returns the matrix based on n,d,l

BuildM = @(n,d,l)[cos((2*pi*d*n)/l),(sin((2*pi*d*n)/l)*i)/n;n*sin((2*pi*d*n)/l)*i,cos((2*pi*d*n)/l)];

BuildM(4,2,100)  %ans=[0.8763,0.1204i;1.9270i,0.8763]

2) Use the symbolic toolbox (if possible)

syms n,d,l
phi = 2*pi*n*d/l;
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

subs(m,{'n','d','l'},{4,2,100})  %ans=[0.8763,0.1204i;1.9270i,0.8763]
ら栖息 2024-10-23 01:58:02

您的意思是使用符号工具箱吗?

如果是这样,我想您想:

phi = 2*pi*n*d/sym('l');
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

顺便说一句,您是否知道 d 的别名?这是故意的吗?

Do you mean to use the symbolic toolbox?

If so, I guess you want:

phi = 2*pi*n*d/sym('l');
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

And as a little aside, are you aware of your aliasing of d? Is that intentional?

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