Azzera filtri
Azzera filtri

f = x^2024. Write a loop to find how many times f needs to be differentiated to get a result of zero.

3 visualizzazioni (ultimi 30 giorni)
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

Risposte (3)

Umar
Umar il 4 Lug 2024
Modificato: Walter Roberson il 4 Lug 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 Commenti
Brian Smyth
Brian Smyth il 4 Lug 2024
OK thanks Paul. I got code from Umar. Didn't know you could run code here directly. Sorry. My first time posting. So basically, symvar can never return 0 in this case which is why it runs indefinitely.
Thanks very much.
Brian.
Paul
Paul il 4 Lug 2024
Modificato: Paul il 5 Lug 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.
Because Fn is a symbolic object, check out isAlways for one approach thay may help.

Accedi per commentare.


Umar
Umar il 4 Lug 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.

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.

I will look forward to your response and help. Thanks again.

  5 Commenti
Rik
Rik il 5 Lug 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
Walter Roberson il 5 Lug 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.']);
The function needs to be differentiated 2025 times for the result to be 0.

Accedi per commentare.


Umar
Umar il 5 Lug 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.

Community Treasure Hunt

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

Start Hunting!

Translated by