0

I would like to find the derivative of this function:

function [f] = f4(x) 
    f=0.5*(x(1)^2+10*x(2)^2);
end

I'm using symbols to find the jacobian:

x = sym('x', [2 1]);
f=0.5*(x(1)^2+10*x(2)^2);
grad=jacobian(f,x)

which returns

grad = [ x1, 10*x2]

Then I change it manually to look like this so I can use feval:

grad = [ x(1,1), 10*x(2,1)];

I find that using feval is faster than using subs. I would like to find the derivative of any function that I can evaluate using feval and avoid doing anything manually.

horchler
  • 18,384
  • 4
  • 37
  • 73
V3XD
  • 21
  • 4
  • Do you want to take derivative without using symbols that usually we define in matlab by sym function? – Hassan Saqib Apr 24 '15 at 20:24
  • I don't mind getting the derivative using symbols as long as the final function for the derivative is not a symbolic function and I didn't have to edit it manually. – V3XD Apr 24 '15 at 20:26
  • So how exactly do you expect to get the function without symbols? In which form? `feval` is working for builtin functions or `.m`-file defined ones. There is no builtin functionality to write an m-file for you. But you can implement one using the symbolic toolbox. But it looks too awkward to me. – Eugene Sh. Apr 24 '15 at 20:51
  • possible duplicate of [Calculate the derivative of a vector](http://stackoverflow.com/questions/20613859/calculate-the-derivative-of-a-vector) – in particular the third option in may answer, Complex Step differentiation. See also this question: [numerical partial derivative in Matlab](http://stackoverflow.com/q/20257508/2278029). – horchler Apr 24 '15 at 22:39
  • possible duplicate of [How do I make a function from a symbolic expression in MATLAB?](http://stackoverflow.com/questions/1995486/how-do-i-make-a-function-from-a-symbolic-expression-in-matlab). – TroyHaskin Apr 24 '15 at 23:23

0 Answers0