Borrar filtros
Borrar filtros

how to calculate a derivative

508 visualizaciones (últimos 30 días)
Nasir Qazi
Nasir Qazi el 26 de Feb. de 2012
Comentada: Sergio E. Obando el 15 de Jun. de 2024
can some one guide me how to calculate a derivative and integration in matlab . can you please give a little example.
  1 comentario
Jan
Jan el 26 de Feb. de 2012
Symbolically or numerically?

Iniciar sesión para comentar.

Respuesta aceptada

bym
bym el 26 de Feb. de 2012
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 comentarios
Jan
Jan el 27 de Feb. de 2012
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 el 15 de Jun. de 2024
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:

Iniciar sesión para comentar.

Más respuestas (3)

Magdalena Glinska
Magdalena Glinska el 16 de Nov. de 2020
f = @(x) sin(x);
second_derivative_f = matlabFunction(diff(sym(f)));

Hamza saeed khan
Hamza saeed khan el 24 de Nov. de 2020
syms x
f = x;
diff(f,x)
why this code give me error as;
>>Error in diff (line 1)
syms x
  2 comentarios
Dheeresh agarwal
Dheeresh agarwal el 22 de Dic. de 2020
diff(f,1)
Walter Roberson
Walter Roberson el 22 de Dic. de 2020
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.

Iniciar sesión para comentar.


Achimsettythanmay
Achimsettythanmay el 14 de Nov. de 2022
Editada: Walter Roberson el 15 de Nov. de 2022
syms x real
f = 1/x
f = 
int(f,1,2) % integration
ans = 
diff(f) %differentiation
ans = 

Community Treasure Hunt

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

Start Hunting!

Translated by