필터 지우기
필터 지우기

Subdividing intervals from unequally spaced numbers

조회 수: 3 (최근 30일)
Angelo
Angelo 2023년 7월 12일
이동: Voss 2023년 7월 12일
Hi,
i want to generate points (say 11, variable xch) for a set of intervals defined by unequal spaced numbers (variable xn_ch). It seems that the code generates the points (xch) but with error, because xch vector dimension should not exceed the dimension of the initial generated numbers (xch, size=5). How to solve? Thanks in advance.
This is the code:
n=5
for i=1:n
xn_ch(i)=cos(pi*(2*i-1)/(2*n+2));
end
for i=1:n
xch=linspace(xn_ch(i),xn_ch(i+1),11)
end
xn_ch
This is the error:
Index exceeds the number of array elements. Index must not exceed 5.
Error in code (line 6)
xch=linspace(xn_ch(i),xn_ch(i+1),11)

답변 (1개)

Manan Jain
Manan Jain 2023년 7월 12일
Hi!
The error you're encountering occurs because the loop index i in the second loop exceeds the range of valid indices for the xn_ch array. In the last iteration of the loop, i becomes 6, while xn_ch has only 5 elements.
n = 5;
for i = 1:n
xn_ch(i) = cos(pi * (2 * i - 1) / (2 * n + 2));
end
for i = 1:n-1
xch = linspace(xn_ch(i), xn_ch(i+1), 11);
end
xn_ch
To solve this issue, you can modify the loop to iterate from 1 to n-1 instead of n.
I hope this helps!
  댓글 수: 4
Angelo
Angelo 2023년 7월 12일
편집: Angelo 2023년 7월 12일
It works fine. Another question, hopefully my last, there is a double counting for the last point of each interval (the first for the next), see for example the value 0.7071 in the first column, repeated in the second and so on for the other two columns. I mean it is correct the subdivision (21 point for each subinterval) but the overall number of points is 84 with three more terms (i will use the numbers as x-values for a sum containing f(x) so i have addiction terms. How can avoid this problem?
Angelo
Angelo 2023년 7월 12일
이동: Voss 2023년 7월 12일
I found this:
b = unique(b(:).')
But is it possible to solve without adding new code rows?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by