Search code examples
matlabsymbolic-math

Implicit derivative with Matlab symbols


Is it possible to have MATLAB do implicit differentiation with its symbols? I have the following code

syms x;
y = symfun(sym('y(x)'), sym('x'));
yPrime = symfun(sym('y+(2*x)-1'), [sym('x'), sym('y')]);
diff(yPrime, x, 1)

From this I get

ans(x, y) =
2

but what I want to get (in some form) is

ans(x, y) =
dy/dx + 2

Is that possible?


Solution

  • It looks you're trying to use y as both a symbolic variable and an abstract symbolic function (symfun), which isn't possible. A symbolic function can only take symbolic variables as arguments. I think that you can accomplish what you want with (old-style strings not needed)

    syms y(x)        % Implicitly defines x too
    yPrime = y+2*x-1 % Also a symfun because y is a symfun
    diff(yPrime,x)
    

    which returns this symfun

    ans(x) =
    
    diff(y(x), x) + 2