How to evaluate symbolic derivative?

I have created a function, powerdiff, but when I try to evaluate it at a number I get the following error message. I am wanting to evaluate it at different points to try and find the root:
Subscript indices must either be real positive integers or logicals.
Error in sym/subsref (line 805)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in ivcurve_var_zoom (line 36)
powerdiff(1e-2)
Here is my code. I can evaluate the original function, power, just not powerdiff (the derivative).
syms J
Veff=@(J)0;
%Creating voltage equation
for i=1:numbg
Jph=flux(i,2);%Constant
Avalue=A(i);%Constant
V=@(J)k*t*log((Jph-J)/Avalue)+bg(i);%Voltage function
Veff=@(J)Veff(J)+V(J);%Adds to previous voltage function
end
power=@(J)Veff(J).*J;
power(1e-2)
powerdiff=diff(power(J));
powerdiff(1e-2)

 Accepted Answer

Stephan
Stephan on 10 Jul 2018
Edited: Stephan on 10 Jul 2018
Hi,
you are treating the derivate like the function handle - but it is not a function handle.
What you do is asking matlab for the value of the vector powerdiff at the index 1e-2...
What you probably want to do is get the result of that function for the input value 1e-2.
You could use matlabFunction to achieve what you want.
Best regards
Stephan

8 Comments

Thank you Stepahn, matlabFunction works perfectly! Now I can use fzero but it isn't working seeing as my function has an asymptote (numbers become imaginary). How can i determine where the asymptote of a function is so I can put in the appropriate limit in fzero?
My asymptote is a vertical asymptote... This would only work for a horizontal asymptote... Here is an image of my plot. I need to determine what x value the dashed line falls at, so I can use this as the limit for fzero.
Stephan
Stephan on 10 Jul 2018
Edited: Stephan on 10 Jul 2018
Is not there a section about finding vertical asymptots directly below the section about horizontal asymptots?
Yes but roots does not work well with symbolic expressions...
Stephan
Stephan on 10 Jul 2018
Edited: Stephan on 10 Jul 2018
Can you provide the symbolic function of powerdiff ?
@(J)log(J.*(-3.937007874015748e-4)+2.442872757695621e-6).*2.58519909e-2+log(J.*(-6.835083114610674e-3)+3.826599732420725e-4).*2.58519909e-2+J.*(1.017794917322835e-5./(J.*3.937007874015748e-4-2.442872757695621e-6)+1.767005064796588e-4./(J.*6.835083114610674e-3-3.826599732420725e-4))+3.1e1./1.0e1
It is a big function that will change, but keep the same general structure.
Are these values possible candidats:
x01 =
0.006204896804547
x02 =
0.055984684725208
???
What did i do?:
% set J to symbolic variable
syms J
% Your function
f = log(J.*(-3.937007874015748e-4)+2.442872757695621e-6).*2.58519909e-2+log(J.*(-6.835083114610674e-3)+3.826599732420725e-4).*2.58519909e-2+J.*(1.017794917322835e-5./(J.*3.937007874015748e-4-2.442872757695621e-6)+1.767005064796588e-4./(J.*6.835083114610674e-3-3.826599732420725e-4))+3.1e1./1.0e1;
% divide in numerator and denominator
[num, den] = numden(f);
% set denominator equal to zero
f_asymp_vert = den == 0;
% solve this equation
x0 = solve(f_asymp_vert);
% set format
format long;
% extract the (still) symbolic values of the solution and convert to double
x01 = double(x0(1,1)) % --> 1831362293716070975/295147905179352825856
x02 = double(x0(2,1)) % --> 252132605266902219/4503599627370496000
--> Is that what you need?
Best regards
Stephan

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Jul 2018

Edited:

on 11 Jul 2018

Community Treasure Hunt

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

Start Hunting!