Problem regarding my function
Show older comments
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
Answers (1)
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)
% Incorrect
e(2.3)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!