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

조회 수: 1 (최근 30일)
Vladislav
Vladislav 2013년 3월 4일
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.

채택된 답변

Brian B
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.
  댓글 수: 1
Vladislav
Vladislav 2013년 3월 4일
Thanks a lot :) Now it's working how it should more or less :P.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
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).
  댓글 수: 1
Vladislav
Vladislav 2013년 3월 4일
Ok, thanks :) Going to remember that array-element-explanation. Helpful :)

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by