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

조회 수: 1 (최근 30일)
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);

채택된 답변

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2019년 11월 10일
  • 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
Paul Velasco
Paul Velasco 2019년 11월 10일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by