[已答复] 用函数返回值给数组赋值提示左右元素数目不同

[复制链接]
deskjethaha 发表于 2021-4-7 21:15:20
  1. Vp2=0.1:0.01:0.5;

  2. ip2 = zeros(1,numel(Vp2));

  3. for ii=1:1:numel(Vp2)

  4.         ip2(ii) = Calc(Vp2(ii));

  5.         disp([ii]);

  6. end

  7. plot(Vp2,ip2);

  8. xlabel('Vp2');

  9. ylabel('ip2');



  10. function ip2 = Calc(Vp2)



  11. syms y

  12. syms ip2

  13. syms xNOsc

  14. syms iP1NO

  15. syms xNOfc

  16. syms Ct



  17. EOfc = 0;%first chamber open circuit voltage

  18. EOsc = 0.4487;%second chamber open circuit voltage

  19. deltaSfc = 0;%entropy change in first chamber

  20. deltaSsc = -12.5325;%entropy change in second chamber

  21. F = 96485.33;% Faraday Constant

  22. T = 1023;% Sensor Temp in Kelvin

  23. T0 = 298.15;% Reference Temp in Kelvin

  24. R=8.314;% Universal Gas Constant

  25. E0 = 30e-3;% Standard Cell Potential

  26. P = 1;% Sample Gas Pressure

  27. xO2rc = 0.1;% O2 mole fraction in ref chamber

  28. xN2rc = 0.9;% N2 mole fraction in ref chamber

  29. Ae1 = 10.15e-6;%1st electrode area

  30. Ae2 = 2.59e-6;%2nd electrode area

  31. Ad1=1.81e-7;%1st diffusion barrier area

  32. Ad2=3.2e-8;% 2nd diffusion barrier area

  33. Rcell=0;%cell resistance

  34. xONOsc=6.62e-7;%ref mole farction NO 2nd chamber

  35. iOP1 = 0.0109;%1st cell exchange current density

  36. iOP2 = 0.0471;%2nd cell exhange current density

  37. gamma=0.1;%concentration ratio

  38. alpha1=4.817;%1st charge transfer coefficient

  39. alphaa2=4.15;%2nd anode charge coefficient

  40. alphac2=7.5;%2nd cathode charge coefficient

  41. Ld1=1.66;%length of 1st diff barrier

  42. Ld2= 0.71;%length of 2nd diff barrier

  43. DNO= 7.1e-6;%diffusion coefficient of NO

  44. Vp1=0.45;

  45. xNOenv=165e-6;

  46. x=Rcell*ip2*Ae2;

  47. z=R*T*log(iP1NO/iOP1)/alpha1*F;

  48. Ct=P/(R*T);



  49. eq1=(iOP2*(xNOsc/xONOsc)^gamma)*(exp(alphaa2*F*y/R*T)-exp(-alphac2*F*y/R*T))-ip2;

  50. eq3=-Vp2+EOsc+(deltaSsc*(T-T0))/(2*F)-(R*T*log((xO2rc)^0.5*(xN2rc)^0.5))/(2*F)-x-y;

  51. eq5=-Vp1+EOfc+deltaSfc*(T-T0)/(2*F)-R*T*log(xNOenv/xNOfc)-z;

  52. eq6=-ip2*Ae2-(2*F*Ct*DNO*Ad2*(xNOfc-xNOsc))/Ld2;

  53. eq8=-iP1NO*Ae1-ip2*Ae2-(2*F*Ct*DNO*Ad1*(xNOenv-xNOfc))/Ld1;

  54. eqns=[eq1==0,eq3==0,eq5==0,eq6==0,eq8==0];

  55. vars=[ip2,xNOfc,xNOsc,iP1NO,y];

  56. [ip2,~,~,~,~]=vpasolve(eqns,vars);



  57. end

复制代码
这个程序的功能是解方程得出Vp2和ip2的关系。在第四行一直报错:

无法执行赋值,因为左侧和右侧的元素数目不同。

出错 Sensor (line 4)
        ip2(ii) = Calc(Vp2(ii));

第四行的原意是通过循环不断把Calc函数的返回值赋值到ip2数组里面。请问如何解决呢?

3 条回复


WarnerChang 发表于 2021-4-7 21:20:17
Vp2=0.1:0.01:0.5;

%ip2 = zeros(1,numel(Vp2));

for ii=1:1:numel(Vp2)

        ip2{ii} = Calc(Vp2(ii));

        disp([ii]);

end

%plot(Vp2,ip2);

%xlabel('Vp2');

%ylabel('ip2');
跑一下,就知道是为什么了!

deskjethaha 发表于 2021-4-8 15:46:01
WarnerChang 发表于 2021-4-7 21:20
Vp2=0.1:0.01:0.5;

%ip2 = zeros(1,numel(Vp2));

用了这个代码跑起来了,但是加上下面的函数就提示相同的错误了:'(

WarnerChang 发表于 2021-4-9 08:05:29
deskjethaha 发表于 2021-4-8 15:46
用了这个代码跑起来了,但是加上下面的函数就提示相同的错误了

那肯定啊!你自己看看ip2里面每个单元有几个元素,又不是只有一个元素
您需要登录后才可以回帖 登录 | 注册

本版积分规则

相关帖子
热门教程
站长推荐
快速回复 返回顶部 返回列表