Input on making a matrix?
9 views (last 30 days)
Show older comments
Tristan McCarver
on 19 Jul 2020
Commented: Tristan McCarver
on 19 Jul 2020
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 Comment
Abhishek Gangwar
on 19 Jul 2020
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.
Accepted Answer
KSSV
on 19 Jul 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!