Dear Matlab Gurus
I want to calculate the symbolic partial derivative of a function w.r.t to 'a' and 'b'. The function is
f=n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2)
Further more I also want to simplify and calculate the roots of both variable 'a' and 'b'. Actually I want to find the optimal point expression for both 'a' and 'b'. My code is
syms a
syms b
syms n
syms p
syms h
syms g
syms d
syms e
syms r
syms s
f=inline('(n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2)','a','b');
diff(diff(f(a,b),a),b)
however, the above code generates the following errors
Error using inlineeval (line 15)
Error in inline expression ==> (n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2)
Undefined function or variable 'n'.
Error in inline/subsref (line 24)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in test (line 12)
diff(diff(f(a,b),a),b)
Any kind of help is highly appreciated.
P.S: I have the symbolic math toolbox installed.
Regards

 Akzeptierte Antwort

@Johannes
@Johannes am 28 Okt. 2015

0 Stimmen

Hi Ali,
instead of the inline function you should use Anonymus Functions because inline will be removed in future releases.
syms a b n p h g d e r s
f=@(n,a,b,p,h,g,d,r,e,s) (n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2);
diff(diff(f,a),b)
Best regards,
Johannes

6 Kommentare

Hello Johannes Thank you very much for your kind reply. I made the changes, now when i execute your code it genrates this error.
Error using subsindex
Function 'subsindex' is not defined for values of class 'function_handle'.
sorry for bothering you again. I would be very thankful to you for your kind reply.
@Johannes
@Johannes am 28 Okt. 2015
Which release of Matlab do you use? Are there any missing arithmetic operators in your code?
I am using 64bit version of R2011b(7.13.0.564). There is no missing operator in code, in fact this is the complete code.
syms a b n p h g d e r s
f=@(n,a,b,p,h,g,d,r,e,s) (n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2);
diff(diff(f,a),b)
@Johannes
@Johannes am 28 Okt. 2015
Unfortunately I could not reproduce your error in R2011b. It could be one possibility to contact the technical support of the MathWorks.
Best regards, Johannes
Thank you Johannes I deleted and reinstalled my Matlab and now the above code is working fine.
>> syms a b n p h g d e r s
>> f=@(n,a,b,p,h,g,d,r,e,s) (n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2);
>> derivative=diff(diff(f,a),b)
derivative =
(a*b*g^2*h^2*n*p*(b*d*n*g^2*r^2 + d*e*(b - 1)*s^2))/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2)^2 - (b*g^2*h^2*n*p)/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2) - (g^2*h^2*n*p*(b - 1))/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2) + (b*g^2*h^2*n*p*(a*d*n*g^2*r^2 + d*e*(a - 1)*s^2)*(b - 1))/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2)^2 + (a*g^2*h^2*n*p*(b*d*n*g^2*r^2 + d*e*(b - 1)*s^2)*(b - 1))/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2)^2 + (a*b*g^2*h^2*n*p*(d*n*g^2*r^2 + d*e*s^2)*(b - 1))/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2)^2 - (2*a*b*g^2*h^2*n*p*(a*d*n*g^2*r^2 + d*e*(a - 1)*s^2)*(b*d*n*g^2*r^2 + d*e*(b - 1)*s^2)*(b - 1))/(a*b*d*n*g^2*r^2 + d*e*(a - 1)*(b - 1)*s^2)^3
since the output is very long and it will take my lots of time to solve it manually, can you help me on how to find the roots of 'a' and 'b' using matlab?. I tried this but it didn't work.
>> [sola, solb] = solve([derivative == 0, derivative == 0], [a, b])
Once again thank you so much.
Regards
Ali
Once again code:
syms a b n p h g d e r s
f=@(n,a,b,p,h,g,d,r,e,s) (n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2);
derivative_a=diff(f,a);
derivative_b=diff(f,b);
[sol_a,sol_b]=solve([derivative_a==0,derivative_b==0],[a,b])
You can type for example " doc solve " in your Matlab command window and the documentation of solve will appear.
Regards, Johannes

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Torsten
Torsten am 28 Okt. 2015

0 Stimmen

Why do you calculate diff(diff(f,a),b) ? Just for testing ?
syms a b n p h g d e r s
[sol_a,sol_b]=solve([diff(n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2,a)==0,diff(n*a*b*(1-b)*p*h^2*g^2)/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2,b)==0],[a,b])
Best wishes
Torsten.

4 Kommentare

Thank you Torsten for you time. I executed your code and it generates this error
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
seems like something is missing in the syntax. Actually this function is an SNR equation and 'a' and 'b' are two power variables that I want to optimize. The rest of the factors are constant.
Try this:
syms a b n p h g d e r s
[sol_a,sol_b]=solve([diff(n*a*b*(1-b)*p*h^2*g^2/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2),a)==0,diff(n*a*b*(1-b)*p*h^2*g^2/(n*a*b*g^2*d*r^2+(1-a)*(1-b)*d*e*s^2),b)==0],[a,b])
Best wishes
Torsten.
Didn't work, generates these errors.
Error using char
Conversion to char from logical is not possible.
Error in solve>getEqns (line 245)
vc = char(v);
Error in solve (line 141)
[eqns,vars,options] = getEqns(varargin{:});
Something wrong with my Matlab? Can you execute and post your results?
Regards
Ali
Torsten
Torsten am 28 Okt. 2015
Consult your documentation on how to use the solve-command in release 2011b.
Best wishes
Torsten.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 28 Okt. 2015

Kommentiert:

am 28 Okt. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by