开发者

MATLAB - matrix function with an independent variable?

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.


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]


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜