"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개)

James Tursa
James Tursa 2016년 10월 13일
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
Star Strider 2016년 10월 13일
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에서 Mathematics에 대해 자세히 알아보기

질문:

2016년 10월 13일

답변:

2016년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by