Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Index exceeds the number of elements (1) HELP

1 Ansicht (letzte 30 Tage)
Cansu Karaca
Cansu Karaca am 24 Mär. 2019
Geschlossen: MATLAB Answer Bot am 20 Aug. 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 Kommentar
tejas alva
tejas alva am 24 Mär. 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.

Antworten (1)

Anant Upadhyay
Anant Upadhyay am 27 Mär. 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)”.

Diese Frage ist geschlossen.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by