As I understand it, the program stumbles over calling the delta function and builds one point. How can I display the desired graph without making the code too heavy?
Main script:
a1=1.7;
b1=9;
subplot(221);
n1=0:20;
x1=a1*delta(n1-b1);
stem(n1, x1);
File delta function (its use is mandatory):
function [ d ] = delta( n )
if n == 0
d = 1;
else
d = 0;
end
end

 채택된 답변

Voss
Voss 2024년 3월 16일
편집: Voss 2024년 3월 16일
Your delta function makes d a scalar. Instead d should be the same size as n, with value 1 where n is 0 and value 0 everywhere else. If you can modify the delta function, do it.
a1=1.7;
b1=9;
% subplot(221);
n1=0:20;
x1=a1*delta(n1-b1);
stem(n1, x1);
function d = delta( n )
d = zeros(size(n)); % initialize d to be an array of 0 the same size as n
d(n == 0) = 1; % set d to 1 where n is 0
end

댓글 수: 2

Thank you a lot! I got 2 good answers but this one is pretty universal for writing file-functions!
You're welcome!

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

추가 답변 (1개)

Torsten
Torsten 2024년 3월 16일
Since your delta function does not support array inputs, you must use something like
x1=a1*arrayfun(@(n1)delta(n1-b1),n1)
instead of
x1=a1*delta(n1-b1);

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2024년 3월 16일

댓글:

2024년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by