how to differentiate this function

조회 수: 2 (최근 30일)
shiv gaur
shiv gaur 2022년 5월 7일
답변: Riccardo Scorretti 2022년 5월 8일
ph=t-z+2*tan(z/zr)-(z/zr)*(r/r0*f)^2;
a=r*exp(-r^2/r0^2*f^2)*cos(ph);
how to differentiate with r in matlab
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2022년 5월 7일
Have you tried anything yet? If yes, show us.
Otherwise provide more info, Which are independent variables? Which are the dependent ones?
Sam Chak
Sam Chak 2022년 5월 8일
@shiv gaur, can you share the link, where a MATLAB function can be used to differentiate symbolic expression or function? This helps me to find a relevant example and help you to solve the mathematical problem.

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

답변 (2개)

KSSV
KSSV 2022년 5월 8일
syms ph(t,z,zr,r,r0,f) a(r,f,r0,ph)
ph = t-z+2*tan(z/zr)-(z/zr)*(r/r0*f)^2;
a = r*exp(-r^2/r0^2*f^2)*cos(ph);
dphdr = diff(ph,r)
dphdr = 
dadr = diff(a,r)
dadr = 

Riccardo Scorretti
Riccardo Scorretti 2022년 5월 8일
Assuming that you want to differentiate ph and a with respect of r, you have several options (and the best option depends on what you want to do: we cannot know in your place):
  1. use your own brain: compute the derivative and program it by hand,
  2. let MATLAB to do it at your place, if you have the Symbolic Computational Toolbox,
  3. derive numerically (this is not elegant and somehow dangerous).
As for the option 2:
syms r z zr r0 f t
ph = t-z+2*tan(z/zr)-(z/zr)*(r/r0*f)^2;
a = r*exp(-r^2/r0^2*f^2)*cos(ph);
ph_r = diff(ph, r)
ph_r = 
a_r = diff(a, r)
a_r = 
Then, you can even ask to Matlab to program the numerical computation in your place:
matlabFunction([a_r phi_r], 'File', 'my_function.m');
As for the option 3, basically you can use finite differences:
fun_ph = @(r) t-z+2*tan(z/zr)-(z/zr)*(r/r0*f)^2;
fun_a = @(r) r*exp(-r^2/r0^2*f^2)*cos(ph);
dr = 1.0E-6; % *** this may be tricky to choice ***
fun_ph_r = @(r) (fun_ph(r+dr) - fun_ph(r)) / dr;
fun_a_r = @(r) (fun_a(r+dr) - fun_a(r) ) / dr;
This is the simplest example (= first order, forward finite differences). You can enjoy a more detailed analysis, and high order formulas, here: ChE 205 — Formulas for Numerical Differentiation

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by