Solving a simple equation

조회 수: 18 (최근 30일)
James
James 2011년 5월 25일
I have a function that can not be simplified further that goes something like this
0 = (1+exp(x))/(1+exp(-x))
(Simplified, my function is much longer and convoluted. The point is that I can't just write it as 'x = ...')
How can Matlab approximate this equation by choosing a good value for x? What is the command for this?

채택된 답변

John D'Errico
John D'Errico 2011년 5월 25일
If you are looking for a numerical solution, then this is a rootfinding problem. Use fzero, a tool designed to solve exactly that problem.
xfinal = fzero(@(x) (1+exp(x))./(1+exp(-x)),xstart);
You must supply a value for xstart. Better yet is if you can supply a pair of points that are known to bracket a solution.
Of course, this function has no solution, so it will always fail, but I assume that your true function does have one.
If you have truly tried to confuse things, and your real function is multi-dimensional, then you can use tools from the optimization toolbox. Here one would use fsolve.
If you are looking for a symbolic solution then solve (which requires the symbolic toolbox) is the answer.

추가 답변 (1개)

Ben Mitch
Ben Mitch 2011년 5월 25일
You can solve an equation algebraically using the symbolic math toolbox, e.g. to solve x+3=0 use:
solve('x+3')
You can solve it numerically using various techniques, including the optimization toolbox, e.g. to solve x+3=0 use:
fminsearch(@(x) (x+3)^2, 0)
However, the equation you've posted has no finite solutions, which is why both of these commands generate trash:
solve('(1+exp(x))/(1+exp(-x))')
fminsearch(@(x) ((1+exp(x))/(1+exp(-x)))^2, 0)
Note that the numerical approach (the second command) does find an answer, but it's only a value of x where the equation is approximately equal to zero, and there's an infinity of them so the particular answer returned is not meaningful.

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by