MatlabFunction removes input arguments

I want to calculate the differentiation of the function of two variables. For instance:

ax^2 + by^2 + cxy

So, I am doing this:

a = 1
b = 1
c = 1

syms x y f
f = a*x^2 + b*y^2 + c*x*y
df = matlabFunction(diff(f,'x'))

which returns:

df = 
    @(x,y)x.*2.0+y

And this is normal. But if c is zero, it returns this:

df = 
    @(x)x.*2.0

and I can't call it with two arguments anymore, but I need to pass two arguments, even if y is no longer in the definition, since c is not always zero. How can i fix this?

+4
source share
1 answer

The argument 'vars'for matlabFunctionallows you to specify the input variables of the generated function:

>> df = matlabFunction(diff(f,'x'),'vars',[x y])

df = 

    @(x,y)x.*2.0
+4
source

Source: https://habr.com/ru/post/1542212/


All Articles