Calculate the derivative of a vector

前端 未结 2 615
滥情空心
滥情空心 2020-12-02 01:31

I have the following function (Viviani\'s curve):

Phi     = @(t)[ cos(t)^2, cos(t)*sin(t), sin(t) ]

Just a check that it\'s valid:

2条回答
  •  -上瘾入骨i
    2020-12-02 02:08

    Here are three ways you can accomplish this. The first uses subs, the second uses a symfun, and the third uses complex step differentiation:

    % Using subs
    syms t
    Phi = [cos(t) cos(t).*sin(t) sin(t)];
    Phi_d2 = diff(Phi,t)
    double(subs(Phi_d2,t,0))
    
    % Using symfun
    syms t
    Phi(t) = [cos(t) cos(t).*sin(t) sin(t)];
    Phi_d2 = diff(Phi,t)
    double(Phi_d2(0))
    
    % Using complex step differentiation
    Phi = @(t)[cos(t) cos(t).*sin(t) sin(t)];
    h = 2^-28;
    cdiff = @(f,x)imag(f(x(:)+1i*h))/h;
    Phi_d2 = cdiff(Phi,0)
    

    You can find a function for performing first- and second-order complex step differentiation on my GitHub: cdiff. Note that complex step differentiation won't work well for higher order derivatives. It's best when one only has a non-differentiable function or needs fast numerical first derivatives.

提交回复
热议问题