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.

 採用された回答

Walter Roberson
Walter Roberson 2020 年 11 月 14 日

0 投票

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 件のコメント

enrique128
enrique128 2020 年 11 月 15 日
編集済み: enrique128 2020 年 11 月 15 日
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 2020 年 11 月 15 日
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 2020 年 11 月 15 日
編集済み: enrique128 2020 年 11 月 15 日
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.
Walter Roberson
Walter Roberson 2020 年 11 月 15 日
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 2020 年 11 月 15 日
編集済み: enrique128 2020 年 11 月 15 日
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.
Walter Roberson
Walter Roberson 2020 年 11 月 15 日
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 2020 年 11 月 15 日
編集済み: enrique128 2020 年 11 月 15 日
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
Walter Roberson
Walter Roberson 2020 年 11 月 15 日
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 2020 年 11 月 15 日
you are a hero! thanks sir.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by