# how to overcome this error?

2 visualizaciones (últimos 30 días)
amir el 18 de Sept. de 2013
Actually,i am supposed to Vary the capacitance value from 10 μF to 50 μF with an interval of 10 μF and Plot and clearly indicate each resultant voltage across the resistor on the same figure; but i got a error which i cannot solve it. here is my codes:
t = 0:0.0001:0.07;
C = 10e-6:10e-6:50e-6;
if true
% code
end
v0 = input('please Enter the value for v0:');
R = input('please Enter the value for resistance:');
C = input('please Enter the value for capacitacne:');
f = input('please Enter the value for frequency:');
m = length(t);
state = 'on';
for A = 1:m
for n = 1:5
vs(A,n) = v0 * sin(2*pi*f*t(A));
switch state
case 'on'
vR(A,n) = vs(A,n);
iR = vR(A,n)/R;
iC = 2*pi*f*C(n)*v0*cos(2*pi*f*t(A));
I = iC + iR;
if I<=0
state = 'off';
tA = t(A);
end
case 'off'
vR(A,n) = v0*sin(2*pi*f*tA)*exp(-(t(A)-tA)/(R * C(n)));
if vs(A,n) >= vR(A,n)
state = 'on';
end
end
end
end
plot (t,vs,':',t,vR,'k')
**here is the error:
??? Attempted to access C(2); index out of bounds because numel(C)=1.
Error in ==> untitled2 at 18 iC = 2*pi*f*C(n)*v0*cos(2*pi*f*t(A));****
*thank you***
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuesta aceptada

Jan el 18 de Sept. de 2013
At first you define C as a vector:
C = 10e-6:10e-6:50e-6;
But afterwards you overwrite it most likely by a scalar:
C = input('please Enter the value for capacitacne:');
Therefore C is a scalar and C(n) must fail for n=2.
##### 2 comentariosMostrar NingunoOcultar Ninguno
amir el 18 de Sept. de 2013
thanks man...
Jan el 19 de Sept. de 2013
@amir: If this solves your problem, please accept it to signal other readers, that you do not need further assistance.

Iniciar sesión para comentar.

### Categorías

Más información sobre Programming en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by