Index exceeds the number of array elements

Hello All, I'm encountring the following error when I run my code: Index exceeds the number of array elements. My code is simple and shown below:
format longg
syms p1
nm = 3e15;
s01=1;
v1=4.82e20;
tao00=1e-13;
p00=3;
q00=21;
q11=10.6;
R=1.9872036e-3;
T=295;
V1=16.7;
K1=3.27e19;
t1=linspace(1,1e6,10);
taop1=3.6;
S1=4.7;
A1=4.74e3;
Nm = A1*nm;
W1=(Nm/(K1*V1));
y1=q00;
a1= ((s01*tao00*v1)/(nm))*exp((y1)/(R*T));
Z1=(1/(1+a1*p00));
Y1 = (p00/(1+(a1*p00)));
eq1 = ((t1./taop1)==(log(p00./p1))+((a1.*W1).*(Z1-(1./(1+a1.*p1))+log(((1+a1.*p1)./p1).*Y1))));
for i=t1
pp1=0;
pp1(i) = vpa(vpasolve(eq1(i),p1),11);
end
Index exceeds the number of array elements. Index must not exceed 10.

Error in indexing (line 1079)
R_tilde = builtin('subsref',L_tilde,Idx);
pp1;
% loglog(t1,pp1,'o')
% ylim([1e-11 1e-2])
% xlim([10 1e6])
Thank you all for the help in advance.

2 个评论

When I replace the lincpace with the regular t1=1:1:10e6; i get the following error:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in sym/privsubsasgn (line 1128)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in sym/subsasgn (line 965)
C = privsubsasgn(L,R,inds{:});
Error in Langmuir_Q21 (line 44)
pp1(i) = vpa(vpasolve(eq1(i),p1),6);
Assign the results of the vpasolve to a variable. Test to see if the variable is empty or has more one value.

请先登录,再进行评论。

 采纳的回答

A number of the solutions involve complex values.
If you start t1 at 1 then the first few output values are enough greater that the rest are not visible on the graph.
When you start t1 at 1 then you get negative components for the real value of some of the results, so you cannot plot on the log scale. There are negative components for most of the imaginary parts so you cannot use log scale for that either.
format longg
syms p1
Q = @(v) sym(v);
nm = Q(3)*Q(10)^15;
s01 = Q(1);
v1 = Q(482)*Q(10)^18;
tao00 = Q(1)*Q(10)^(-13);
p00 = Q(3);
q00 = Q(21);
q11 = Q(106)/Q(10);
R = Q(19872036)*Q(10)^(-10);
T = Q(295);
V1 = Q(167)/Q(10);
K1 = Q(327)*Q(10)^17;
taop1 = Q(36)/Q(10);
S1 = Q(47)/Q(10);
A1 = Q(4740);
Nm = A1*nm;
W1=(Nm/(K1*V1));
y1=q00;
a1= ((s01*tao00*v1)/(nm))*exp((y1)/(R*T));
Z1=(1/(1+a1*p00));
Y1 = (p00/(1+(a1*p00)));
t1 = linspace(1,50,50);
t2 = linspace(50,200,50);
t3 = linspace(200,1e6,500);
syms T1
eq1 = ((T1./taop1)==(log(p00./p1))+((a1.*W1).*(Z1-(1./(1+a1.*p1))+log(((1+a1.*p1)./p1).*Y1))));
pp1 = arrayfun(@(E)vpasolve(E,p1, 1e-5), subs(eq1, T1, t1));
pp2 = arrayfun(@(E)vpasolve(E, p1, 1e-5), subs(eq1, T1, t2));
pp3 = arrayfun(@(E)vpasolve(E, p1, 1e-5), subs(eq1, T1, t3));
subplot(2,3,1);
plot(t1, real(pp1));
title('p1 real');
subplot(2,3,2);
plot(t2, real(pp2));
title('p2 real');
subplot(2,3,3);
plot(t3, real(pp3));
title('p3 real');
subplot(2,3,4);
plot(t1, imag(pp1))
title('p1 imag')
subplot(2,3,5);
plot(t2, imag(pp2))
title('p2 imag');
subplot(2,3,6);
plot(t3, imag(pp3))
title('p3 imag');

1 个评论

Thank you so much! I understand now whats going on here! I really apprecitae your help.

请先登录,再进行评论。

更多回答(1 个)

I suggest you insert values for p1 and solve for t1. This will be easier than the other way round.
format longg
syms p1 t1
nm = 3e15;
s01=1;
v1=4.82e20;
tao00=1e-13;
p00=3;
q00=21;
q11=10.6;
R=1.9872036e-3;
T=295;
V1=16.7;
K1=3.27e19;
T1=linspace(1,10,10);
taop1=3.6;
S1=4.7;
A1=4.74e3;
Nm = A1*nm;
W1=(Nm/(K1*V1));
y1=q00;
a1= ((s01*tao00*v1)/(nm))*exp((y1)/(R*T));
Z1=(1/(1+a1*p00));
Y1 = (p00/(1+(a1*p00)));
eq1 = ((t1./taop1)==(log(p00./p1))+((a1.*W1).*(Z1-(1./(1+a1.*p1))+log(((1+a1.*p1)./p1).*Y1))));
for i=1:numel(T1)
pp1(i) = double(vpasolve(subs(eq1,t1,T1(i)),p1,11));
end
pp1.'
ans = 10×1
2.27239538495313 1.72126026208779 1.30379462550031 0.987578963504762 0.748056626521194 0.566626808824952 0.429200048722703 0.325104070308059 0.246254996746847 0.186529573242241

5 个评论

Thank you very much! I really appreciate your help.
I'm still getting this error, even when I tried to increase n in T1=linspace. For some reason, it only works for n up to T1=Linspace(1,1e6,117).
Unable to perform assignment because the left and right sides have a different number of elements.
Error in (line 29)
pp1(i) = real(double(vpasolve(subs(eq1,t1,T1(i)),p1,11)));
I'm still getting this error, even when I tried to increase n in T1=linspace. For some reason, it only works for n up to T1=Linspace(1,1e6,117).
We must be able to reproduce the error. So:
What is the error message ?
How do you set T1 when you get the error message ?
Thank you for the kind reply. This is the error message:
Index exceeds the number of array elements (200).
Error in sym/subsref (line 902)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in (line 54)
pp1(i) = vpa(solve(eq1(i),p1,'IgnoreAnalyticConstraints',1),6);
It occurs everytime I change anything in the T1=linespace(1,1e6,n) command. I want it because I want to plot pp1 vs T1 from 1 to 1e6 to see how pp1 will change with time.
I didn't use
pp1(i) = vpa(solve(eq1(i),p1,'IgnoreAnalyticConstraints',1),6);
anywhere in the code I provided.
And the decrease towards 0 has nearly ended at t=10. There is no need to solve up to 1e6.
But if you want: T1 = linspace(1,1e6,100) will also work:
format longg
syms p1 t1
nm = 3e15;
s01=1;
v1=4.82e20;
tao00=1e-13;
p00=3;
q00=21;
q11=10.6;
R=1.9872036e-3;
T=295;
V1=16.7;
K1=3.27e19;
T1=linspace(1,1e6,100);
taop1=3.6;
S1=4.7;
A1=4.74e3;
Nm = A1*nm;
W1=(Nm/(K1*V1));
y1=q00;
a1= ((s01*tao00*v1)/(nm))*exp((y1)/(R*T));
Z1=(1/(1+a1*p00));
Y1 = (p00/(1+(a1*p00)));
eq1 = ((t1./taop1)==(log(p00./p1))+((a1.*W1).*(Z1-(1./(1+a1.*p1))+log(((1+a1.*p1)./p1).*Y1))));
for i=1:numel(T1)
pp1_help = double(vpasolve(subs(eq1,t1,T1(i)),p1,11));
pp1_help = pp1_help(real(pp1_help)>0 & abs(imag(pp1_help))<1e-8);
if ~isempty(pp1_help)
pp1(i) = real(pp1_help(1));
else
pp1(i) = 0.0;
end
end
plot(T1,pp1)
Thank you very much! This is extremely helful. I appreicte your time and efforts.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by