logical index issue in for lloop

24 ビュー (過去 30 日間)
yogeshwari patel
yogeshwari patel 2024 年 12 月 7 日 23:44
回答済み: Walter Roberson 2024 年 12 月 8 日 0:06
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

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 12 月 8 日 0:06
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.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by