## How can I raise an anonymous expression to a power?

### Dustin (view profile)

on 9 Jun 2014
Latest activity Edited by Star Strider

### Star Strider (view profile)

on 16 Jun 2014
I am trying to replicate the quotient rule for finding derivatives using anonymous functions. I cannot figure out a way to raise one anonymous function to a power for this to work. Here is what I have
syms x
f1 = @(x) (x+1).*(x-3);
g1 = @(x) x.^3 + 2.*x +1;
h = @(x) f1(x)./g1(x);
df1dx = diff(@(x) f1(x), x);
dg1dx = diff(@(x) g1(x), x);
dhdx = diff(@(x) h(x), x);
simplify(dhdx)
% The below line does not work, and I cannt figure out why...
quotient = (f1.*dg1dx - df1dx.*g1) / (g1).^2;
simplify(quotient)
How can I use a power with this function?

Dustin

### Dustin (view profile)

on 16 Jun 2014
Thank you for the answers. I got both of them to work for my problem. I am posting here because my next question si very related to my last. I am wondering how to know where to put the @(x) in anaonymous functions that later will take real-valued input. For example, I am trying to plot a function, its linearization and its 2nd order Taylor Polynomial on the same graph. I believe I can do this with ezplot correctly (though I cannot distinguish between the linearization and polynomial at all, so I could very well be wrong), however, I cannot get this to work with using "plot." My basic questions are: how do I know where to put the @ symbol? How does this affect whether or not the function takes real-valued inputs?
%%Taylor Polynomials
syms x
f = @(x) cos(x);
% dydx = diff(@(x) f(x), x);
dydx = @(x) diff(f(x), x);
dy2dx = @(x) diff(dydx(x), x);
a = 0;
% L(x) = f(a) + f'(a)(x-a)
Lx = @(x) f(a) + dydx.*(x-a);
Px = @(x) f(a) + dydx.*(x-a) + dy2dx.*(x-a).^2;
figure(1);
q = ezplot(f(x)); set(q,'Color','b'); hold on;
w = ezplot(Lx(x)); set(w,'Color','r'); hold on;
p = ezplot(Lx(x)); set(p,'Color','g'); hold on;
legend('original','linearization','Polynomial');
axis([-.05, .05, 0.95, 1.05]);
hold off; figure(2);
xnum = -5:0.1:5;
plot(xnum,f(xnum)); hold on;
%Neither of the below will work...
plot(x,dydx(x)); hold on;
plot(xnum,dydx(xnum)); hold on;

### Rashmil Dahanayake (view profile)

on 9 Jun 2014
Edited by Rashmil Dahanayake

### Rashmil Dahanayake (view profile)

on 9 Jun 2014

Firstly correct the definition the inline functions for differentiation. Start the command with @ symbol. When calling inline functions you must pass an argument. eg F(x).
Updated code
syms x
f1 = @(x) (x+1).*(x-3);
g1 = @(x) x.^3 + 2.*x +1;
h = @(x) f1(x)./g1(x);
df1dx = @(x) diff(f1(x), x); % note the location of @ symbol
dg1dx =@(x) diff(g1(x), x);
dhdx = @(x) diff(h(x), x);
simplify(dhdx(x)) % pass an argument to the function
quotient = (f1(x).*dg1dx(x) - df1dx(x).*g1(x)) / (g1(x)).^2;
simplify(quotient)

### Star Strider (view profile)

on 9 Jun 2014
Edited by Star Strider

### Star Strider (view profile)

on 9 Jun 2014

The Symbolic Toolbox will work with anonymous functions, but it prefers not to, because anonymous functions prefer numeric inputs.
‘Undefined function 'power' for input arguments of type 'function_handle'.’
syms x
f1(x) = (x+1).*(x-3);
g1(x) = x.^3 + 2.*x +1;
h(x) = f1(x)./g1(x);
df1dx = diff(f1(x), x);
dg1dx = diff(g1(x), x);
dhdx = diff(h(x), x);
simplify(dhdx)
% The below line does not work, and I cannt figure out why...
% (It does now!)
quotient = (f1.*dg1dx - df1dx.*g1) / (g1).^2;
quotient = simplify(collect(quotient))
% Compare:
quotient2 = diff(f1/g1, x);
quotient2 = simplify(collect(quotient2))
If you want to use anonymous functions outside of the Symbolic Math Toolbox to calculate numeric derivatives, it‘s easy enough:
dfdx = @(f,x) (f(x+1E-8)-f(x)) ./ 1E-8; % Generic derivative function
f = @(x) x.^2; % Function to differentiate
x = 0:5; % Evaluate function & derivative
fx = f(x)
dfx = dfdx(f,x)
produces:
fx =
0.0000e+000 1.0000e+000 4.0000e+000 9.0000e+000 16.0000e+000 25.0000e+000
dfx =
10.0000e-009 2.0000e+000 4.0000e+000 6.0000e+000 8.0000e+000 10.0000e+000