I can't write recursive function in Matlab (Please help me)
古いコメントを表示


I tried many times but couldn't write a recursive function in Matlab. I don't know how to do it. My friends, I would be glad if someone could help me.
12 件のコメント
Rooter Boy
2020 年 10 月 23 日
Rik
2020 年 10 月 23 日
Do you explicitly want a recursive function, or do you just want to implement this algorithm?
Rooter Boy
2020 年 10 月 23 日
Rooter Boy
2020 年 10 月 23 日
Rik
2020 年 10 月 24 日
At the very least you will have to start your newton.m file with the line
function itr=newton(x,tol)
Then the rest of you code can calculate the number of iterations needed. It doesn't sound like you need a recursive function. The loop you have there should work.
Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
I don't speak the language in this screenshot, but I don't see any indication that the only way to solve this is recursion. I see no indication that a while loop wouldn't work. You're free to implement it any way you like. What is your question?
Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
Then implement it as a recursive function. What is your question?
Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
Just because you aren't getting the help you were hoping for dosn't make this question not appropriate, so I removed your flag.
回答 (1 件)
Rik
2020 年 10 月 24 日
In case you just want to know how to create a recursive function at all:
function n=I_call_myself(n)
disp(n)
if n>0
n=I_call_myself(n-1);
else
disp('n has reached 0')
end
end
8 件のコメント
Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
I have no clue how you should force this algorithm in a recursive function that makes any sense. This seems the perfect place for a while loop. The point is that in a recursive function you have the function call itself. At some point that recursion should stop, in your can when hata<tol.
Rooter Boy
2020 年 10 月 24 日
編集済み: Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
You need to use while instead of if. For readability I would also suggest you include the closing end for you function.
function [itr] = newton(x,tol)
hata=tol+1;
n=1;
itr=0;
while hata>tol
x(n+1)=0.2*(4*x(n)+(32/power(x(n),4)));
hata=abs(x(n+1)-x(n));
n=n+1;
itr=itr+1;
end
disp(itr);
end
If you want to make it recursive you should probably have two outputs of your function, or use the length of x to define itr and n:
%untested code:
function [itr,x] = newton(x,tol)
hata=tol+1;
n=numel(x);
itr=n-1;
x(n+1)=0.2*(4*x(n)+(32/power(x(n),4)));
hata=abs(x(n+1)-x(n));
if hata>tol
[itr,x] = newton(x,tol);
return
end
disp(itr);
end
Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
What was the error? You need to provide the full error message. If you don't, we can only give you this advice: change the code.
Rooter Boy
2020 年 10 月 24 日
Rik
2020 年 10 月 24 日
Sorry, once you receive an answer it is considered rude to delete it, so the system will not allow you to do that. People with a similar question might find the posts here helpful.
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

