Here is my code
这是我的代码
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,'-')
I want to have a figure that the X label is i, the Y label is v and we can see change of V in different Temperature (T). but I face the error that "plot Vectors must be the same lengths."
我想知道X的标签是I, Y的标签是v,我们可以看到v在不同温度下的变化,但是我要面对的是,“图向量必须是相同长度的。”
How can I fix this error?
我该如何改正这个错误?
2 个解决方案
#1
0
Check dimension of your x, y vector by using size(i) size(V)
. i should be a vector and the size should be [1 Len] or [Len 1]. Make sure the size of V is [Len, xxx] or [xxx, Len] too, so they are in the same length and you can plot it. Also, make sure i, and V have the same length 'Len' in the same dimension, otherwise you can use dot quotation operator, V.'
, to get the transpose.
用尺寸(i)大小(V)来检查x的尺寸。我应该是一个向量,大小应该是[1 Len]或[Len 1]。确保V的大小是[Len, xxx]或者[xxx, Len],所以它们的长度是相同的,你可以把它画出来。同样,确保i和V在相同的维度上有相同长度的Len,否则你可以使用点引用运算符V。,为了得到转置。
#2
0
As Steven mentioned, you have a dimensional mismatch. Getting more familiar with a couple of the Matlab array operators (: and .) explained in the code can shorten and clarify the code.
正如Steven提到的,你有一个维度不匹配。更熟悉一些Matlab数组运算符(:和)在代码中解释可以缩短和澄清代码。
Housekeeping before we get started:
在我们开始之前:
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
Declare all your variables:
声明所有变量:
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;
use T as a 1x4 and do that stuff first all of these will be 1x4 afterwards T=333:10:363; Notice the use of the dot operator to do pointwise operations
用T作为1x4,先做这些,所有这些都是1x4,然后T=333:10 363;注意点运算符使用点操作。
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)]);
Now let's do all the parts that involve i. Let's not start at i=0 because that gives Nan/Inf results i=0.01:0.01:1;
现在让我们做所有涉及i的部分,我们不要从i=0开始,因为这给了Nan/Inf结果i=0.01。
If you wanted to, you could do this with section using matrix multiplication. It's implemented here with a for loop because that is easier to follow.
如果你想,你可以用矩阵乘法来做这个。它是用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
In the V=(idx,:)
line we are taking the 101x1 vector and shoving it into the idx row of V using the colon operator.
在V=(idx,:)行中,我们取101x1向量,并将它推入使用冒号的V的idx行。
Now we can plot our answer!
现在我们可以画出答案了!
plot(i,V,'-')
% now we'll do a little labeling
hYLabel = ylabel('V');
hXLabel = xlabel('i');
hTitle = title('Your title here');
#1
0
Check dimension of your x, y vector by using size(i) size(V)
. i should be a vector and the size should be [1 Len] or [Len 1]. Make sure the size of V is [Len, xxx] or [xxx, Len] too, so they are in the same length and you can plot it. Also, make sure i, and V have the same length 'Len' in the same dimension, otherwise you can use dot quotation operator, V.'
, to get the transpose.
用尺寸(i)大小(V)来检查x的尺寸。我应该是一个向量,大小应该是[1 Len]或[Len 1]。确保V的大小是[Len, xxx]或者[xxx, Len],所以它们的长度是相同的,你可以把它画出来。同样,确保i和V在相同的维度上有相同长度的Len,否则你可以使用点引用运算符V。,为了得到转置。
#2
0
As Steven mentioned, you have a dimensional mismatch. Getting more familiar with a couple of the Matlab array operators (: and .) explained in the code can shorten and clarify the code.
正如Steven提到的,你有一个维度不匹配。更熟悉一些Matlab数组运算符(:和)在代码中解释可以缩短和澄清代码。
Housekeeping before we get started:
在我们开始之前:
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
Declare all your variables:
声明所有变量:
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;
use T as a 1x4 and do that stuff first all of these will be 1x4 afterwards T=333:10:363; Notice the use of the dot operator to do pointwise operations
用T作为1x4,先做这些,所有这些都是1x4,然后T=333:10 363;注意点运算符使用点操作。
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)]);
Now let's do all the parts that involve i. Let's not start at i=0 because that gives Nan/Inf results i=0.01:0.01:1;
现在让我们做所有涉及i的部分,我们不要从i=0开始,因为这给了Nan/Inf结果i=0.01。
If you wanted to, you could do this with section using matrix multiplication. It's implemented here with a for loop because that is easier to follow.
如果你想,你可以用矩阵乘法来做这个。它是用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
In the V=(idx,:)
line we are taking the 101x1 vector and shoving it into the idx row of V using the colon operator.
在V=(idx,:)行中,我们取101x1向量,并将它推入使用冒号的V的idx行。
Now we can plot our answer!
现在我们可以画出答案了!
plot(i,V,'-')
% now we'll do a little labeling
hYLabel = ylabel('V');
hXLabel = xlabel('i');
hTitle = title('Your title here');