Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

arrays - Calculate the derivative of a vector

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:

s = linspace(0,T,1000);
plot3(cos(s).^2, cos(s).*sin(s), sin(s));

How to derivate the function Phi (maybe multiple times), which represents Viviani's curve in a point t where t goes from 0 to 2*pi? Did I defined Phi suitable for such a derivative? I've tried diff, but it did not keep the Phi as I would need it.

If the second derivative would be Phi_d2, I need to get it's value (for example in t = 0).

How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...