MATLAB Answers

How to find the derivative of the function at some value of x?

1,283 views (last 30 days)
I am trying to write a code for the Newton-Raphson iteration method. So at first i created a function
function y=f1(x)
y=x^2-3*x+2;
Then in next M-file,i wrote
syms x;
x(1)=0;
i=1;
x(i);
z=f1(x(i))
But then I try to differentiate the function
df= eval((subs(diff(f1,x,x(i)),x,x(i))))
it shows error.
how do i solve the problem? I tried various ways but none of them worked. How do i call the function from previous M-file?

Accepted Answer

John D'Errico
John D'Errico on 1 Jul 2017
MATLAB cannot do symbolic differentiation on an m-file. That would in general be impossible, since you could stick anything you wanted in there.
You have two choices:
1. Perform the differentiation in advance, using the sym tools. Then you can pass the derivative function also to your NR code.
2. Inside the NR code, use finite differencing to compute an approximation to the derivative. This is almost always adequate for Newton schemes, although care must be taken to get a good estimate, using an appropriate step size. Also, central differences are considerable more accurate, so use them whenever possible.
  2 Comments
Walter Roberson
Walter Roberson on 1 Jul 2017
Consider the lines
a = 5;
b = a + 2;
a = 11;
Then what is the value of b afterwards? Is it now 13, because a is 11 and b = a + 2? Or is it 7 because the value of a at the time of the operation was looked up and used?
Likewise when you use
a = sym('a');
b = a + 2;
a = 11;
then what is the value of b? Is it now 11, because a is 11 and b = a + 2? Or is it sym('a')+2 because the value of a at the time of the operation was looked up and used?
If you were to use
a = sym('a');
b = a + 2;
a = 11;
subs(b, a, 5)
then b is sym('a')+2 and a has become (numeric) 11, and you are then asking to do
subs(b, 11, 5)
which does not change the sym('a') inside b.
When you assigned numeric 11 to a then it lost its identity as sym('a').
Moral of the story:
Never assign a new value to something that was previously a sym and expect anything to have been updated with the new value. subs() the new value in for the symbol instead:
subs(g, x, 0)

Sign in to comment.

More Answers (2)

Karan Gill
Karan Gill on 5 Jul 2017
Edited: Karan Gill on 17 Oct 2017
To differentiate a function and then find the value, use symbolic functions. For details, see https://www.mathworks.com/help/symbolic/create-symbolic-functions.html
>> syms f(x)
>> f(x) = x^2 -3*x + 2
f(x) =
x^2 - 3*x + 2
>> g = diff(f)
g(x) =
2*x - 3
>> g(2) % value at x = 2
ans =
1
>> xValues = [-10 5 88]
xValues =
-10 5 88
>> g(xValues)
ans =
[ -23, 7, 173]
  3 Comments
Walter Roberson
Walter Roberson on 13 Feb 2020
>> f(x) = x^2 -3*x + 2
f(x) =
x^2 - 3*x + 2
>> g = diff(f)
g(x) =
2*x - 3
>> g(7)
ans =
11
Works for me.
When I was glancing at your other recent post, it looked to me as if you have a different question: namely to determine the value of a constant in the formula such that the known boundary value was satisfied.

Sign in to comment.


Hamza saeed  khan
Hamza saeed khan on 24 Nov 2020
Undefined function or variable 'syms'.
Error in difff (line 2)
syms y(x)
  1 Comment
Walter Roberson
Walter Roberson on 24 Nov 2020
If you get that answer, then you do not have the Symbolic Toolbox installed and licensed.
There are some third-party symbolic packages that can be used with MATLAB with varying degrees of difficulty. Some of them are commercial; some of them are free (such as symbolic python).
If you do not have a symbolic software package of some kind, then derivatives can be calculated for some restricted cases such as polynomials or piecewise polynomials (including cubic spline); beyond that you start having to do numeric approximations of derivatives.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!