Plotting sequences over intervals
古いコメントを表示
Hello,
How would I go about plotting a seqeunce over the below intervals?
A) 𝑥(𝑛) = 2𝛿(𝑛 + 1)− 𝛿(𝑛 −6), −10 ≤ 𝑛 ≤ 10.
B) 𝑥(𝑛) = {… , 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, … }; −20 ≤ 𝑛 ≤ 12.
Thanks
6 件のコメント
Connor Wright
2020 年 11 月 13 日
Jon
2020 年 11 月 13 日
This looks like a homework problem. Please attempt to solve the problem, and then once you are having specific problems with the MATLAB code you have written ask about these problems. If you don't know how to get started please get help from your professor or TA
Connor Wright
2020 年 11 月 13 日
Jon
2020 年 11 月 13 日
Sorry you haven't been able to get any help. Maybe I can try to get you started.
Let's start with problem a.
First note that the unit sample function, delta(arg) has a value of 1 when arg = 0 and otherwise is zero. So for example delta(0) = 1 delta(-3) = 0, delta(4) = 0 etc and delta(n) = 1 when n = 0 and delta(n) = 0 when n is not equal zero. You can also shift the argument relative to n, so delta(n + 1) = 1 when n = - 1 (because the argument n+1 =0 when n = -1) and so on. So now look at your series and start plugging in values for n starting at n = -10. x(-10) = 2*delta(-10+1) - delta(-10 -6) So x(n) = 2*delta(-9) -delta(-16) ,x(n) = 2*0 -0 x(n) = 0 Now repeat for n = -9
x(-9) = 2*delta(-8) - delta(-15) , x(-9) = 0 and so on. Some terms will finally be non zero
Now think about how you would automate this is MATLAB, you could use a for loop like this
n = -10:10
x = zeros(length(n),1) % preallocate array of zeros to hold result
for k = 1:length(n)
arg1 = n(k) + 1
if arg1 == 0
delta1 = 1
else
delta1 = 0 % 2*0
end
arg2 = n(k) - 6
if arg2 == 0
delta2 = 1
else
delta2 = 0
end
% compute x for this element in seqence
x(k) = 2*delta1 - delta2
end
% plot result
stem(n,x)
But this isn't very good coding practice because we shouldn't cut and paste the same code twice. Otherwise you might fix something in the first part and forget to fix it in other, plus it has lots of extra lines to read. Instead you could define a unit sample function and do it like this
n = -10:10
x = zeros(length(n),1) % preallocate array of zeros to hold result
for k = 1:length(n)
% evaluate x(n)
x(k) = 2*usmpl(n(k)+ 1) - usmpl(n(k)-6);
end
% plot result
stem(n,x)
% define helper function
function delta = usmpl(n)
% define unit sample function
if n == 1
delta = 1
else
delta = 0
end
end
Connor Wright
2020 年 11 月 16 日
Jon
2020 年 11 月 19 日
I define it as a helper function in my script above
採用された回答
その他の回答 (5 件)
SaiDileep Kola
2020 年 11 月 16 日
0 投票
Hi Connor,
Start by copying complete code provided by Jon and usmpl is just a local function created to use. Don't use usmpl in the terminal without defining, that will solve your issue.
7 件のコメント
Connor Wright
2020 年 11 月 16 日
SaiDileep Kola
2020 年 11 月 16 日
That is already defined, it is just a local function.
Copy everything in to a new matlab file and execute it.
You can replace "usmpl", with some "qwerty", it shouldn't change anything.
Connor Wright
2020 年 11 月 16 日
Jon
2020 年 11 月 16 日
Hi thanks to SaiDileep Kola for helping Connor with this.
I'm glad you finally got it running without errors. You can run the code as a live script, or just as a plain script (just save it as a .m file.
Note I tried to make the example self contained by including the function definition for the unit sample function (usmpl) inside of the script. If you want to make it available to other scripts, or at the command line then you can cut and paste that function definition into it's own m file. As long as the location for that file is on the MATLAB search path it will then be available to use on the command line or within another script or function. Note if it is saved in the folder from which you are currently running MATLAB it will already be on the path.
When you say the graph does not look as you expect, maybe you can be more specific. Note I just quickly provided the code to give you an example to gety you started on how to think about this going about how to do this. I didn't carefully check to see that the answer was correct. You should check the details of the code to make sure that the calculations make sense to you, and possible correct any errors if you find them.
Hi Connor, I think actually my answer rather than SaiDileep actually answered your question, (accepted answer) SaiDileep only explained how to get my code to run by copying and pasting it into a file. It probably would have been better if SaiDileep had put this as a comment to the orignal answer rather than as a new answer to avoid confusion. Probably would be easier for someone trying to follow this thread if the accepted answer was the one that had the original details in it. Also although I guess it may be a little petty I felt like I had put quite a bit of effort into getting you started on this when you sounded so frustrated, and I guess getting an answer accepted is a nice form of recognition.
Connor Wright
2020 年 11 月 19 日
Jon
2020 年 11 月 19 日
Oops now I feel kind of silly, didn't realize I had just continued my original comment. I will try to copy and paste into an answer.
Noor Hossen
2021 年 7 月 18 日
編集済み: Noor Hossen
2021 年 7 月 18 日
0 投票
(A).
Step 1. At first, needed to make a script file of delta function. It would be

Step 2. Then write another on script file following the problem statement

Output-----------

Md. Monirul Islam
2022 年 10 月 30 日
0 投票
δ(n+2)
Md. Monirul Islam
2022 年 10 月 30 日
0 投票
δ(n+2)
カテゴリ
ヘルプ センター および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
