plot a graph using For statement

1 次查看(过去 30 天)
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