Why am I getting Error "Array indices must be positive integers or logical values"?

N=[5 6 7 8 9 10 11 12 13 14 15];
fprintf('%5s\t%15s\t%15s\t%15s\n','n','T(h)','En','En/E(n-1)')
trueValue = exp(-2.5).*(-sin((2.5).^3)-sin((2.5).^2)+2.*(2.5).*cos((2.5).^3)+3.*(2.5).^2.*cos((2.5).^3)+3.*(2.5)-3);
err1=-1;
for i=1:length(N)
n=N(i);
x=2.5;
h=2.^(-n);
f=exp(-x).*(sin(x.^3)+sin(x.^2)-3.*x);
fprime = MyCenteredDifference(f, x, h);
err2=abs(fprime-trueValue);
ratio = err2/err1;
err1=err2;
if i==1
ratio=1;
end
fprintf('%5d\t%15.10f\t%15.10f\t%15.10f\n',n,I,err1,ratio)
end
_I'm getting an error for the "fprime = MyCenteredDifference(f, x, h);" line, and I don't know why it says "Array indices must be positive integers or logical values".
Please help me.

Respuestas (1)

You have not given/ shown us the function MyCenteredDifference, so we cannot check the function. But the error is simple and staright. In matlab array indices must be positive integers and logicals. In your case, you have voilated that crieteria and ended up with error.
EXample:
A = rand(1,10) ;
A(1) % no error
ans = 0.0417
A(10) % no error
ans = 0.8617
A(1.5) % error, index cannot be fraction
Array indices must be positive integers or logical values.
Use of logicals:
idx = A > 0.5 ;
A(idx) % no error
A(0) % error index cannot be double 0, 0 is not logical here
In the function, check where index is voilating the rule.

2 comentarios

function fprime = MyCenteredDifference(f, x, h)
fprime = (f(x+h) - f(x-h)) ./ (2*h);
end
Here is my function.
Can you plz check?
Your f is not a function handle, it is a number as you have already substitued x in it. You may follow like shown below.
N=[5 6 7 8 9 10 11 12 13 14 15];
fprintf('%5s\t%15s\t%15s\t%15s\n','n','T(h)','En','En/E(n-1)')
n T(h) En En/E(n-1)
trueValue = exp(-2.5).*(-sin((2.5).^3)-sin((2.5).^2)+2.*(2.5).*cos((2.5).^3)+3.*(2.5).^2.*cos((2.5).^3)+3.*(2.5)-3);
err1=-1;
f=@(x) exp(-x).*(sin(x.^3)+sin(x.^2)-3.*x); %<--- function handle
for i=1:length(N)
n=N(i);
x=2.5;
h=2.^(-n);
fprime = MyCenteredDifference(f, x, h);
err2=abs(fprime-trueValue);
ratio = err2/err1;
err1=err2;
if i==1
ratio=1;
end
fprintf('%5d\t%15.10f\t%15.10f\t%15.10f\n',n,i,err1,ratio)
end
5 1.0000000000 0.9039129196 1.0000000000 6 2.0000000000 0.8406641934 0.9300278547 7 3.0000000000 0.8245927387 0.9808824322 8 4.0000000000 0.8205585495 0.9951076585 9 5.0000000000 0.8195489799 0.9987696557 10 6.0000000000 0.8192965236 0.9996919570 11 7.0000000000 0.8192334055 0.9999229606 12 8.0000000000 0.8192176257 0.9999807384 13 9.0000000000 0.8192136808 0.9999951845 14 10.0000000000 0.8192126946 0.9999987961 15 11.0000000000 0.8192124480 0.9999996990
function fprime = MyCenteredDifference(f, x, h)
fprime = (f(x+h) - f(x-h)) ./ (2*h);
end

Iniciar sesión para comentar.

Categorías

Más información sobre Communications Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2022a

Preguntada:

el 8 de Abr. de 2022

Comentada:

el 8 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by