Subdividing intervals from unequally spaced numbers

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)

Réponses (1)

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 commentaires

Angelo
Angelo le 12 Juil 2023
Many thanks.
The x variable dimesion is 11, the fixed amount of points in linspace. How to accumulate in a single variable all the points generated from each subinterval? Is it possible to increase the x dimension? From the output it looks like x points come only from the last interval [-0.2588 -0.7071]. I need all together the points generated from each subinterval.
Voss
Voss le 12 Juil 2023
Modifié(e) : Voss le 12 Juil 2023
n = 5;
xn_ch = cos(pi * (2 * (1:n) - 1) ./ (2 * n + 2))
xn_ch = 1×5
0.9659 0.7071 0.2588 -0.2588 -0.7071
How to accumulate in a single variable all the points generated from each subinterval?
One way is to put them in a matrix:
npts = 11;
xch = zeros(npts, n-1);
for i = 1:n-1
xch(:,i) = linspace(xn_ch(i), xn_ch(i+1), npts);
end
disp(xch)
0.9659 0.7071 0.2588 -0.2588 0.9400 0.6623 0.2071 -0.3036 0.9142 0.6174 0.1553 -0.3485 0.8883 0.5726 0.1035 -0.3933 0.8624 0.5278 0.0518 -0.4381 0.8365 0.4830 0.0000 -0.4830 0.8106 0.4381 -0.0518 -0.5278 0.7848 0.3933 -0.1035 -0.5726 0.7589 0.3485 -0.1553 -0.6174 0.7330 0.3036 -0.2071 -0.6623 0.7071 0.2588 -0.2588 -0.7071
Angelo
Angelo le 12 Juil 2023
Modifié(e) : Angelo le 12 Juil 2023
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 le 12 Juil 2023
Déplacé(e) : Voss le 12 Juil 2023
I found this:
b = unique(b(:).')
But is it possible to solve without adding new code rows?

Connectez-vous pour commenter.

Catégories

Question posée :

le 12 Juil 2023

Déplacé(e) :

le 12 Juil 2023

Community Treasure Hunt

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

Start Hunting!

Translated by