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

Andrew code is much much faster and clean, use his code!
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 Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by