can some one guide me how to calculate a derivative and integration in matlab . can you please give a little example.

 채택된 답변

bym
bym 2012년 2월 26일

2 개 추천

Symbolically
syms x real
f = 1/x
int(f,1,2) % integration
ans =
log(2)
diff(f) %differentiation
ans =
-1/x^2
[edit - amplification]
syms x a b real positive
f = 1/x
f =
1/x
int(f) % without limits
ans =
log(x)
int(f,a,b) % with limits
ans =
log(b) - log(a)
fn = matlabFunction(f) % convert symbolic to anonymous function
fn =
@(x)1./x
quadgk(fn,1,2) % integrate numerically
ans =
0.6931
log(2) % previous result from symbolic integration
ans =
0.6931
(fn(2+1e-6)-fn(2))/1e-6 %numerical derivative at fn(2)
ans =
-0.2500
subs(diff(f),2) %substitute 2 into symbolic result previously obtained
ans =
-0.2500

댓글 수: 5

Nasir Qazi
Nasir Qazi 2012년 2월 27일
inside int(f,1,2) what exactly this 1,2 stand for in here. 2ndly what do you means by symbolically of numerically .?, right and wht if we have limits in integration, how can we solve that expression .?
Walter Roberson
Walter Roberson 2012년 2월 27일
int(f,1,2) is integrate the symbolic expression f over the definite interval from 1 to 2.
Nasir Qazi
Nasir Qazi 2012년 2월 27일
in case of limits in integration then ?
Jan
Jan 2012년 2월 27일
Yes, Nasir, then the integral is calculated from 1 to 2. "Symbolically" mean calculations with symbols, usually characters. The result is a formula. "Numerically" means, that you calculate a numerical value, a number.
Sergio E. Obando
Sergio E. Obando 2024년 6월 15일
Just here to suggest some recent resources, There is a nice section in the Symbolic Math Toolbox with additional examples for differentiation as well as a FEX curriculum on this topic:

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

추가 답변 (3개)

Magdalena Glinska
Magdalena Glinska 2020년 11월 16일

1 개 추천

f = @(x) sin(x);
second_derivative_f = matlabFunction(diff(sym(f)));
Hamza saeed khan
Hamza saeed khan 2020년 11월 24일

0 개 추천

syms x
f = x;
diff(f,x)
why this code give me error as;
>>Error in diff (line 1)
syms x

댓글 수: 4

Dheeresh agarwal
Dheeresh agarwal 2020년 12월 22일
diff(f,1)
Walter Roberson
Walter Roberson 2020년 12월 22일
You probably do not have the symbolic toolbox installed or licensed.
Also, you accidentally named your script diff.m which is going to conflict with calling diff() to differentiate.
belvia
belvia 2025년 5월 12일

même chez moi le diff là ne donne pas

Steven Lord
Steven Lord 2025년 5월 12일
"même chez moi le diff là ne donne pas"
Please show us the exact code you're running and all the text displayed in red in the Command Window (and if there are any warning messages displayed in orange, please show us those too) that you see when you run that code. The exact text may be useful and/or necessary to determine what's going on and how to avoid the warning and/or error.

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

Achimsettythanmay
Achimsettythanmay 2022년 11월 14일
편집: Walter Roberson 2022년 11월 15일

0 개 추천

syms x real
f = 1/x
f = 
int(f,1,2) % integration
ans = 
diff(f) %differentiation
ans = 

질문:

2012년 2월 26일

댓글:

2025년 5월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by