Index must be a positive integer or logical. Very simple program.

Hey guys, I am pretty new to Matlab and to this Forum, so excuse me my question if stupid and obvious. I need to write a recursive code for a Newton sequence for f(x) (found in the matlab-code). Now the principe is simple, and I should get a line-vector with the entries a(1), a(2), ... Again: I am really new to Matlab, so maybe the error is really simple :P I googled for the same error, and simply don't get other peoples codes with the same error, so the answers are pretty useless for me.
function [a]=a5folge(n)
function [f]=f(x)
f(x)=((x^5)/5)-((2*x^3)/3)+x;
end
function [f1]=f1(x)
f1(x)=(x^4)-(2*x^2)+1;
end
function [psi]=psi(x)
psi(x)=x-(f(x)/f1(x));
end
a=zeros(n+1);
a(1)=sqrt((25+2*sqrt(55))/27);
for k=1:n
a(k+1)=psi(a(k));
end
end
thx in advance.

 Accepted Answer

You cannot use the same name for a function and for its output argument. Try
function [val]=f(x)
val=((x^5)/5)-((2*x^3)/3)+x;
end
and similarly for the others.

1 Comment

Thanks a lot :) Now it's working how it should more or less :P.

Sign in to comment.

More Answers (2)

You don't say f(x) or f1(x), just say f and f1.
function fout = f(x)
fout = ((x^5)/5)-((2*x^3)/3)+x;
end
When you had the (x) in there, it was thinking that you were trying to access the xth element of an existing array called f, which you don't have yet. And don't have the output be the same as the function name - this isn't Visual Basic (where you could do that).

1 Comment

Ok, thanks :) Going to remember that array-element-explanation. Helpful :)

Sign in to comment.

function a=a5folge(n)
a=zeros(n+1);
a(1)=sqrt((25+2*sqrt(55))/27);
for k=1:n
a(k+1)=psi(a(k));
end
end
function psi1=psi(x)
psi1=x-f(x)/f1(x);
end
function ff=f(x)
ff=((x^5)/5)-((2*x^3)/3)+x;
end
function ff1=f1(x)
ff1=(x^4)-(2*x^2)+1;
end

Categories

Community Treasure Hunt

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

Start Hunting!