Index must be a positive integer or logical. Very simple program.
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Brian B
2013-3-4
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.
更多回答(2 个)
Image Analyst
2013-3-4
编辑:Image Analyst
2013-3-4
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
2013-3-4
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!