Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

1 vue (au cours des 30 derniers jours)
Hi im currently a student and not very good at matlab (or coding in general). Im trying to run this code but keep getting this error mesage. Thank you in advance.
syms t T p
T=input('what is the period');
p=input('how many coefficients');
t=linspace(0,3*T,100);
N=10;
bp=sin(80)/320 - cos(80)/4;
ap=sin(80)/4 - sin(40)^2/160;
for i=1:100
sum=0;
for p=1:N
sum=(sum+(N*ap*cos(2*pi*p*t/T))+(N*bp*sin(2*pi*p*t/T)));
end
F(i)=sum;
end
figure(1);
plot(t,F);

Réponse acceptée

Thiago Henrique Gomes Lobato
  • You don't need to declare numerical values with syms
  • your variable sum is the same as the function sum for matlab, this can cause many problems
  • (This is what prevent your code to run) your t is a vector, so you actually don't need to make a for loop for each time point since sum will already be a vector, otherwise you try to put a vector in a single variable spot and thus you get the wrong size problem
This version of your code should work:
T=input('what is the period');
p=input('how many coefficients');
t=linspace(0,3*T,100);
N=10;
bp=sin(80)/320 - cos(80)/4;
ap=sin(80)/4 - sin(40)^2/160;
sum=0;
for p=1:N
Sum=(Sum+(N*ap*cos(2*pi*p*t/T))+(N*bp*sin(2*pi*p*t/T)));
end
F=Sum;
figure(1);
plot(t,F);
  1 commentaire
Paul Velasco
Paul Velasco le 10 Nov 2019
Thanks this helped a lot! Its creating a square wave when i need to create a sawtooth wave, any idea why this is happening? Maybe my values for ap and bp?

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by