Problem regarding my function

Here I was trying find out various values of created functions for values of t. But there seems to be an error please help.
The error,
"Array indices must be positive integers or logical values.
Error in nana>myfunc1 (line 20)
er = e(i);
Error in nana (line 6)
a = myfunc1(fs*t + 1);"
My code:
f = 200;
fs = 100*f;
t = [0:1/fs:40];
a = myfunc1(fs*t + 1);
b = myfunc2(fs*t + 1);
disp(a);
disp(b);
plot(t,a,t,b)
function er = myfunc1(i)
f=200; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
time=0:1/fs:40; % time vector
e=zeros(size(time));
e(1:fs/f:end)=1;
er = e(i);
end
function edotr = myfunc2(j)
f=200; %frequency of the impulse in Hz
fs=f*100; % sample frequency is 10 times higher
time=0:1/fs:40; % time vector
e=zeros(size(time));
e(1:fs/f:end)=1;
edot = diff(e);
eedot = length(time);
eedot = edot;
eedot(length(time)) = edot(length(time) - 1);
edotr = eedot(j) ;
end

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 2 月 13 日

0 投票

You have an indexing problem. You input to myfunc1 is used to index e. Your calculate it as fs*t + 1. The result is a 1x800001 vector of numbers, and only every 200th number is an integer. You can't use decimal numbers to index an array.
e=1:5;
% Correct
e(2)
ans = 2
% Incorrect
e(2.3)
Array indices must be positive integers or logical values.

カテゴリ

製品

リリース

R2020b

質問済み:

2021 年 2 月 13 日

回答済み:

2021 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by