Index must be a positive integer or logical. Very simple program.
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Brian B
on 4 Mar 2013
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.
More Answers (2)
Image Analyst
on 4 Mar 2013
Edited: Image Analyst
on 4 Mar 2013
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).
Azzi Abdelmalek
on 4 Mar 2013
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!