Hi! How can I show the numerical value of the first derivative of a function in this code?
(-0.1*x^4) - (0.15*x^3) - (0.5*x^2) - (0.25*x) + 1.2
Enter Step Size:
0.5
Enter the point you want to approximate the Derivative:
0.5
clear
clc
format short
syms x
P=input ('Enter the Equation: ','s');
f=inline(P)
g=diff(f(x),x);
h=input ('Enter Step Size: ')
x=input ('Enter the point you want to approximate the Derivative: ')
fprintf('f(x) = %0.4f', f(x))
dy=g(x) % How Can I get the derivative equation of g in numerical answer by substituting the value of x?
fprintf('x(xi+1) = %0.4f', x+(1*h))
fprintf('f(xi+1) = %0.4f', f(x+(1*h)))
FDD=(f(x+(1*h))-f(x))/h;
fprintf('FDD (Truncated) = %0.4f', (f(x+(1*h))-f(x))/h)

댓글 수: 2

dy = subs(g,x,x0)
if you rename x in x0 in the preceeding input-command for x.
Dyuman Joshi
Dyuman Joshi 2024년 1월 5일
inline is not recommended.
Simply define the expression/function by typing or use str2sym.
Don't you think the step size is too big for calculating numerical derivative?

댓글을 달려면 로그인하십시오.

답변 (1개)

Ayush
Ayush 2024년 1월 5일
편집: Ayush 2024년 1월 5일

0 개 추천

Hi Christoppe,
I understand that you want to show the numerical value of the first derivative of a function in the given code.
To do so, you need to substitute the value of “x” into the derivative function “g” and then evaluate it. This can be done by using the “subs” function or by converting the symbolic expression to a function and then evaluating it at the given point.
Refer the modified code below for better understanding:
clear
clc
format short
syms x
% P = input('Enter the Equation: ','s');
P = (-0.1*x^4) - (0.15*x^3) - (0.5*x^2) - (0.25*x) + 1.2;
f = inline(P);
g = diff(f(x),x);
% h = input('Enter Step Size: ');
h = 0.5;
% x_val = input('Enter the point you want to approximate the Derivative: ');
x_val = 0.5;
fprintf('f(x) = %0.4f\n', f(x_val)); % Display the value of the function at x
f(x) = 0.9250
% Calculate the numerical value of the derivative at x
g_val = double(subs(g, x, x_val));
fprintf('g(x) = %0.4f\n', g_val); % Display the numerical value of the derivative
g(x) = -0.9125
fprintf('x(xi+1) = %0.4f\n', x_val + h);
x(xi+1) = 1.0000
fprintf('f(xi+1) = %0.4f\n', f(x_val + h));
f(xi+1) = 0.2000
FDD = (f(x_val + h) - f(x_val)) / h;
fprintf('FDD (Truncated) = %0.4f\n', FDD);
FDD (Truncated) = -1.4500
The "inline" function is not recommended, instead use the "anonymous function". For more information on the “subs” function and "anonymous function" refer the documentation page given below:
Regards,
Ayush

댓글 수: 7

Stephen23
Stephen23 2024년 1월 5일
Two questions:
  1. Why do you use deprecated INLINE ?
  2. Are you TMW staff?
Dyuman Joshi
Dyuman Joshi 2024년 1월 5일
Stephen, I suspect that Ayush is a TMW staff member, as I have seen a staff account named Ayush upvoting the answers from this account (in succession) awhile ago.
They might have been an intern at TMW.
Star Strider
Star Strider 2024년 1월 5일
I saw that as well, and reported it to the appropriate person at TMW.
Dyuman Joshi
Dyuman Joshi 2024년 1월 5일
@Star Strider, Have you gotten any response/update from TMW about it?
Star Strider
Star Strider 2024년 1월 5일
Not yet, other than the person promising to look into it. (I provided the dates and times.) I reported it when I saw it, so a few minutes after it occurred.
Dyuman Joshi
Dyuman Joshi 2024년 1월 5일
I see. Thank you for your response.
Star Strider
Star Strider 2024년 1월 5일
My pleasure!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2022년 8월 4일

댓글:

2024년 1월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by