positive index error in matlab
1 view (last 30 days)
Show older comments
shobi swaminathan
on 25 Jan 2014
Commented: shobi swaminathan
on 26 Jan 2014
clc;
clear all;
R=3;
beta=8;
x1=1:1:10;
for x=1:length(x1)
x=round(x);
f(x)=1-(exp(-((2^(R))-1)/x)^(beta/2));
df(x)=(beta/2)*(((2^R)-1)^beta)*(x^(-(beta+1)))*(exp(-((2^(R))-1)/x)^(beta/2));
format compact
disp(' iterate x f(x) est. error ')
for n = 0:5
s =( f(x)/df(x));
sprintf(' %2d %2.10f %2.10f % 2.10f \n', n,x,f(x),s)
x = x-s;
end
end
error is:
Attempted to access f(-62718.3); index must be a positive integer or logical.
Error in newton (line 15)
s =( f(x)/df(x));
plzz suggest a solution
0 Comments
Accepted Answer
Amit
on 25 Jan 2014
In MATLAB f(x) usage is to (if f is a matrix) access the xth element in the matrix. And Here thats why you're getting this error.
2 Comments
Amit
on 25 Jan 2014
If I was you, I'd do something like this:
R=3;
beta=8;
x1=1:1:10;
x = 1; % Intial value of x
for n=1:length(x1)
f=1-(exp(-((2^(R))-1)/x)^(beta/2));
df=(beta/2)*(((2^R)-1)^beta)*(x^(-(beta+1)))*(exp(-((2^(R))-1)/x)^(beta/2));
format compact
disp(' iterate x f(x) est. error ')
% for n = 0:5
s =( f/df);
disp(sprintf(' %2d %2.10f %2.10f % 2.10f \n', n,x,f,s));
x = x-s;
%end
end
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!