How can I raise an anonymous expression to a power?

조회 수: 16 (최근 30일)
Dustin
Dustin 2014년 6월 9일
편집: Star Strider 2014년 6월 16일
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?
  댓글 수: 1
Dustin
Dustin 2014년 6월 16일
편집: Star Strider 2014년 6월 16일
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;

댓글을 달려면 로그인하십시오.

답변 (2개)

Rashmil Dahanayake
Rashmil Dahanayake 2014년 6월 9일
편집: Rashmil Dahanayake 2014년 6월 9일
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
Star Strider 2014년 6월 9일
편집: Star Strider 2014년 6월 9일
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'.’
Try this instead:
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))
Not surprisingly, the same answer.
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
With your functions:
f1 = @(x) (x+1).*(x-3);
g1 = @(x) x.^3 + 2.*x +1;
quotnt = f1(x)./g1(x)
dquotnt1 = (f1(x).*dfdx(g1,x) - dfdx(f1,x).*g1(x)) ./ (g1(x)).^2
dquotnt2 = dfdx(@(x) f1(x)./g1(x),x)
The result produced by dquotnt1 is negative. The dquotnt2 expression gives the correct result.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by