Hi,I want to select a range of elements in matrix p0(i,j), i started from (at-9) to (at+9) and j started from 1 to n, (n=20) and the value of at is known. then do calculation for p0.
The code is very ambiguous and is not even documented. However, this error comes when you divide by 0. In your code, I checked it and tried to understand its workflow. I found that, this maybe because the:
theta(i)=(i-1)*delttheta; %%%%(line 15)
since i starts with 1 the first value in theta(i) will be zero. This has effect on the variable wi, wt and wr (lines from 88 to 102) and consequently these values will be set to zero.
Therefore, the line 104
alt=atan(wt/wr);
will yield the value NaN (since trying to calculate atan(0/0) which has 0 divided by 0).
The appearance of NaN in any variable calculations will make the results NaN (e.g. 5+NaN = NaN).
going to the line 112 the variable (at) will be set to NaN, that is why the code will stop in the next iteration in line 26 as the counter i will be set to NaN and this exactly the error of Matlab:
Subscript indices must either be real positive integers or logicals.
Error in stat (line 27)
p0(i,j)=pin;
which means the array index should be positive integer or logicals but in fact it has the value NaN and therefore the error appeared.
Hope you understand this description and it help you to solve it.
The problem of your code is mathematical. For 'k = 1', there is no error, however, for 'k = 2' an error occurs. This is because of the fact that for 'k = 1', 'wi' and consequently 'wr' in line 92 and 94 become zero and 'alt' in line 105 becomes NAN (0/0)! So in line 113 the value of 'at' becomes NAN, therefore for 'k = 2' the loop starts with 'j = NAN' and 'p0(NAN,2)' cannot be defined because index of a matrix in MATLAB must be either numerical or logical but not a NAN. You should recheck your mathematical relations to find the problem that why 'wi' becomes zero.
Thanks a lot Mr.Ahmed Elazab , Mr.Saeed Lotfan for your explanation , it was very helpful.
wr=double integration (p*cos(theta)). & wt=double integration (p*sin(theta)).and the convergence criteria is alt=atan2(wt, wr) , so how can I calculate the value of wr,wt ?
I think you can add some conditions or just try to avoid dividing by zero. This can be done in very simple way by adding a very small value in the denominator to avoid dividing by zero (e.g. 0.0001).
However, you have to make sure adding such value will have no or very low effect on the logical meaning of your problem.
Mr. Ahmed I made some changes but it still don't calculate the load (i.e. the load is zero in each iteration) please see the modified .m file. also the matrix p1 is zero , I don't understand why, is there wrong in the if statement?
Excellent answers by others. You probably know this already, but I'll just say it because no one else has. NaN stands for "not a number". It has a number of interesting properties.
NAN stands for "not a number". Somewhere zero is dividing some number leading to infinite i.e. very large number. Please check your code. In computation, in place of zero you may assign a very very small number to avoid devision by zero.