Index exceeds the number of array elements. Index must not exceed 1
조회 수: 6 (최근 30일)
이전 댓글 표시
% f_rs
N=2;
w1=@(x) (1-x.^2).^(-0.5);
w2=@(t) (1-t.^2).^(-0.5);
f=@(x,t) (((x+1)/2)*sin((t+1)/2)-(1/4)*((x+1)/2)^5+(1/4)*((x+1)/2)^5*cos((t+1)/2)-(1/4)*((x+1)/2)^2*(sin((t+1)/2))^2);
for r=1:N+1
for s=1:N+1
SS=0;
for m=1:N+1
for n=1:N+1
SS=SS+ f(x(m),t(n)).*chebyshevT(r-1,x(m)).*chebyshevT(s-1,t(n)).*w1(m).*w2(n);
end
end
f_rs(r,s)=double(SS)
end
end
댓글 수: 0
답변 (1개)
the cyclist
2023년 3월 5일
Your code gives a different error than what you are asking about, but I guess your problem comes from the expression:
x(m)
inside
chebyshevT(r-1,x(m))
where I'm guessing you definitely a variable x, but then are indexing into it.
For future reference, it is best to upload the exact code that is giving the problem, and the exact error message (including line number) that is giving the error. That way, we don't have to guess at what is happening.
댓글 수: 1
Walter Roberson
2023년 3월 5일
Even before that, the x(m) in f(x(m),t(n)) would trigger the problem.
However, if we look further we can see that the same expression invokes w1(m) and w2(n) . w1 was defined as an anonynous function that treats its parameter locally as-if named x and w2 treates its parameter as-if named t . So we can speculate that x should be m and t should be n and that x and t should not be indexed .
But... if we examine the w1 and w2 code more closely, they appear to have been written for continuous x and continuous t, and so it is not obvious the the discrete integers m and n should be passed into them. Perhaps there should be a vector x and a vector t defined before this point, and perhaps the w1(m) should be w1(x(m)) and the w2(n) call should be w2(t(n))
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!