Multiple variables in for loop

조회 수: 3 (최근 30일)
Asli Merdan
Asli Merdan 2018년 4월 3일
편집: Birdman 2018년 4월 3일
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

Aditya Deshpande
Aditya Deshpande 2018년 4월 3일
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

카테고리

Help CenterFile 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