Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Index exceeds the number of elements (1) HELP

1 vue (au cours des 30 derniers jours)
Cansu Karaca
Cansu Karaca le 24 Mar 2019
Clôturé : MATLAB Answer Bot le 20 Août 2021
it gives me an error with the x1 calculation
% Ask for inputs
y1 = input('Please input y1:');
t = input('Please input T:');
% Calculations
y2 = 1-y1;
p1 = exp(16.59158-(3643.31/(t-33.424)));
p2 = exp(14.25326-(2665.54/(t-53.424)));
a = 2.771-(0.00523*t);
% Initialize
c1(1) = 1;
c2(1) = 1;
% Initial P calculation
p(1) = 1./((y1/(c1(1)*p1)+(y2/(c2(1)*p2))));
% loop
for i = 2:8
% update calculations
x1(i) = (y1.*p(i-1))./(c1(i).*p1);
x2(i) = (y2.*p(i-1))./(c2(i).*p2);
c2(i) = exp(a.*(x1(i).^2));
c1(i) = exp(a.*(x2(i).^2));
p(i)= 1./((y1./(c1(i).*p1)+(y2./(c2(i).*p2))));
end
  1 commentaire
tejas alva
tejas alva le 24 Mar 2019
x1(i) = (y1.*p(i-1))./(c1(i).*p1);
c1(i) in the for loop would start with c1(2) which has not been initialized. I think if you sort that for all variables, including c2, it should not throw you any error.

Réponses (1)

Anant Upadhyay
Anant Upadhyay le 27 Mar 2019
Hi Cansu,
According to my understanding, you are facing error while calculating x1 because, in the expression
>> x1(i) = (y1.*p(i-1))./(c1(i).*p1);
“x1(i)” depends upon the value of “c1(i)” which is not being initialized/pre-calculated. Similar pattern is repeated for “x2(i)” also.
It may be a case, that for updating “x1(i)” you require “c(i-1)” and not “c(i)”.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by