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

86 次查看(过去 30 天)
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?

采纳的回答

John D'Errico
John D'Errico 2017-7-1
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 个评论
reema shrestha
reema shrestha 2017-7-1
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 2017-7-1
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)

请先登录,再进行评论。

更多回答(2 个)

Karan Gill
Karan Gill 2017-7-5
编辑:Karan Gill 2017-10-17
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 个评论
Sarah Johnson
Sarah Johnson 2020-2-12
This does not print out the values at g, when I plug it in it prints out the functions themselves
Walter Roberson
Walter Roberson 2020-2-13
>> 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.

请先登录,再进行评论。


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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by