Input on making a matrix?
9 次查看(过去 30 天)
显示 更早的评论
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
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
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) ;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!