Assignment error only occuring within a while loop
显示 更早的评论
So I'm getting what I hear is quite a common MATLAB eror, "In an assignment A(I) = B, the number of elements in B and I must be the same." on line 25 of my code (the line: answer2(f)=(1/f)*sin(f*x);)
The code is:
function b=evaluatesum(x,n)
answer1=[];
for z=1:2:n
answer1(z)=(1/z)*sin(z*x);
end
b=(4/pi)*sum(answer1);
disp(b)
clf
x=0:pi/3:8*pi;
f1=(4/pi)*(1/3)*sin(3*x);
f2=(4/pi)*(1/33)*sin(33*x);
f3=(4/pi)*(1/99)*sin(99*x);
f4=sin(x);
hold on
plot(x,f1,'r--')
plot(x,f2,'g')
plot(x,f3, 'b--')
plot(x,f4)
legend('f1','f2','f3','f4')
n=0;
answer=0;
answer2=[];
while abs(answer-1)>(1/1000)
for f=1:2:n
answer2(f)=(1/f)*sin(f*x);
end
answer=sum(answer2)*(pi/4);
n=n+2;
end
disp(n-2)
end
I'm not sure why I'm getting the error. Also, when I remove the while loop, it runs just fine, there is no problem with the assignment. I'm not sure why the while loop would impact this at all?
Andrew
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Parallel Computing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!