f = x^2024. Write a loop to find how many times f needs to be differentiated to get a result of zero.
1 view (last 30 days)
Show older comments
A sequence of functions F0, F1, F2...is defined as follows, for x a positive real number:#
- F0 = x^2024
- For n>=1, the function Fn is the derivative of F(n-1), with respect to x.
Find the first value of n for which Fn = 0
Answers (3)
Umar
on 4 Jul 2024
Edited: Walter Roberson
on 4 Jul 2024
Hi Brian,
To solve this problem, you need to understand that the nth derivative of a function is obtained by taking the derivative of the (n-1)th function in the sequence. Then, you can use this recursive definition to find the first value of n for which the nth derivative becomes zero.Here is the MATLAB code to find the first value of n for which the nth derivative of a function in the sequence becomes zero:
syms x; n = 0; Fn = x^2024;
while true
n = n + 1;
Fn = diff(Fn, x);
if symvar(Fn) == 0
break;
end
end
disp(['The first value of n for which Fn = 0 is: ', num2str(n)]);
So, the above code initializes a symbolic variable 'x' and sets 'n' to 0. It defines the symbolic function 'Fn' as x^2024. The 'while' loop increments 'n' and computes the derivative of 'Fn' with respect to 'x' using the 'diff' function. The loop continues until the derivative no longer contains 'x', indicating it has reached a constant value.
6 Comments
Paul
on 4 Jul 2024
Edited: Paul
on 5 Jul 2024
Hi Brian,
No need to apologize. On this forum you're most likely to get help fastest by posting the code in the question, running it, and explaining where or what the problem is.
To use code formating, select a block of code and then hit alt-enter, or hit alt-enter and then copy in the code to highlighted area. Posting an .m-file is fine, but it's easier for others, IMO, to just post the code directly in the question/answer/comment, especially if it's not too long.
Yes, the loop will run forever because the condition in the if statement using symvar will never be true.
Umar
on 4 Jul 2024
Hi Paul,
After reviewing your comments and taking your constructive criticism into account, I updated the code. Please check if there are any bugs or what needs to be fixed. Since the purpose of this forum is sharing knowledge with each other.
![](/matlabcentral/answers/uploaded_files/1728051/IMG_6988.jpeg)
Using abs(subs(Fn, x, 0)) < 1e-10 instead of subs(Fn, x, 0) == 0 accounts for numerical errors and consider values close to zero as effectively zero.
You mentioned earlier about running the code on this forum, please tell me which one of the above icons accomplishes it.
![](/matlabcentral/answers/uploaded_files/1728056/IMG_6989.jpeg)
I will look forward to your response and help. Thanks again.
5 Comments
Rik
on 5 Jul 2024
Dear Umar, you seem to be working with the legacy editor, which is the fallback method on mobile devices that do not support the new editor. The controls for this editor are explaned here.
The Run feature was added later, and apparently the help page was not updated. You will see a green triangle between the 'insert' and 'help' sections of the toolbar.
Also, why exactly are you claiming to be insulted? And what exactly in the flagged comment refers to race, color, or religion? And you still seem to be posting answers that look like comments to me. An answer should be a self-contained solution to the original question, as their order can change after being accepted or receiving votes.
Walter Roberson
on 5 Jul 2024
syms x f
f = x^2024; % Define the function you want to differentiate
n = 0; % Initialize the differentiation counter
while true
df = diff(f, n); % Differentiate the function 'f' 'n' times
if isAlways(df == 0, Unknown = false) % Check if the result is 0
break; % Exit the loop if the result is 0
end
n = n + 1; % Increment the counter
end
disp(['The function needs to be differentiated ', num2str(n), ' times for the result to be 0.']);
Umar
on 5 Jul 2024
No problem Brian, glad to help out. Hope I will win my mathworks t-shirt. Good luck with your future endeavors. If you further assistance, we are still here to help you out.
2 Comments
Umar
on 5 Jul 2024
Hi Brian,
Thanks for sharing this information. Glad to know everything is working out for you.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!