问题
I am now trying to solve an exponential equation in MATLAB as a part of my assignment. It is easy to see that the equation
exp(-t)+t*exp(-t)-n=0
would have two solutions, one greater than zero and one smaller.
However, using just the solve function, MATLAB returns something called lambertw function and it can only eval() to the solution below zero, which happens not to be the one I want for the answer. Could anyone help me here?
Thanks in advance for all the answers and comments!
p.s. As an alternative, I am thinking about using Newton-Raphson method to solve it, but I wonder how is the speed comparing to solve()?
Ziyao Wei
回答1:
lambertw
is the function that you need. However, it is a multivalued function and has several branches. You need to choose the right branch for your answer. See my answer to another question on how to choose a different branch for the solution.
回答2:
In the code below, I am numerically solving the equation for n=0.5
(the constant), but you it should be similar for other values you choose.
Note how the SOLVE function only returned the first solution found. Therefore I am calling the MuPAD engine directly, and specifying each time an interval in which to search for the solution:
%# 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')
The solution returned by SOLVE:
x =
- 1.0*lambertw(0, -1/(2*exp(1))) - 1.0
x =
-0.76803904701346556525568352607755
MuPAD numeric solutions:
x1 =
-0.76803904701346556525568352607755
x2 =
1.6783469900166606534128845120945
回答3:
The answer provided by matlab is correct but it gives you only one branch.
To get another branch, use the answer provided but replace lambertw(x)
by lambertw(k,x)
for different values of k
.
See the doc of lambertw for more details.
You can have a look at the Lambert W function on mathworld to learn more and visualize the different branches.
来源:https://stackoverflow.com/questions/5838796/solving-exponential-equation-in-matlab