Multiple variables in for loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
Birdman
am 3 Apr. 2018
Bearbeitet: Birdman
am 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
0 Kommentare
Aditya Deshpande
am 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!