symbolic differentiation with multiple variables/parameters

12 views (last 30 days)
I use the follow code to create a sigmoid function of x with a parameter a, and its derivative with respect to x:
>> syms a x
>> g = 1/(1+exp(-a*x))
>> dg = diff(g,x)
I then plot them with a=1:
>> g=matlabFunction(g);
>> dg=matlabFunction(dg);
>> x=-10:0.01:10;
>> plot(x,g(x,1),x,dg(x,1))
>> subplot(1,2,1)
>> plot(x,g(1,x),x,g(x,1))
>> subplot(1,2,2)
>> plot(x,dg(1,x),x,dg(x,1))
While the first plot gives two identical curves, the second one gives two different ones: dg(1,x) is right but dg(x,1) is wrong.
From these results it seems the function dg takes the first argument as a and second x, instead of the other way around. But why is it this the case? What is the general rule in terms of the order of the parameters/variables in such a function?
Thanks for the help!

Answers (1)

Iman Ansari
Iman Ansari on 8 May 2013
See:
doc matlabFunction
Help: When converting symbolic expressions, the order is alphabetical.When converting symbolic functions, the input arguments appear infront of other variables. Other variables are sorted alphabetically.
You can change it:
syms a x
g = 1/(1+exp(-a*x))
dg = diff(g,x)
g=matlabFunction(g,'vars',[x a])
dg=matlabFunction(dg,'vars',[x a])
x=-10:0.01:10;
figure;
plot(x,g(x,1),x,dg(x,1))
figure;
subplot(1,2,1)
plot(x,g(1,x),x,g(x,1))
subplot(1,2,2)
plot(x,dg(1,x),x,dg(x,1))

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!