【问题标题】:plot Vectors must be the same lengths绘图向量必须具有相同的长度
【发布时间】:2014-07-13 09:36:35
【问题描述】:

这是我的代码

po=0.21; %presuree of oxigen atm
ph=1;  %presurre of hydorgen atm
t=0.018; %mem tickness cm 
F=96487;   %C/mol
R=8.314471;      % gas confident J/K Mol
e1=-0.948;         %v act confident
e2=0.00312;         %v act confident
e3=7.6*(10^(-5));     %v act confident
e4=-1.93*(10^(-4));    %v act confident
n=2;      %number of electron
anda=14;   %landa
b=8;  %confident of V consentration cm^2/Amp
A=4; %cell active area cm^2 and i is current density Amp/Cm^2
r=0.2114
f=1;
j=1;
for T=333:10:363
    Co=((po)/(5.08*(10^(6))*exp((-498/T))))
for i=0:0.01:1
    v1(j,f)=-[(e1)+(e2*T)+(e3*T*log(Co))+(e4*T*log(i*A))]
    f=f+1
    end
    j=j+1
end
c=1;
h=1
for T=333:10:363
    z=(0.005139*(anda)-(0.00326))*exp((1268*((1/303)-(1/T))))
    for i=0:0.01:1
    v2(h,c)=i*(t/z)
    c=c+1
    end
h=h+1
end
d=1;
u=1;
for T=333:10:363
    a1=(1.1)*(10^(-4))-((1.2*10^(-6))*(T-273))
    for i=0:0.01:1
  v3(u,d)=a1*exp(b*i)
  d=d+1
    end
u=u+1
end
q=1;
for T=333:10:363
    E=1.229-(0.85*(10^(-3))*(T-298.15))+4.3085*(10^(-5))*T*([log(ph)+(1/2)*log(po)])
  V(q)=E-v1(j)-v2(h)-v3(u)
  q=q+1
end
T=333:10:363
i=0:0.01:1
plot(i,V,'-')

我想要一个图,X 标签是 i,Y 标签是 v,我们可以看到不同温度 (T) 下 V 的变化。但我面临的错误是“情节 向量的长度必须相同。”

我该如何解决这个错误?

【问题讨论】:

  • iV 有什么关系?您正在尝试沿 x 轴绘制 101 点矢量,沿 y 轴绘制 4 点矢量。
  • 我想画这个图:dl.dropboxusercontent.com/u/107043399/3r.tiff 我们知道 v 随 i 和 T 变化是恒定的,现在我想展示 T 的效果

标签: matlab plot figure


【解决方案1】:

使用size(i) size(V) 检查 x、y 向量的维度。 i 应该是一个向量,大小应该是 [1 Len] 或 [Len 1]。确保 V 的大小也是 [Len,​​ xxx] 或 [xxx, Len],因此它们的长度相同,您可以绘制它。另外,请确保 i 和 V 在同一维度上具有相同的长度 'Len',否则您可以使用点引号运算符 V.' 来获得转置。

【讨论】:

    【解决方案2】:

    正如 Steven 所说,您的尺寸不匹配。更加熟悉代码中解释的几个 Matlab 数组运算符(: 和 .)可以缩短和阐明代码。

    开始之前的家务:

    clc % clear command window
    close all % close all figure windows
    clear all % clear all variables in workspace
    dbstop if error % program stops in debugger if error occurs
    

    声明所有变量:

        po=0.21; %presuree of oxigen atm
        ph=1;  %presurre of hydorgen atm
        t=0.018; %mem tickness cm 
        F=96487;   %C/mol
        R=8.314471;      % gas confident J/K Mol
        e1=-0.948;         %v act confident
        e2=0.00312;         %v act confident
        e3=7.6*(10^(-5));     %v act confident
        e4=-1.93*(10^(-4));    %v act confident
        n=2;      %number of electron
        anda=14;   %landa
        b=8;  %confident of V consentration cm^2/Amp
        A=4; %cell active area cm^2 and i is current density Amp/Cm^2
        r=0.2114;
    

    将 T 用作 1x4 并首先执行该操作 之后所有这些都将是 1x4 T=333:10:363; 注意使用dot operator 进行逐点操作

        Co = ((po)./(5.08e6*exp((-498./T))));
        z = (0.005139*(anda)-(0.00326))*exp((1268*((1/303)-(1./T))));
        a1 = (1.1)*(10^(-4))-((1.2*10^(-6))*(T-273));
        E = 1.229-(0.85*(10^(-3))*(T-298.15))+4.3085*(10^(-5))*T*([log(ph)+(1/2)*log(po)]);
    

    现在让我们完成所有涉及 i 的部分。 我们不要从 i=0 开始,因为这会给出 Nan/Inf 结果 i=0.01:0.01:1;

    如果您愿意,您可以使用矩阵乘法的部分来做到这一点。它在这里使用 for 循环实现,因为这样更容易理解。

        for idx = 1:length(T)
            % each of these will be a 101x1 vector
            v1 =-(  e1 + (e2*T(idx)) + (e3*T(idx)*log(Co(idx))) + (e4*T(idx)*log(i*A))  );
            v2 = i*(t/z(idx));
            v3 = a1(idx)*exp(b*i);
            V(idx,:) = E(idx) - v1 - v2 - v3;
        end
    

    V=(idx,:) 行中,我们使用 101x1 向量并使用 colon operator 将其推入 V 的 idx 行。

    现在我们可以绘制答案了!

        plot(i,V,'-')
        % now we'll do a little labeling
        hYLabel = ylabel('V');
        hXLabel = xlabel('i');
        hTitle = title('Your title here');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多