如何在Matlab图上连接点?

时间:2016-06-16 11:27:32

标签: matlab plot

我编写代码来获取两个变量之间的关系。代码如下:

for fn=0.1:0.1:10
    for Qe= [0.1,0.5, 1, 5]
        fn2=fn.*fn;
        Mg=abs((Ln*fn2)/(((Ln+1)*(fn2)-1)+1i*(((fn2)-1).*fn.*Qe*Ln)));
        semilogx(fn,Mg,'b','linewidth',7); 
        hold on;
        save;
    end
end

enter image description here 但是,我得到的曲线是散点。我希望得到所有要点。当我增加fn的步长点,即fn = 0.1:0.001:10时,它会更好,但需要很长时间。

2 个答案:

答案 0 :(得分:3)

您的问题是您每次迭代都会绘制一个点。让循环运行,将点保存为矢量,然后在绘制矢量的末尾调用单个绘图,这将自动生成连接线。

fn=0.1:0.1:10
Qe= [0.1,0.5, 1, 5]
Mg = zeros(numel(fn),numel(Qe)); %initialise output
for ii=1:numel(fn)
    fn2=fn(ii)^2;
    for jj = 1:numel(Qe)
        Mg(ii,jj)=abs((Ln*fn2)/(((Ln+1)*(fn2)-1)+1i*(((fn2)-1).*fn(ii).*Qe(jj)*Ln))); %save in matrix form
    end
end

figure
hold on;
semilogx(fn,Mg(:,1),'b','linewidth',7); % Generate plots with different colours
semilogx(fn,Mg(:,2),'r','linewidth',7);
semilogx(fn,Mg(:,3),'y','linewidth',7);
semilogx(fn,Mg(:,4),'g','linewidth',7);

答案 1 :(得分:0)

我可以使用此代码解决它。它可以解决问题,然而,它有点慢(

    %%Parameters%%
Ln=8;
Mgmin=0.88;
Mgmax=1.18;
Qe= [0.25,0.3,0.35,0.7,1.65,4,8];
c= {'b','m','r','g','y','c','k'};
%%Gain Vs Frequecny Function%%
fn=0.1:0.001:10;
fn2=fn.*fn;
%%Gain Curve%%
figure;
for j=1:7 
    for i=1:9901
        Mg(i)=abs((Ln*(fn2(i)))/(((Ln+1)*(fn2(i))-1)+1i*(((fn2(i))-1).*fn(i).*Qe(j)*Ln)));
    end;
    semilogx(fn,Mg,c{j},'linewidth',3);
    hold on;
    plot(fn,Mgmax,'k');
    hold on;
    plot(fn,Mgmin,'k');
    save;
end;