Filter löschen
Filter löschen

How can I fix the "Subscript indices must either be real positive integers or logicals." I am getting the errors my j(k) and l(k). my program works when I change the range but i only get ending term, any point in the right direction would be helpfu

1 Ansicht (letzte 30 Tage)
z(1) = 1.5;
for k = 1:6
fprintf('%d %5.8f %5.8f %5.8f\n',n , z(k), e(k), p(k));
z(k+1) = (.5)*z(k) + sqrt(2)/2*z(k);
x(k) = z(k+1);
e(k) = z(k+1) - sqrt(2);
j(k)= log((e(k))/log(e(k-1)));
l(k)= log(e(k-1))/log(e(k-2));
p(k) = j(k)/l(k);
end

Akzeptierte Antwort

Robert U
Robert U am 27 Sep. 2017
Bearbeitet: Robert U am 27 Sep. 2017
Hi Janvier Solaris,
the main problem despite of posting non-executable code is the reference to e(k-2) for l(k) calculation. Since indices have to a positive integers or logicals it will not work for k <= 2. That code snippet does:
z(1) = 1.5;
for k = 1:6
z(k+1) = (.5)*z(k) + sqrt(2)/2*z(k);
x(k) = z(k+1);
e(k) = z(k+1) - sqrt(2);
if k > 2
j(k)= log((e(k))/log(e(k-1)));
l(k)= log(e(k-1))/log(e(k-2));
p(k) = j(k)/l(k);
fprintf('%d %5.8f %5.8f %5.8f\n',k , z(k), e(k), p(k));
end
end
Kind regards,
Robert
  1 Kommentar
Janvier Solaris
Janvier Solaris am 27 Sep. 2017
Thank You @Robert, I was afraid that was my issue but instead of using an if statement I was starting at k = 3 to try and fix it but it was not working. Your fix cleared it. I am fairly new to Matlab and your help is much appreciated

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by