For loop - what about my min and max values?

조회 수: 1 (최근 30일)
Tom
Tom 2011년 12월 11일
For loop - what about my min and max values? I'm using a for loop and although my plot is right, I'm only seeing my maximum x-axis value in the Workspace Max column. Is there a way to have the Min and Max columns actually correct?
Many thanks
iter=1;
for f=50:1000
rho_a=1.2041;
sigma=11000;
phi=0.99;
alpha=1;
c=343.26;
i=sqrt(-1);
L=0.1;
a=0.03;
k_a=(2*pi*f)/c;
k_2=((2*pi*f)/c)*sqrt(alpha-((i*sigma*phi)/(2*pi*f*rho_a)));
z_c_2=((rho_a*c)/phi)*sqrt(alpha-((i*sigma*phi)/(2*pi*f*rho_a)));
z_1=(z_c_2*cot(k_2*L)*(cos(k_a*a)+(i/(rho_a*c))*sin(k_a*a)))/(i*cos(k_a*a)-(1/(rho_a*c))*sin(k_a*a));
R=((z_1/(rho_a*c))-1)/(1+(z_1/(rho_a*c)));
Rrec(iter)=R;
AbsCoef=1-(real(R))^2;
AbsCoefrec(iter)=AbsCoef;
z_1rec(iter)=z_1;
iter=iter+1;
end
plot(50:1000,AbsCoefrec)
  댓글 수: 2
Jan
Jan 2011년 12월 11일
What is "the maximum x-axis value" and what is the "Workspace Max column"? And how is the correctness of the "Min and Max columns" defined? What the is the relation between the question and the code?
To improve the code (dramatically!) look in this forum for the term "pre-allocation".
David Young
David Young 2011년 12월 11일
i is already defined in Matlab - as long as you haven't redefined it earlier in the program you don't need to do i = sqrt(-1)

댓글을 달려면 로그인하십시오.

채택된 답변

bym
bym 2011년 12월 11일
Your x axis is the loop variable f, which can only take on one value at a time, thus the min and max are the same. To do what you want, you would have to modify your code something like this
freq = 50:1000;
AbsCoefrec = zeros(size(freq)); % preallocate!
for f = freq
% ---insert code here---
end
plot(freq,AbsCoefrec)
  댓글 수: 1
Tom
Tom 2011년 12월 11일
right okay. is req the same as rec?
many thanks for that

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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