Hello everyone, I need to plot the graph of x[n^2]. When I enter the values like n=n^2 the stem graph has 2 values on the spesific number. For example my code is
n=[-5 -4 -3 -2 -1 0 1 2 3]
x=[1 -2 3 2 1 0 9 7 2]
when I write n=n^2 the -3 and 3, -2 and 2, -1 and 1 values are located on each other. How can I solve this problem? Thanks.

 Réponse acceptée

Walter Roberson
Walter Roberson le 14 Nov 2020

0 votes

n=[-5 -4 -3 -2 -1 0 1 2 3];
x=[1 -2 3 2 1 0 9 7 2];
stem(x, n.^2)
This is correct output for n^2 vs x, because you have duplicate x values.
stem3(1:length(x), x, n.^2); xlabel('t'); ylabel('x'); zlabel('n^2')

9 commentaires

enrique128
enrique128 le 15 Nov 2020
Modifié(e) : enrique128 le 15 Nov 2020
sir shouldn't it be like stem(n.^2,x) ?
and how can it be possible I mean 3 dimensional graph. It is discrete time signal, I know we have duplicate values but there should be a trick like scaling with n or something like that.
Walter Roberson
Walter Roberson le 15 Nov 2020
Why would it be stem(n.^2, x) ? the first parameter is to be the independent variable, which is traditionally labeled "x".
Any coordinate that you are doing polynomial scaling on is not an independent variable.
If you intend n.^2 to be your indepedent variable, then draw a mock-up of what you want the outcome to look like.
enrique128
enrique128 le 15 Nov 2020
Modifié(e) : enrique128 le 15 Nov 2020
sir can you look this link:
I want to do it like that not a 3 dimensional graph
and also my real signal is stem(n,x) because n is -5 -4 -3 -2 -1 0 1 2 3 that's the reason why I said n must be in the beggining of stem command.
I had to invent new x data because (-5-1)^2 -> 36 and x[36] did not exist
n=[-5 -4 -3 -2 -1 0 1 2 3];
x=[1 -2 3 2 1 0 9 7 2 -10:-1:-37];
stem(n, x((n-1).^2+1)) %the +1 is to move from 0 indexing to 1 indexing
enrique128
enrique128 le 15 Nov 2020
Modifié(e) : enrique128 le 15 Nov 2020
sir the link in the algorithm is not like that, for example assume that we have
n=[-3 -2 -1 0 1 2 3 4];
x=[0 0 1 1 1 1 1/2 1/2];
-3^2=9, there is no value at n=9 so there should be 0 in -3.
-2^2=4, there is a value at 4 which is 1/2 so there should be 1/2 in -2.
-1^2=1, there is a value at 1 which is 1 so there should be 1 in -1.
0^2=0, there is a value at 0 which is 1 so there should be 1 in 0.
1^2=1, there is a value at 0 which is 1 so there should be 1 in 1.
2^2=4, there is a value at 4 which is 1/2 so there should be 1/2 in 2.
3^2=9, there is no value at n=9 so there should be 0 in 3.
4^2=16, there is no value at n=16 so there should be 0 in 4.
so my signal become
n=-3 -> 0
n=-2 >1/2
n=-1 > 1
n=0 > 1
n=1 >1
n=2 >1/2
n=3 -> 0
n=4 -> 0
The algorithm is like that.
n = [-3 -2 -1 0 1 2 3 4];
x = [0 0 1 1 1 1 1/2 1/2];
t = n.^2;
mask = t >= 1 & t <= length(x) & fix(t) == t;
y = zeros(size(t));
y(mask) = x(t(mask));
stem(n, y)
enrique128
enrique128 le 15 Nov 2020
Modifié(e) : enrique128 le 15 Nov 2020
sir the values that i write is not the same as your graph it should be like 0 1/2 1 1 1 1/2 0 0. The time interval is true but the values are wrong
n = [-3 -2 -1 0 1 2 3 4];
x = [0 0 1 1 1 1 1/2 1/2];
y = interp1(n, x, n.^2, 'linear', 0);
stem(n, y)
enrique128
enrique128 le 15 Nov 2020
you are a hero! thanks sir.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by