Any idea on how to create a n by n square matrix.

The size of the matrix follows the npanels=3
Let say for this case it is a 3 by 3 square matrix but if i want to create a 7 by 7 square matrix or a 15 by 15 square matrix I will have to repeat the procedure many times. I want MATLAB to be able to auto create a n by n square matrix by only changing the value of npanels.
Vector P,Vector x and Vector y are row vectors with 3 elements which is the value of npanels=3.
npanels=3; %number of panels
dP=2.4/npanels; %equal length panels
P=[-1.2+(2.4/npanels)/2:dP:1.2-(2.4/npanels)/2]; %panels x-ccordinates
a=1.25; %major axis
b=0.75; %minor axis
O=[pi-pi/(2*npanels):-pi/(npanels):pi/(2*npanels)]; %boundary points
r=(a*b)./sqrt((b*cos(O)).^2+(a*sin(O)).^2); % ellipse in polar coordinates
x=r.*cos(O); %x-coordinate
y=r.*sin(O); %y-coordinate
C1j=(y(1).*dP)./((x(1)-P).^2+(y(1).^2));
C2j=(y(2).*dP)./((x(2)-P).^2+(y(2).^2));
C3j=(y(3).*dP)./((x(3)-P).^2+(y(3).^2));
C=[C1j;C2j;C3j]

 採用された回答

Paulo Silva
Paulo Silva 2011 年 5 月 9 日

2 投票

v=1:npanels;
C=reshape(cell2mat(arrayfun(@(v)(y(v).*dP)./((x(v)-P).^2+(y(v).^2)),...
v,'uni',false)),npanels,npanels)';

2 件のコメント

Paulo Silva
Paulo Silva 2011 年 5 月 9 日
Andrew code is much much faster and clean, use his code!
Andrew Newell
Andrew Newell 2011 年 5 月 9 日
You're very kind!

サインインしてコメントする。

その他の回答 (2 件)

Andrew Newell
Andrew Newell 2011 年 5 月 9 日

2 投票

Use meshgrid:
npanels=3; %number of panels
dP=2.4/npanels; %equal length panels
P=[-1.2+(2.4/npanels)/2:dP:1.2-(2.4/npanels)/2]; %panels x-ccordinates
a=1.25; %major axis
b=0.75; %minor axis
O=[pi-pi/(2*npanels):-pi/(npanels):pi/(2*npanels)]; %boundary points
[P,O] = meshgrid(P,O);
r=(a*b)./sqrt((b*cos(O)).^2+(a*sin(O)).^2); % ellipse in polar coordinates
x=r.*cos(O); %x-coordinate
y=r.*sin(O); %y-coordinate
C = y*dP./((x-P).^2 + y.^2)
Yoon Hong Ng
Yoon Hong Ng 2011 年 5 月 9 日

1 投票

Thank you Mr. Newell and Mr.Silva.

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by