How to solve an equation with an array of constant parameters?

7 views (last 30 days)
I am trying to solve equation xlogx=e where x is the variable and e is a real constant. e can take many (say 10000) values which are stored in an array e_arr. I want to return an array of numerical solutions with each element corresponding to an element of e_arr. Of course, I can construct a string 'x*log2(x)= - 0.1' for example for each of the element of e_arr, but is there any way to call solve (or vpasolve) function with vector input for the constant parameter e? Could you please help me with this, assuming you are given an array, say, e_arr=[-0.1, -0.54, -0.32, ... ..., -0.79, -0.6]? Thanks a lot. PS: the log function has base 2 and x takes values in [0.0, 1.0].

Accepted Answer

Ridwan Alam
Ridwan Alam on 6 Dec 2019
Edited: Ridwan Alam on 6 Dec 2019
syms x e
>> eqns = x*log2(x)==e
eqns =
(x*log(x))/log(2) == e
>> S= solve(eqns,x)
S =
(e*log(2))/lambertw(0, e*log(2))
>> e=[-0.1, -0.54, -0.32, -0.79, -0.6];
>> subs(S)
ans =
[ -log(2)/(10*lambertw(0, -log(2)/10)), -(27*log(2))/(50*lambertw(0, -(27*log(2))/50)), -(8*log(2))/(25*lambertw(0, -(8*log(2))/25)), -(79*log(2))/(100*lambertw(0, -(79*log(2))/100)), -(3*log(2))/(5*lambertw(0, -(3*log(2))/5))]
  4 Comments
Heng Zhou
Heng Zhou on 6 Dec 2019
Thanks. So the handling of array of constants is not in solve(), but in symbolic functions (subs). Anyway, it is a way out. Thank you for your prompt answer (PS, fsolve is also a good idea).
Ridwan Alam
Ridwan Alam on 6 Dec 2019
Sure. Glad to help. Please vote up if you liked the conversation.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!