Solving an exponential equation in MATLAB

Now I'm trying to solve the exponential equation in MATLAB as part of my job. It is easy to see that the equation

exp(-t)+t*exp(-t)-n=0 

will have two solutions: one less than zero and one less.

However, using only the solution function, MATLAB returns something called the lambertw function, and it can only eval () until the solution is below zero, which is not what I want for the answer. Can someone help me here?

Thanks for all the answers and comments!

ps As an alternative, Iโ€™m thinking about using the Newton-Raphson method to solve it, but I wonder how the speed compares with solution ()?

Ziyao Wei

+3
source share
3 answers

lambertw is the function you need. However, this is a multi-valued function and has several branches. You need to choose the right branch for the answer. See my answer to another question on how to choose a different branch for the solution.

+1
source

In the code below, I numerically solve the equation for n=0.5 (constant), but you should be similar for the other values โ€‹โ€‹you choose.

Note that the SOLVE function returned only the first solution found. Therefore, I directly call the MuPAD engine and each time I specify the solution search interval:

 %# lets plot the function: f(x) = exp(-x)+x*exp(-x) h(1) = ezplot('0.5', [-1.5 10]); hold on h(2) = ezplot('exp(-x)+x.*exp(-x)', [-1.5 10]); set(h(1), 'LineStyle',':', 'Color','r') legend(h, 'y = 0.5', 'y = exp(-x)+x.*exp(-x)') %# The numeric solver only returns the first solution that it finds x = solve('exp(-x)+x*exp(-x)=0.5') x = vpa(x) %# we can call the MuPAD solver and give the interval where solution can be found x1 = evalin(symengine, 'numeric::solve(exp(-x)+x*exp(-x)=0.5, x = -1..0)') x2 = evalin(symengine, 'numeric::solve(exp(-x)+x*exp(-x)=0.5, x = 0..3)') %# show the solutions on the plot plot([x1 x2], 0.5, 'ro') 

Solution returned by SOLVE:

 x = - 1.0*lambertw(0, -1/(2*exp(1))) - 1.0 x = -0.76803904701346556525568352607755 

MuPAD Digital Solutions:

 x1 = -0.76803904701346556525568352607755 x2 = 1.6783469900166606534128845120945 

enter image description here

+2
source

The answer provided by Matlab is correct, but it only gives you one branch. To get another branch, use the provided answer, but replace lambertw(x) with lambertw(k,x) for different values โ€‹โ€‹of k .

See doc lambertw more details.

You can look at the Lambert W function on mathworld to learn more and visualize different branches.

+1
source

All Articles