"Index exceeds matrix dimensions" error in For Loop
Show older comments
I continue to get an "Index exceeds matrix dimensions" error when running the following for loop. It says there is an error in sym/subsref (line805), although my code only goes down ~200 lines, any help is much appreciated! Here is my code
%Code
T=8;
t = -T/7:0.001:T/7;
syms t
y = 7*t.^2/(2*T);
N = 50;
k = -N:N; % vector of "k" coefficients.
ak = zeros(size(k));
omega = 2*pi/T; % frequency
dt = t(2) - t(1); % time delta
for n = 1:length(k)
if k(n) == 0
ak(n) = a0;
else
ak(n) = (1/T)*sum(y.*exp(-1i*k(n)*omega*t)*dt); % Simulates the integral of the analysis equation
end
end
end
Answers (2)
James Tursa
on 13 Oct 2016
You've got these two lines that define t differently:
t = -T/7:0.001:T/7;
syms t
What is your actual intent for t? The first line? Maybe comment out the 2nd line?
Star Strider
on 13 Oct 2016
With two changes to your code, this runs:
syms t a0 % The ‘syms’ Statement Is First
T=8;
t = -T/7:0.001:T/7;
y = 7*t.^2/(2*T);
N = 50;
k = -N:N; % vector of "k" coefficients.
ak = sym(zeros(size(k))); % <— Create ‘sym’ Object
omega = 2*pi/T; % frequency
dt = t(2) - t(1); % time delta
for n = 1:length(k)
if k(n) == 0
ak(n) = a0;
else
ak(n) = (1/T)*sum(y.*exp(-1i*k(n)*omega*t)*dt); % Simulates the integral of the analysis equation
end
end
Categories
Find more on Mathematics 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!