plot a graph using For statement

조회 수: 1 (최근 30일)
zamri
zamri 2013년 2월 13일
Hi, I wonder if anyone can help.
I want to plot a function called mag(m2,1) from x-axis 10 to 500 but i want to simplify the code so that i dont have to rewrite cases where m and n are other numbers, say m=3,n=4 ; m=1,5 etc, generally for m=1 to 5 and n=1 to 5, for codes below, i only put 2 cases where m=1 n=1 and m=1 n=2. At the end i want to add all the plot of m=1..5, n=1..5.
for num2 = 10:500
f=1e-10+(num2-1);
Freq(num2,1)=f;
coef_HFv=a;
for m=1
for n=1
HHFv1=HHFv1+sin(m*n);
end
end
HFv1(num2)=coef_HFv*HHFv1;
for m=1
for n=2
HHFv2=HHFv2+sin(m*n);
end
end
HFv2(num2)=coef_HFv*HHFv2;
end
mag=zeros(491,1);
for m2=10:500
mag(m2,1)=log10(HFv1(m2))+log10(HFv2(m2));
end
figure(1)
plot(Freq(10:500,1), mag(10:500,1), 'LineWidth',2);
grid on
  댓글 수: 3
Eric Truslow
Eric Truslow 2013년 2월 13일
Actually, your code should not work since you have not initialized HHFv1 to a value:
HHFv1=HHFv1+sin(m*n)
Is this sum important?
Youssef  Khmou
Youssef Khmou 2013년 2월 13일
where is the variable a, and as you want m and n from 1to 5 then HHFv1 and HHFv2 are the same and are equal .

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

채택된 답변

Youssef  Khmou
Youssef Khmou 2013년 2월 13일
Hi,you have to define all your variables , the code now is technically correct but you have to initialize your variables :
for num2 = 10:500
f=1e-10+(num2-1);
Freq(num2)=f;
coef_HFv=3; % you put here a , to me a is unknown
HHFv1=0; HHFv2=0;
for m=1:5
for n=1:5
HHFv1=HHFv1+sin(m*n);
HHFv2=HHFv2+sin(m*n);
end
end
HFv1(num2)=coef_HFv*HHFv1;
HFv2(num2)=coef_HFv*HHFv2;
end
mag=zeros(491,1);
for m2=10:500
mag(m2)=log10(HFv1(m2).*HFv2(m2));
end
mag=mag';
figure(1)
plot(Freq(10:500), mag(10:500), 'LineWidth',2);
grid on
You can replace the two loops for HHFv1 and HHFv2 by this :
G=(1:5)'*(1:5);
HHFv1=sum(sum(sin(G)));
HHFv2=HHFv1 % according to you code they are the same,

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by