I have very little knowledge about Matlab and need some input. Here is my code. I keep getting an error on line 27. Index exceed the number of array elements. I understand a little bit on why im getting this error, but I do not know how to fix it.
L = 1; %m
a = 0.001; %m
VO = 1.0; %V
Eo = 8.8541e-12;
N = 5 ; % number of segments
d = 0.05; %m
Delta = L/N;
prompt ='Enter the value for d ';
d = input (prompt)
% Matrix A
y = Delta/2:Delta:L-Delta/2;
B=N*2;
c=N*2;
A = zeros(B,c);
for n = (1:N)*2
for m = (1:N)*2
if m == n
A(n,n) = 2*log(Delta/a);
else
A(n,m) = Delta/abs(y(m)-y(n));
if n==m
A(n,n) = 2*log(Delta/sqrt((y(n)-y(m))^2+(d)^2));
else
A(n,m) = Delta/abs(y(m)-y(n));
end
end
end
end
disp(n);
disp(m);
disp(A);
% Vector B
b = 4*pi*Eo*(VO/2)*[-ones(1,N) ones(1,N)].';
%rho = inv(A)*b;
rho = A\b;
% Capacitance calculation
Ql = sum(rho([1:N]))*Delta;
Qu = sum(rho([N+1:2*N]))*Delta;
C = Qu/VO;
disp(C);
%figure (1), clf;
%plot(y,rho,'*--'); grid on;
%Plot rho against Y
xlabel('y_n');
ylabel('Charge Density (C/m');
title ('Calculation of Charge Density');

1 件のコメント

Abhishek Gangwar
Abhishek Gangwar 2020 年 7 月 19 日
You are getting this error because y is a 1x5 row vector and the index values (n, m) in nested loops are all even numbers [2, 10], try to modify your code.

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

 採用された回答

KSSV
KSSV 2020 年 7 月 19 日

0 投票

Size of y is 1*N where as size of A is 2N*2N. You need to increase the size of y. You can replace y with:
y = linspace(Delta/2,L-Delta/2,2*N) ;

3 件のコメント

Tristan McCarver
Tristan McCarver 2020 年 7 月 19 日
Thank you for the help! I no longer get the error. I do have another question though. When I run the code I can populate a 5X5 matrix but the rest are 0 in the 10X10 matrix of A. How do I increase n and m to fill up the rest of the matrix?
Tristan McCarver
Tristan McCarver 2020 年 7 月 19 日
Ok I believe I have figured that out. The plot is suppose to look like a parabola it is coming out wrong.
Tristan McCarver
Tristan McCarver 2020 年 7 月 19 日
Here is what im trying to make my A matrix look like.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by