Why do I keep getting "Array indices must be positive integers logical value"

조회 수: 1 (최근 30일)
Aarushi Ajit
Aarushi Ajit 2020년 10월 16일
댓글: Walter Roberson 2020년 10월 17일
a=0.00810
g=9.81
w=0:0.5:2
n=length(w)
for i=1:n
S(i)=((a*(g.^2)/(w(i)).^5))*exp(-0.74*(w(i)*v/g).^(-4
))
  댓글 수: 4
Aarushi Ajit
Aarushi Ajit 2020년 10월 17일
편집: Aarushi Ajit 2020년 10월 17일
a=0.00810
g=9.81
v=10
w=0:0.5:2
n=length(w)
for i=1:n
S(i)=((a*(g.^2)/(w(i)).^5))*exp(-0.74*(w(i)*v/g).^(-4))
figure;
plot(w(i),S(i));
This is the entire code that i actually needed help on. I have to plot a graph for w v/s S. Please help. This is code is still not giving me any figure or graph.
Walter Roberson
Walter Roberson 2020년 10월 17일
a=0.00810
g=9.81
v=10
w=0:0.5:2
n=length(w)
for i=1:n
S(i)=((a*(g.^2)/(w(i)).^5))*exp(-0.74*(w(i)*v/g).^(-4));
end
figure;
plot(w,S);

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

답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 10월 16일
What is value of 'v' in your case? If it is scalar, the code works fine. Run the following
a=0.00810;
g=9.81;
w=0:0.5:2;
n=length(w);
v=1;
S = zeros(1, n); % pre-allocation is a good coding practice
for i=1:n
S(i)=((a*(g.^2)/(w(i)).^5))*exp(-0.74*(w(i)*v/g).^(-4));
end

Jon
Jon 2020년 10월 16일
In general that error comes from trying to index an array with something other than a positive integer or a logical variable. So for example if I have an array x = [1,8,19.3 2] and a variable b = 43.3 and I try to index x using the non-integer value b as in x(b) I will get the error you describe.
I can't reproduce your error from what code you have included here. For example you don't define your variable v, and there is no end statement for the for loop so it won't even run as is. Even if I assign a value to v, and provide the end statement I still don't get the error you indicate. Please provide code that reproduces the error. Note that you can format the code nicely using the code button in the MATLAB answers toolbar.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by