How to calculate derivative of function inside of another function

I have been searching for how to calculate the symbolic derivative of a function to use with my Newton's Method function. Currently I have to hand calculate the derivatives and pass it into my Newton's Method Function, I save both the original function and derivative as separate functions.
My function code is:
% M- file :f5.m
% define function to evaluate real zero using Newton's method
function y = f5(x)
y = x^3-2*x+2;
(derivative is hand calculated and entered the same way)
My Newton's method code starts with:
function q = newton(f, fprime, x0, maxim, epsi, delta)
f,fprime are my defined functions
x0 is starting point
maxim,epsi,delta are my tolerances
from here my function works great but I want the code to work like this:
function q = newton(f, x0, maxim, epsi, delta)
fprime = diff(f,x) <-calculate derivative of f and save as fprime
so that fprime = 3x^2-2 and I can calculate fprime at a point xo by feval(f,x0)
Any help on how to do this would be appreciated greatly. Thanks!

 Accepted Answer

function q = newton(f, x0, maxim, epsi, delta)
syms x
fprime = diff(f(x),x) % calculate derivative of f and save as fprime
This is not going to work unless f(x) is a simple formula -- for example, f(x) must not have any "if" or "for" statements that depend upon x.

4 Comments

Using the code you have provided gives me this error:
??? Error using ==> sym.sym>notimplemented at 2514
Function 'subsindex' is not implemented for MuPAD symbolic objects.
Error in ==> sym.sym>sym.subsindex at 1344
notimplemented('subsindex');
Error in ==> newton at 20
fprime1 = diff(f(x),x)
Error in ==> prog1 at 40
newton ('f1',x0, maxim, epsi, delta)
This is the function:
% M- file :f1.m
% define function to evaluate real zero using Newton's method
function y = f1(x)
y = -2*exp(-2*x) + 2*x;
and here is my Newton file:
function q = newton(f, x0, maxim, epsi, delta)
%calculate first and second derivative to find critical points
syms x
fprime1 = diff(f(x),x)
Your original problem statement called for f to be your defined function. In this code you show here, your f is instead a string. A string is not a function (though it might have a content that is the _name_ of a function.)
newton (@f1,x0, maxim, epsi, delta)
If you are required to pass in a string instead, then you are going to have to use feval():
function q = newton(f, x0, maxim, epsi, delta)
syms x
fprime = diff( feval(f, x), x) % calculate derivative of f and save as fprime
Thank you! Having no problems calculating the derivatives now, but 1 more question, when I enter an expression in with a cubic root (f(x)=(3/4)*x^(4/3)) I get the correct derivative(x^(1/3), but I cannot figure out the best way to get the real roots and plot the function.
v= subs(fprime,x1); %find next value of f'(p)
Produces imaginary results and I only want the real ones, likewise ezplot(fprime, [leftbound-.5, rightbound+.5]) does not graph the real values of the cubic, which is what I want.
I used (3/4)*sign(x).*(abs(x)).^(4/3) as my function in the original code but Matlab will not take the derivative of this function. The program works great for any functions not involving odd roots. Thanks again in advance!
Assign to a variable the potential values (which must not contain any variables for this solution to work out). double() the variable, and use logical indexing to select from that list the double precision value whose imag() is 0. The remaining entry indices can be used to index back to the symbolic forms of you want the analytical values as outputs.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!