What does this mean? (Attempted to access Q(0); index must be a positive integer or logical.)

조회 수: 1 (최근 30일)
I am trying to use the formula Q(t ) = CV(1 − e−t/RC ) to monitor the charge on the capacitor every .2 seconds given V = 9,R = 4, C = 1, and Q at t(0) = 0. We are supposed to display the time and charge every .1 seconds until Q exceeds 8.
This is my code so far:
Q(0)=0;
V = 9; %Voltage
R = 4; %Resistor Value
C = 1; %Capacitor Value
p = 0;
t = .1; %Time
for Q = 0:8
Q(t) = C * V * (1-exp(-(p+t)/(R * C))); %Charge Function
plot (Q(t),'c+')
end
When I attempt to evaluate the code i keep getting this:
Attempted to access Q(0); index must be a positive integer or logical.
Thank you

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 10일
편집: Azzi Abdelmalek 2013년 3월 10일
In Matlab you can not use
A(0) or A(-1) or A(1.5)
index must be a positive integer or logical:
Positive integer are: 1,2,3,...
Logical are: logical(0) and logical(1)
  댓글 수: 2
Turk
Turk 2013년 3월 10일
now when I change the Q(0) to Q(1) I get:
>> Q(1)=0;
V = 9; %Voltage
R = 4; %Resistor Value
C = 1; %Capacitor Value
p = 0;
t = .1; %Time
for Q = 0:8
Q(t) = C * V * (1-exp(-(p+t)/(R * C))); %Charge Function
plot (Q(t),'c+')
end
Attempted to access Q(0.1); index must be a positive integer or logical.
How would you correct this? I am not that good at Matlab yet sadly..
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 10일
Yes, you can not use Q(0.1) because 0.1 is not positive integer

댓글을 달려면 로그인하십시오.


Daniel Shub
Daniel Shub 2013년 3월 10일
Your for loop doesn't make any sense. It is a bad idea to iterate on a variable that you then change within a for loop (in this case Q). Further, t is a non-integer value so cannot be used to index Q. Finally plot (Q(t),'c+') is probably not doing what you expect.
MATLAB comes with some really great documentation. I suggest reading it.

카테고리

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