Multiple variables in for loop

5 views (last 30 days)
Asli Merdan
Asli Merdan on 3 Apr 2018
Edited: Birdman on 3 Apr 2018
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

Answers (2)

Birdman
Birdman on 3 Apr 2018
Edited: Birdman on 3 Apr 2018
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

Aditya Deshpande
Aditya Deshpande on 3 Apr 2018
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!