logical index issue in for lloop

4 views (last 30 days)
yogeshwari patel
yogeshwari patel on 7 Dec 2024
Answered: Walter Roberson on 8 Dec 2024
syms x t real
mu=(1/16)^4
m=(cos(x)+1i*sin(x))/(1+((1/mu)^4-1)*(cos(4*t)+1i*sin(4*t)));
n=real(m)
x=0.2:0.2:0.8
t=0.1:0.4:0.9
u=zeros(1);
row=0
for i=1:length(x)
row=row+1
col=0;
for j=1:length(t)
col=col+1
u(row,col)=n(x(i),t(j))
end
end
why it is showing the below error .
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in indexing (line 936)
R_tilde = builtin('subsref',L_tilde,Idx);
According to my logic row index will be one and colum will be also one and loop will run upto length of x and t

Answers (1)

Walter Roberson
Walter Roberson on 8 Dec 2024
syms x t real
mu=(1/16)^4
mu = 1.5259e-05
m=(cos(x)+1i*sin(x))/(1+((1/mu)^4-1)*(cos(4*t)+1i*sin(4*t)));
n=real(m)
disp(char(n))
(cos(x)*(18446744073709551616*cos(4*t) + 1))/((18446744073709551616*cos(4*t) + 1)^2 + 340282366920938463463374607431768211456*sin(4*t)^2) + (18446744073709551616*sin(4*t)*sin(x))/((18446744073709551616*cos(4*t) + 1)^2 + 340282366920938463463374607431768211456*sin(4*t)^2)
So n is a scalar value.
u(row,col)=n(x(i),t(j))
But there you are asking to index n at location x(i) and t(j) . x(i) and t(j) are not integers, so you get an error about indexing at non-integers. If x(i) and t(j) did happen to be integers, you would probably have gotten an error about attempting to index out of range, since n is a scalar.
The cure is to write,
n(x,t) = real(m)
which will declare n as a symbolic function of two variables.

Categories

Find more on Operators and Elementary Operations 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!