Problem with derivative function

10 vues (au cours des 30 derniers jours)
Alexis
Alexis le 26 Fév 2023
Modifié(e) : Stephen23 le 26 Fév 2023
Hello, I have a problem with dfunc(x) which is my derivative function. The problem is that when using derivative we have to use symbolic value but I want dfunc(x) to actually return f'(x) which is a numerical result.I tried to use subs but it would give me answers like 2*2^(1/2) - (2*exp(1/2))/(exp(1/2) - 1)^2 instead of the actual numerical value. How can I solve it?
  1 commentaire
Stephen23
Stephen23 le 26 Fév 2023
Modifié(e) : Stephen23 le 26 Fév 2023
"How can I solve it?"
Look at the list of functions in the symbolic toolbox:
particularly under the title "Conversion Between Symbolic and Numeric", e.g.:

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 26 Fév 2023
What you don't seem to appreciate is that in the eyes of the symbolic toolbox, something like 2^(-1/2) Is a numerical result. Sort of. Yes, I suppose the difference is a subtle one. There are no unknown parameters in there.
What you want is a floating point result. For that you can use a tool like VPA to turn the result into a symbolic floating point number, so a high precision float.
X = str2sym('2^(-1/2)')
X = 
vpa(X)
ans = 
0.70710678118654752440084436210485
Or you can use double, which converts the result into a double precision float.
format long g
double(X)
ans =
0.707106781186548

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by