I wrote simply code blog, i want to recursive function of it. I want to like this: function [itr] = newton(x,tol)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
I can't write recursive function in Matlab (Please help me)
3 views (last 30 days)
Show older comments


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 Comments
Rik
on 23 Oct 2020
Do you explicitly want a recursive function, or do you just want to implement this algorithm?
Rooter Boy
on 23 Oct 2020
User will enter values of x and tol. I will call function. After i will enter x and tol. Simply, i will enter f(x) and tolerance. These are my inputs. My output is number of iteration. If iteration < tolerance, take output. For example, i called function newton(x,tol). after, i entered values randomly. such as newton(1.8,0.01). My output will be 3. Because 3.iterations is < tolerance. To sum up, I already sent my code blog of newton raphson, so you can see above. I need recursive function of this.
Rik
on 24 Oct 2020
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
on 24 Oct 2020
Sir, I need recursive function. For example, i have a question like this. I should call recursive function.
You know recursive function should contain your function. You do not use while loop.


Rik
on 24 Oct 2020
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
on 24 Oct 2020
I'm a master student in computer engineering. This is my homework. I agree with you. But i need do with recursive function.
Rooter Boy
on 24 Oct 2020
function [itr] = newton(x,tol) how can we apply this according to the above question. Maybe we can use ifelse instead while.
function [itr] = newton(x,tol)
x=0;
tol=0;
if hata>tol.......
I don't know how to adapt it.
Rik
on 24 Oct 2020
Just because you aren't getting the help you were hoping for dosn't make this question not appropriate, so I removed your flag.
Stephen23
on 24 Oct 2020
Answers (1)
Rik
on 24 Oct 2020
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 Comments
Rooter Boy
on 24 Oct 2020
Sir, I know principles of recursive function. How do we define my function? I don't understand it.
Rik
on 24 Oct 2020
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
on 24 Oct 2020
Edited: Rooter Boy
on 24 Oct 2020
I tried write but result is false. output should be 3 for solution. I don't know how to call. Firstly i define function but i can't.
function [itr] = newton(x,tol)
hata=tol+1;
n=1;
itr=0;
if 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;
disp('itr');
end
Rik
on 24 Oct 2020
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
Rik
on 24 Oct 2020
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
on 24 Oct 2020
Sir, I found solutions myself. This is advanced level coding. I want to delete these questions. If you help me about this topic, i will be happy.
Thanks for everything by the way.
Rik
on 24 Oct 2020
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)