"Index exceeds matrix dimensions" error in For Loop

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

回答(2 个)

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?
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

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by