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

88 vues (au cours des 30 derniers jours)
reema shrestha
reema shrestha le 1 Juil 2017
Commenté : Walter Roberson le 24 Nov 2020
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?

Réponse acceptée

John D'Errico
John D'Errico le 1 Juil 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 commentaires
reema shrestha
reema shrestha le 1 Juil 2017
I also tried using vpa.
syms x
f = x^2-3*x+2;
g=diff(f);
t =0.00005;
i=1;
j=1;
a=vpa(subs(f,x,i-1));
b=vpa(subs(g,x,i-1));
so when i use this it shows the value of a and b. But then when i initialize the value of x.
x(1)=0;
and try for the value of b.
b=vpa(subs(g,x,i-1))
Then the value of b is,
2.*x-3.
Sorry,the question has somewhat deviated from the original one.
Walter Roberson
Walter Roberson le 1 Juil 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)

Connectez-vous pour commenter.

Plus de réponses (2)

Karan Gill
Karan Gill le 5 Juil 2017
Modifié(e) : Karan Gill le 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 commentaires
Sarah Johnson
Sarah Johnson le 12 Fév 2020
This does not print out the values at g, when I plug it in it prints out the functions themselves
Walter Roberson
Walter Roberson le 13 Fév 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.

Connectez-vous pour commenter.


Hamza saeed khan
Hamza saeed khan le 24 Nov 2020
Undefined function or variable 'syms'.
Error in difff (line 2)
syms y(x)
  1 commentaire
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by