Solve a equation where I cant isolate the variable.

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.

 Akzeptierte Antwort

John D'Errico
John D'Errico am 25 Mär. 2014

1 Stimme

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 Kommentare

Thank you,
I didnt tried fzero because I need a initial value, x0, and I dont know mine, not even a interval. I have no idea...
Sorry, I'm just initiating at matlab..
John D'Errico
John D'Errico am 25 Mär. 2014
Bearbeitet: John D'Errico am 25 Mär. 2014
So then my answer is perfectly logical. The solution might be even easier though.
1. Solve the problem at a set of 10 or 100 points. However many it takes to give a good starting value.
2. Fit a spline through those points. I would suggest pchip.
3. Use the spline to predict a good starting value. This will give quick convergence.
4. Start fzero at the point given by the spline. BUT, can we do better? TRY SOMETHING! Get your hands dirty.
g = 9.8;
p = 80;
fw = @(x,w) w.^2 - g*x.*tanh(x*p);
Note that this function is perfectly symmetric around zero. Plot it. ALWAYS plot your work if you can!
ezplot(@(x) fw(x,2),[-2,2])
To be honest, this plot alone should make it perfectly clear!
A. The function is perfectly symmetrical around zero. (This is easy to prove.)
B. It is almost perfectly linear. So the starting value is trivially obtained from a linear approximation. Think about it!
Come on. Make an effort. I've given you a huge start at this point.
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 am 27 Mär. 2014
Bearbeitet: John D'Errico am 27 Mär. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 25 Mär. 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!

Translated by