Effacer les filtres
Effacer les filtres

Multiple variables in for loop

1 vue (au cours des 30 derniers jours)
Asli Merdan
Asli Merdan le 3 Avr 2018
Modifié(e) : Birdman le 3 Avr 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

Réponses (2)

Birdman
Birdman le 3 Avr 2018
Modifié(e) : Birdman le 3 Avr 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 le 3 Avr 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by