Solving Exponential equation in MATLAB

拈花ヽ惹草 提交于 2019-12-01 09:29:24
abcd

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!