Solve a equation where I cant isolate the variable.

21 views (last 30 days)
I want to solve this equation : (w^2)-(g*x*tanh(x*p)=0
where g=9.8 and p=80; and w is a matrix with 345 345 rows and 1 column.
My goal is to know the x's values.
I've used the solve function for one row, and it worked well, but using it for the entire w values, is taking forever! I've waited for 3 hours and it wasnt finished yet.
There must be a better function to solve this case, please help me.
this was what I've tried:
syms x
eq=solve((a(i,j+3)^2)-(g*x*tanh(x*p));
a(i,j+3) corresponds to w.

Accepted Answer

John D'Errico
John D'Errico on 25 Mar 2014
It strongly depends on the accuracy you need.
If I needed only a 2 or 3 correct digits, I would solve the problem at a limited set of points (maybe a hundred.) Then use a spline interpolation to get the solution at any point.
If I needed more accuracy, I would use fzero, not solve. fzero will surely be faster. As well, I would use the spline approximation to give me good starting values for fzero.
Only if I needed serious accuracy would I go to solve.
  4 Comments
Walter Roberson
Walter Roberson on 25 Mar 2014
Over longer intervals the plot is more like quadratic (but not quadratic itself.) For example, output x = 1000 for w = 100, x = 6000 for w = 256
John D'Errico
John D'Errico on 27 Mar 2014
Edited: John D'Errico on 27 Mar 2014
Walter is doing his very best to confuse things, although not I think not intentionally.
That expression is EXACTLY quadratic in w, but NOT so in x. As a function of w, the root is indeed approximately quadratic. So Walter's comment is not very clear. He refers to the plot, but maybe not seeing what the plot showed!
In x for a FIXED value of w the function is essentially linear. The tanh term goes rapidly to 1 for large x. Large x here is roughly any value of the tanh argument larger than about 3.
tanh(3)
ans =
0.99505475368673
Since x is multiplied by p, which was 80, then x*p will be effectively huge. For example, when x is only 0.1, we have 0.1*80=8.
tanh(.1*80)
ans =
0.999999774929676
My point is that this function is EXACTLY quadratic in w, but VERY, VERY rapidly asymptotically linear in x, since for any value of x just a bit larger than zero, the expression reduces to
w^2 - g*x*1 = 0
Thus for almost any value of w, the solution is quite accurately
x = w^2/g
Thus we see that we can write the root as a function of w, which is indeed approximately quadratic in w. (Only approximately so, since this is only an approximate solution.) I think this is what Walter was thinking of in his comment, despite his reference to the plot.
For example...
g = 9.8;
p = 80;
w = 1;
xhat = w^2/g
xhat =
0.102040816326531
How well does this do?
w^2 - g*xhat*tanh(xhat*p)
ans =
1.62370753842289e-07
So to within a small tolerance, the linear approximation in x yields a very good solution. In fact, for w only as large as 2, the linear approximation yields what is essentially an exact solution in double precision.
w = 2;
xhat = w^2/g
xhat =
0.408163265306122
w^2 - g*xhat*tanh(xhat*p)
ans =
0
So in double precision, the answer is perfect. We need to move to higher precision just to see any error.
xhat = sym(xhat)
xhat =
20/49
vpa(w^2 - g*xhat*tanh(xhat*p))
ans =
0.00000000000000000000000000034753726008443696345277977524663

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 25 Mar 2014
x = (5/49)*w^2 + (10/49)*w^2 / (exp(2*RootOf(-400*exp(2*Z)*w^2 + 49*exp(2*Z)*Z - 400*w^2 - 49*Z,Z))-1)
where Rootof(f(Z),Z) stands for the set of Z such that f(Z) is 0 -- the roots of the equation.
When you use solve() all numeric values are by default converted into rational values. You might not want that: you might want to force them to be floating point so that the numeric root finding routines are used right off. You might want to convert it all to a loop of fzero(), as that would probably be faster.
You might also want to unique abs(w) and work with that sorted order, using the solution of the previous w as the initial search value for the next value.

Community Treasure Hunt

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

Start Hunting!