how to plot all the data point in a for loop

조회 수: 2 (최근 30일)
Sheryl
Sheryl 2020년 5월 3일
댓글: KSSV 2020년 5월 3일
Hi, I am wondering how to plot the data in the foor loop, and here is my code
Thanks for helping
figure()
for w=1:100
lambda = 1/w;
g1 = (w^2*lambda^2)/(1+w^2*lambda^2);
g2 = (w*lambda^2)/(1+w^2*lambda^2);
end
plot(w, g1, w, g2)

채택된 답변

KSSV
KSSV 2020년 5월 3일
편집: KSSV 2020년 5월 3일
No loop needed:
w = 1:100 ;
lambda = 1./w;
g1 = (w.^2.*lambda.^2)./(1+w.^2.*lambda.^2);
g2 = (w.*lambda.^2)./(1+w.^2.*lambda.^2);
plot(w, g1, w, g2)
If you want a loop (which is not required)
w = 1:100 ;
g1 = zeros(size(w)) ;
g2 = zeros(size(w)) ;
for i=1:100
lambda = 1/w(i) ;
g1(i) = (w(i)^2*lambda^2)/(1+w(i)^2*lambda^2);
g2(i) = (w(i)*lambda^2)/(1+w(i)^2*lambda^2);
end
plot(w, g1, w, g2)
  댓글 수: 2
Sheryl
Sheryl 2020년 5월 3일
thanks
KSSV
KSSV 2020년 5월 3일
Thanks is accepting the answer.....:)

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

추가 답변 (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