So I'm trying to make a .m file that calculates the fourier-series and plots it, but I keep getting an error
Subscript indices must either be real positive integers or logicals.
I have located where the code error is located, but i have no idea how to fix this, can you help me understand this error?
The error happens when I try to sum my function so in sum(a0)
.
My code:
syms k x
f = [...
cos(x)
];
a = [... % Hele perioden
-pi pi;
];
sum = [2 5 20]; % N - Antal af Fourie-skridt.
%% Fourie Koeffiecienter
for i = 1:length(f)
a0(i) = int(f(i),x,a(i,:)); %a_0 findes
ak(i) = int(f(i)*cos(k*pi*x/max(a(:))),x,a(i,:)) ; %a_k findes
bk(i) = int(f(i)*sin(k*pi*x/max(a(:))),x,a(i,:)) ; %b_k findes
end
a0 = 1/(2*max(a(:))) * sum(a0);
ak = 1/(max(a(:))) * sum(ak);
bk = 1/(max(a(:))) * sum(bk);
%% Summen for 3 forskellige N
x = linspace(a(1,1),max(a(:)),25); %linspace til x for hele perioden.
fsum_1 = a0 + symsum(ak*cos(k*pi*x/max(a(:))) + bk*sin(k*pi*x/max(a(:))),k,1,sum(1,1));
fsum_2 = a0 + symsum(ak*cos(k*pi*x/max(a(:))) + bk*sin(k*pi*x/max(a(:))),k,1,sum(1,2));
fsum_3 = a0 + symsum(ak*cos(k*pi*x/max(a(:))) + bk*sin(k*pi*x/max(a(:))),k,1,sum(1,3));%F.R sum
%% plot
subplot(3,1,1)
plot(x,fsum_1)
title(['Fourierække ved n =',num2str(sum(1,1))])
subplot(3,1,2)
plot(x,fsum_2)
title(['Fourierække ved n =',num2str(sum(1,2))])
subplot(3,1,3)
plot(x,fsum_3)
title(['Fourierække ved n =',num2str(sum(1,3))])