problem with for loop
Ältere Kommentare anzeigen
hello i try to make a for loop but i need the jumps between the numbers will be in factor of 10 lets say there is a for loop like that: for i=1:5
so i need to make
for i=10,100,1000,10000
but it wont work maybe someone know what to do? thanks alot!
Akzeptierte Antwort
Weitere Antworten (4)
Oleg Komarov
am 18 Mai 2011
for ii = 10.^(1:5)
Sean de Wolski
am 18 Mai 2011
w=0:60*(10^6/4096)+1:60*10^6 ; % [Hz]
zl=50 ; % [ohm]
zs=50 ; % [ohm]
R=1.9884 ; % [ohm/meter]
G=0.01686*10^-9 ; % [mho/meter]
C=0.13394*10^-9 ; % [farad/meter]
L=362.81*10^-9 ; % [henrry/meter]
zc=sqrt((R+1j.*w.*L)./(G+1j.*w.*C)); % characteristic impedance
gama=sqrt((R+1j.*w.*L).*(G+1j.*w.*C));
l_change = 10.^(1:5);
%Engine
lg = (l_change')*gama;
aa=cosh(lg);
bb=bsxfun(@times,zc,sinh(lg));
cc=bsxfun(@times,(1./zc),sinh(lg));
dd=cosh(lg);
H_change= (zl./((aa.*zl)+bb+(cc.*zl.*zs)+(dd.*zs)));
EDITED: per Oleg's comment and full data set.
1 Kommentar
Oleg Komarov
am 18 Mai 2011
Doesn't work with complete code, bsxfun is needed
itsik
am 18 Mai 2011
6 Kommentare
Oleg Komarov
am 18 Mai 2011
Rewriting like this (if throws an error post it, since I don't know the dimensions of gama, can't check myself):
for l = 10.^(1:5)
aa = cosh(gama.*l);
bb = zc.*sinh(gama.*l);
cc = 1./zc.*sinh(gama.*l);
dd = cosh(gama.*l);
H_change = zl./(aa.*zl + bb + cc.*zl.*zs + dd.*zs);
end
Andy
am 18 Mai 2011
First of all, the order of the answers changes over time when more people add answers or vote for existing ones. You shouldn't add an "answer" for what is really a comment about another answer. You should use the comment, or update your original question with this new information.
Second, what doesn't work? Was there an error? If so, post the message. Did you just get unexpected output? If so, what was unexpected about it. We don't have your data, so we can't run your code. to see what went wrong.
Third, if you are still having problems with this, why did you accept an answer? Do your current problems indicate that the other answer didn't work either? If not, what went wrong?
Fourth, I'm guessing the problem with this code is that you wrote "for length=l_change", making "length" your loop variable, but you proceed to use "l_change" instead in your commands inside the loop. But I can't be sure this is the only error.
Oleg Komarov
am 18 Mai 2011
@Andy: I'will add the first paragraph to the DON'Ts, wasn't really convinced before.
Matt Fig
am 18 Mai 2011
What Oleg showed fixes your problem, but it may not be what you need. Did you want to store the values of H_change or any of aa, bb, cc, dd or all of them?
Also, don't name a variable length. You will regret this when you get an error later...
Matt Fig
am 18 Mai 2011
@Oleg and Andy,
Also, as people comment on non-Answers, it is harder for the editors to move the answers to the appropriate comments section...
Perhaps this will be fixed in the future, but right now editors cannot edit comments, only delete them, and cannot change an answer to a comment with the same authorship...
Andy
am 18 Mai 2011
@Oleg, thanks!
@Matt, good to know. In the future, I'll make this sort of comment on the main question so that the 'answer' can be edited appropriately.
itsik
am 18 Mai 2011
0 Stimmen
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!