【问题标题】:Is it possible to make the whole work in one loop?是否可以在一个循环中完成整个工作?
【发布时间】:2016-06-23 02:24:26
【问题描述】:

下午好, 我想知道我在 MATLAB 中所做的工作是否可以在循环中完成,而不是重复相同的方程来更改相同的变量?

r=1:0.1:20;
A=(r+1./r)./4;
%%%%%%%%%%%%%%%%%%%%%%%%%
x1=0;
R1=((r+1./r)-2)./((r+1./r)+x1);
B1=(1-(x1/2)^2)^-0.5;
NUM1= 2 + (0.5.*x1.*(r+1./r));
DUM1=(r- 1./r ).*(1-(x1/2)^2)^0.5;
C1=EA(NUM1./DUM1);
G1=A.*B1.*C1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x2=-0.1;
R2=((r+1./r)-2)./((r+1./r)+x2);
B2=(1-(x2/2)^2)^-0.5;
NUM2= 2 + (0.5.*x2.*(r+1./r));
DUM2=(r- 1./r ).*(1-(x2/2)^2)^0.5;
C2=EA(NUM2./DUM2);
G2=A.*B2.*C2;

figure
plot(r,G1,'b',r,G2,'r',r,G3,'g',r,G4,'k',r,G5,'c',r,G6,'y','Linewidth',3)
xlabel('$r$','Interpreter','latex','FontSize',18)
ylabel('$G=\gamma P_P L$','Interpreter','latex','FontSize',18)
title('Gain vs r for different values of D/sqrt(M)')
legend('0','-0.1','-1','-1.5','0.5','1.5','1')
%% The function  G for different values of x(or equivalent R) are plotted below sigma 0 %is a notation i am using for different R
figure
plot(R1,G1,'b--',R2,G2,'r',R3,G3,'g',R4,G4,'k',R5,G5,'b--',R6,G6,'r','Linewidth',3)
xlabel('$\sigma_0$','Interpreter','latex','FontSize',18)
ylabel('$G=\gamma P_P L$','Interpreter','latex','FontSize',18)
title('Gain vs r for different values of D/sqrt(M)')
legend('0','-0.5','0.1','1')

所以我的问题不是重复相同的方程 R 和 G 只是为了改变一个变量,我们可以做同样的工作 10 次并通过使用循环使代码更短并且仍然得到相同的图吗? 谢谢

【问题讨论】:

  • 如果我理解正确,你想要的是在 for 循环中重复运行 plot(R,G)。我猜你已经发现新图会覆盖旧图,但是有hold on 语法来保护这些旧图。
  • 最后别忘了hold off
  • 变量EA是什么?
  • 抱歉,BillBokeey EA 是如下定义的函数:function y = EA(x) y = acot(x); y(y
  • 我同意你们的观点,但我不知道该怎么做,希望能得到一些帮助

标签: matlab plot matlab-figure figure


【解决方案1】:

你应该扭曲函数中的所有内容,例如:

function rxPlot(r,x)
A = (r+1./r)./4;
G = zeros(length(A),length(x));
R = zeros(length(A),length(x));

for k=1:length(x)
    R(:,k) = ((r+1./r)-2)./((r+1./r)+x(k));
    B = (1-(x(k)/2)^2)^-0.5;
    NUM = 2 + (0.5.*x(k).*(r+1./r));
    DUM = (r- 1./r ).*(1-(x(k)/2)^2)^0.5;
    C = EA(NUM./DUM);
    G(:,k) = A.*B.*C;
end   
plot(r,G,'Linewidth',3)
xlabel('$r$','Interpreter','latex','FontSize',18)
ylabel('$G=\gamma P_P L$','Interpreter','latex','FontSize',18)
title('Gain vs r for different values of D/sqrt(M)')
legend('0','-0.1','-1','-1.5','0.5','1.5','1')

figure;
plot(R,G,'Linewidth',3)
xlabel('$\sigma_0$','Interpreter','latex','FontSize',18)
ylabel('$G=\gamma P_P L$','Interpreter','latex','FontSize',18)
title('Gain vs r for different values of D/sqrt(M)')
legend('0','-0.1','-1','-1.5','0.5','1.5','1')
end

然后简单地用你的输入调用它:

r = 1:0.1:20;
x = [0 -0.1 -1 -1.5 0.5 1.5 1];
rxPlot(r,x)

这是否按需要工作?

【讨论】:

  • 非常感谢
猜你喜欢
  • 2016-11-22
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
相关资源
最近更新 更多