Array indices must be positive integers or logical values.

1 view (last 30 days)
Hi, I really need help with this program, it does not take Nn(i) at all, and the for code it says that "parse error" over and over, can anyone help me with this program?
Nn=1:2:30;
Nn(i)=60;
Tn=0.5*10^-3;
B0=1*10^3;
Lambda=100;
Cn=2.7;
for i=1, i<70, i++
for n=1, n<16,n++
%Nn(i) for Nn
{
Dr(n)=(Lambda/Nn(i)*B0*(1-qfunc(Nn(i)*Tn*B0*Cn-Lambda+log*Nn(i)*Tn*B0/2)/log(e)*sqrt(Nn(i)*Tn*B0)));
end}
Dr_s (i)=sum(Dr(n));
end
}
Figure;
Plot(Nn(i), Dr_s);

Answers (2)

Steven Lord
Steven Lord on 5 Mar 2021
Nn=1:2:30;
This is valid MATLAB code.
Nn(i)=60;
As long as i has been defined as an array containing only positive integer values or a logical array before this line, this too is valid MATLAB code. If it hasn't been defined as an array this attempts to assign to the sqrt(-1) element of NN and that is not a valid operation in MATLAB.
Tn=0.5*10^-3;
B0=1*10^3;
Lambda=100;
Cn=2.7;
Valid MATLAB code.
for i=1, i<70, i++
Not valid MATLAB code. This looks like something close to a C or C++ for loop.
And there is no ++ operator in MATLAB. Two plus signs next to one another can be valid (++1 is just 1 due to two calls to uplus, 2++3 is 5 because it's a call to uplus for +3 then a call to plus to add together 2 and 3) but it's not usually needed.
for n=1, n<16,n++
%Nn(i) for Nn
{
Again, this is closer to C than MATLAB. For loops in MATLAB do not have their body delimited by curly braces, they end with an end matching the for.
Dr(n)=(Lambda/Nn(i)*B0*(1-qfunc(Nn(i)*Tn*B0*Cn-Lambda+log*Nn(i)*Tn*B0/2)/log(e)*sqrt(Nn(i)*Tn*B0)));
end}
Dr_s (i)=sum(Dr(n));
end
}
Figure;
The command to open a figure window in MATLAB is figure. The case matters.
Plot(Nn(i), Dr_s);
Similarly the function usually used to create a line plot is plot not Plot.
Since I'm guessing you're new to MATLAB, I suggest you start with the free MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.

Jan
Jan on 5 Mar 2021
Edited: Jan on 5 Mar 2021
Nn = 1:2:30;
Nn(i) = 60;
Without a further definition, i is a constant with the value sqrt(-1). So which element to you want to set to 60?
for i=1, i<70, i++
This is not Matlab but a kind of C.
Matlab's FOR loops look like this:
for 1 = 1:69
Curly braces are used to define cell arrays in Matlab. You cannot inlcude a code block in them.
Matlab is case sensitive. The commands are called "figure" and "plot" without uppercase characters.
I strongly recommend to read the instructions for the fundamental syntax of Matlab: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
Youz cannot simply guess, how this language works.
For future question post the complete error message, not a vague description of it.
Without comments, the reader has to guess, what the intention of the code is. A wrong guess can be more confusing than positing no code. But I dare:
Nn = 1:2:30;
Tn = 0.5e-3;
B0 = 1e3;
Lambda = 100;
Cn = 2.7;
for i = 1:69
for n = 1:15
Dr(n) = (Lambda / Nn(i) * B0 * (1 - qfunc(Nn(i) * Tn * B0 * Cn ...
- Lambda + log * Nn(i) * Tn * B0 / 2) / log(e) * ...
... % ^^^ ??? ^^^^^^
sqrt(Nn(i) * Tn * B0)));
end
Dr_s(i) = sum(Dr);
end
figure;
plot(Nn, Dr_s);
I cannot guess, what "log" and "log(e)" is.

Categories

Find more on Argument Definitions 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!