Multiple variables in for loop

Hello, I want to get each error value(E) for 3 a's. How can I do it ? (I don't know how to pre-allocate)
a=[11,51,101]
Toplam=0
x=pi/3
for (k=0:a)
x=(((-1)^k*(x))^((2*k)+1))/factorial((2*k)+1)
Toplam=Toplam+x
W=Toplam+x
E(a)=abs(W-0.86602540378444)
H(a)=E(a)/(0.866)
end

回答(2 个)

Birdman
Birdman 2018-4-3
编辑:Birdman 2018-4-3
Something like this?
a=[11,51,101]
%preallocation
E=zeros(1,numel(a));
H=zeros(1,numel(a));
Toplam=0
x=pi/3
for (k=1:numel(a))
x=(((-1)^(k-1)*(x))^((2*(k-1))+1))/factorial((2*(k-1))+1)
Toplam=Toplam+x
W=Toplam+x
E(k)=abs(W-0.86602540378444)
H(k)=E(k)/(0.866)
end
NOTE: MATLAB arrays starts with index of 1. I think you meant to do the following:
a=[11,51,101];
Toplam=0;
x=pi/3;
H = zeros(3,1); E=zeros(3,1);
for i=1:length(a)
k=a(i);
x=(((-1)^k*(x))^((2*k)+1))/factorial((2*k)+1);
Toplam=Toplam+x;
W=Toplam+x;
E(i)=abs(W-0.86602540378444);
H(i)=E(i)/(0.866);
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by