필터 지우기
필터 지우기

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

조회 수: 50 (최근 30일)
reema shrestha
reema shrestha 2017년 7월 1일
댓글: Walter Roberson 2020년 11월 24일
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에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by