0
votes

I have a function:

enter image description here

Where ||x|| is an Euclidean distance.

For given n (number of variables) I want Matlab to differentiate this function and then substitute real numbers into it.

What I truly don't understand how to do is:

1) How let Matlab create all these new n variables for later differentiation?

2) Each variable is a vector of dimension = d, i.e x=[x1, x2, ... xd], so later I want to differentiate function with respect to certain vector elements, for example with respect to x1, how can I do it?

EDIT 1: function should be differentiated at each x_i, where i=1:n

1
Hi ike, I'm having trouble understanding your problem. Can you be a bit more precise with your notation? In particular, in the summation equation, is x_i the i-th component of the vector x, or is it the i-th vector in a sequence of vectors x_1, ..., x_n? Also be more explicit with the function you want to differentiate: is its domain n-dimensional real numbers? When you say you want to differentiate, do you want a numerical scheme, or do you want an analytical solution? If you want an analytical solution, can you guarantee that your function is differentiable everywhere? - Michael
@Michael 1) x_i is i-th vector in a sequence. 2) yes, the domain is d-dimensional space of real numbers (as I said that each vector is d-dimensional). 3) in the end I need only the numerical value, but to come to that point, as I understand, I firsttly have to solve it analytically. Yes, it is differentiable everywhere, all my vectors are unit vectors. - ike
1) If your function is the entire sum, i.e. f = sum_{i=1}^n ||x_i + x_{i+1}||^(-1), then its domain is certainly not R^d, but rather (R^d)^n, i.e. a sequence of n vectors in R^d. If you're not explicit with notation, it's hard to understand which derivative you want to compute. The grad operator in R^d or something else? 2) Do you plan on solving the derivative yourself by hand, or leaving it to MATLAB? If the latter, I'm curious how you know this function is differentiable everywhere, since f(x) = |x| is not differentiable at x=0 in 1D space. - Michael

1 Answers

1
votes

The derivative of a sum, is the sum of the derivatives of each element... so, we need to find the derivative only once (you can do this manually, if it is a simple function like in your toy-example, but we do this the general way, using the Symbolic Math Toolbox):

syms x y z % declaring 3 symolic variables
F = 1/(norm([x,y,z])); % declaring a function
f = diff(F,x) % calculate the derivative with regard to the symbolic variable x

f = -(abs(x)*sign(x))/(abs(x)^2 + abs(y)^2 + abs(z)^2)^(3/2)

you now have different options. You can use subs to evaluate the function f (simply assign numeric values to x,y, and z and call subs(f). Or, you create a (numeric) function handle by using matlabFunction (this is the way, I prefer)

fnc = matlabFunction(f); % convert to matlab function

Then, you just need to sum up the vector that you created (well, you need to sum up the sum of each of the two vector elements...)

% create arbitrary vector
n = 10;
x = rand(n+1,3);

% initialize total sum
SumFnc = 0;
% loop through elements
for i = 1:n
    % calculate local sum
    s = x(i,:)+x(i+1,:);
    % call derivative-function + sum up
    SumFnc = SumFnc + fnc(s(1),s(2),s(3));
end