0

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))])
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
Mane-Pal
  • 13
  • 4

1 Answers1

4

Your problem is that you have used sum as a variable. By doing this you're overloading the builtin function sum(). Thus, when you try to do: sum(a0), MATLAB tries to use a0 as an index to the vector sum which obviously fails. You could workaround this, but I would not recommend it as it's easy to forget that you did it when you're working with the code in a week/month/year from now.

MATLAB has a lot of builtin function, most of which have very intuitive names. sum, max, min, length, size, abs, ... the list goes on. Try to avoid using such names as variable names as that can easily mess up your code. Check out this question, I think you'll find it interesting.

Also, using i and j as variable names in MATLAB is not recommended (although you probably learnt it in MATLAB 101).

The good thing about programming is, you're rarely the first one to have encountered the problem. There are 147 questions with the exact same error message posted on SO. You can have a look at Dennis' generic solution, or any of these ( <= The letters are links.)

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • Good answer. MATLAB really should not allow overwriting of built-in functions, that'd save approximately 147 questions on SO :) – Adriaan Sep 22 '15 at 12:48
  • Thanks @Adriaan! I bet it's a lot more than 147! Not everyone manages to put the error message in the question. And, I actually don't agree with you. You will suddenly have a lot of worthless code the moment Mathworks decides to create a few functions. `my_sum`, `abs_val`, `res`, `results` +++. A warning in the editor would be nice though =) – Stewie Griffin Sep 22 '15 at 13:27
  • Hmm, they could ban it and show the same kind of warning as is currently on functions that "will be removed in a future release". Warning would be good anyway for sure. – Adriaan Sep 22 '15 at 13:29