Matlab: Finding a quicker method than simple iteration

  • Thread starter Thread starter rewmck
  • Start date Start date
  • Tags Tags
    Matlab Method
Click For Summary

Discussion Overview

The discussion centers on optimizing a MATLAB function that solves for wavelength (λ) using an iterative method. Participants explore various numerical methods, including simple iteration and the Newton-Raphson method, to improve computational efficiency. The conversation includes considerations of built-in MATLAB functions and the accuracy of results across different parameter ranges.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes using simple iteration to solve for λ, noting it is a slow process requiring thousands of iterations.
  • Another participant suggests exploring MATLAB's built-in solvers, specifically mentioning "roots of scalar functions."
  • A participant reports that using the fzero function significantly speeds up calculations for most cases but raises concerns about its accuracy for certain values of T, indicating discrepancies in results.
  • There is a correction regarding the formulation of the equation for λ, with a participant acknowledging an error in their initial equation and providing the corrected version.
  • Concerns are raised about the potential for round-off errors or divergence when using fzero, particularly for larger values of T, with one participant noting a significant discrepancy in results.
  • Some participants suggest plotting the function to better understand its behavior and to derive analytic expressions for λ at extreme values of T.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of various methods, with some finding fzero beneficial while others question its accuracy. The discussion remains unresolved regarding the best approach to take and the reasons for discrepancies in results.

Contextual Notes

Limitations include potential round-off errors, divergence of methods under certain conditions, and the need for further investigation into the behavior of the function across its domain.

rewmck
Messages
3
Reaction score
0

Homework Statement



I am trying to speed up a function in MATLAB that solves the equation by simple iteration:

λ= \frac{gT^{2}\frac{\text{tanh}(2\pi h)}{λ}}{2\pi}

Where λ is wavelength, h is wave height, T is period and g=9.81.

Also here where it's a bit easier to read.

Homework Equations


The Attempt at a Solution



Currently I'm using simple iteration so I'm guessing lambda then putting the equation in a while loop where the new value of lambda is given by putting the old value in the above equation. The while terminates when the values of lambda converge.

This is one of the slowest parts of my code and thousands of iterations are done when I run my code.

Does anyone know of a method which converges faster or would be computationally faster? I tried the Newton Raphson method and rearranged the equation by making one side zero to get f(x) and differentiated for f(x). This converges quicker but often gives the wrong answer.

Thanks
 
Last edited:
Physics news on Phys.org
Matlab probably has built-in solvers that may behave better. Investigate "roots of scalar functions", for example.
 
Thanks for the push in the right direction. Matlab has a built in function fzero which gets the roots. Iteration is quicker for 99% of my calculations but the final 1% is much faster with fzero so I have found the point where it becomes beneficial to use fzero and put an if in my function. So now the function is about 50 times faster. Thanks!
 
rewmck - what are you trying to solve for? Also, is that formulae stated correctly? I read your equation as follows:

<br /> \lambda = \frac{g \cdot T^2}{2\, \pi} \cdot \frac{\tanh(2\, \pi \, h)}{\lambda}<br />
 
Hi Theo, solving for lambda and you're right the equation is wrong, it should be:
<br /> \lambda = \frac{g \cdot T^2}{2\, \pi} \cdot \tanh\left(\frac{2\, \pi \, h}{\lambda}\right)<br />
Sorry about that.

So I solve for lambda over a range of T values. I discovered that the fzero function which works much quicker than simple iteration for large T values is actually giving completely the wrong answer but at smaller T values it gives the same as simple iteration. I'm not sure what is happening as I haven't had the chance to look closely at the fzero code but I guess it is round off errors or diverging. I think it is probably diverging as it is out by a factor of 10^16 for the value I checked.


TheoMcCloskey said:
rewmck - what are you trying to solve for? Also, is that formulae stated correctly? I read your equation as follows:
...
 
Have you plotted the function f(λ) = 0 to investigate why it might be 'tricky' in some regions of its domain?
 
gneill said:
Have you plotted the function f(λ) = 0 to investigate why it might be 'tricky' in some regions of its domain?
Derive analytic expressions for what lambda approaches at very large T and at very small T.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
10
Views
2K