symbolic differentiation with multiple variables/parameters

2 次查看(过去 30 天)
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!

回答(1 个)

Iman Ansari
Iman Ansari 2013-5-8
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))

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by