Derivating a variable with respect to other vars

1 view (last 30 days)
I have the following variable:
Which I want to derivate with respect to UMin and with respect to a1 , and then to do the following:
When all the deltas are defined variables.
What I am lacking is, how to derivate? I only saw the option to use syms, but I don't think I can use it for my purpose.

Answers (1)

Riccardo Scorretti
Riccardo Scorretti on 8 Apr 2022
In my opinion, you can do it analytically:
syms Umin a1
tau = sqrt( (2*Umin-Umin^3+Umin^4+2*Umin^2-8) / (2*a1*Umin^3*(Umin^2+4)^1.5) )
tau = 
d_tau = simplify(diff(tau, a1))
d_tau = 
fun_d_tau = matlabFunction(d_tau)
fun_d_tau = function_handle with value:
@(Umin,a1)sqrt(2.0).*1.0./Umin.^3.*1.0./a1.^2.*1.0./(Umin.^2+4.0).^(3.0./2.0).*1.0./sqrt((1.0./Umin.^3.*1.0./(Umin.^2+4.0).^(3.0./2.0).*(Umin.*2.0+Umin.^2.*2.0-Umin.^3+Umin.^4-8.0))./a1).*(Umin.*2.0+Umin.^2.*2.0-Umin.^3+Umin.^4-8.0).*(-1.0./4.0)
Example:
Umin = 10 ; a1 = 2 ; format long ; fun_d_tau(Umin,a1)
ans =
-0.011649625737107
Otherwise, of course you can do it by finite differences, but the result will be necessarily less accurate. Most importantly, in order to achieve a decent accuracy, in practice you must know at least the order of magnitude of a1. Perhaps the simplest way is:
da1 = 1.0E-7; % *** this depends on the order of magnitude of a1 ***
fun_tau = @(Umin,a1) sqrt( (2*Umin-Umin.^3+Umin.^4+2*Umin.^2-8) / (2*a1.*Umin.^3.*(Umin.^2+4).^1.5) );
fun_d_tau = @(Umin,a1) ( fun_tau(Umin,a1+da1) - fun_tau(Umin,a1-da1) ) / (2.0*da1);
Example:
Umin = 10 ; a1 = 2 ; fun_d_tau(Umin,a1)
ans =
-0.011649625743237
Notice that once you have created the object fun_d_tau, you don't have to care anymore of the variable da1. For instance, you can modify its value, or even clear it: the function fun_d_tau will continue to work as well:
clear da1
Umin = 10 ; a1 = 2 ; fun_d_tau(Umin,a1)
ans =
-0.011649625743237

Community Treasure Hunt

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

Start Hunting!