Input on making a matrix?

조회 수: 9 (최근 30일)
Tristan McCarver
Tristan McCarver 2020년 7월 19일
댓글: Tristan McCarver 2020년 7월 19일
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일
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일
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by