Matlab: Finding a quicker method than simple iteration

In summary: Derive analytic expressions for what lambda approaches at very large T and at very small T.In summary, the author is trying to speed up a function in MATLAB that solves the equation by simple iteration. He is using simple iteration but the while loop terminates when the values of lambda converge. He is wondering if there is a faster or more computationally efficient method. The author has discovered that the fzero function which gets the roots is faster but it is round off errors or diverging.
  • #1
rewmck
3
0

Homework Statement



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

λ= [itex]\frac{gT^{2}\frac{\text{tanh}(2\pi h)}{λ}}{2\pi}[/itex]

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
  • #2
Matlab probably has built-in solvers that may behave better. Investigate "roots of scalar functions", for example.
 
  • #3
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!
 
  • #4
rewmck - what are you trying to solve for? Also, is that formulae stated correctly? I read your equation as follows:

[tex]
\lambda = \frac{g \cdot T^2}{2\, \pi} \cdot \frac{\tanh(2\, \pi \, h)}{\lambda}
[/tex]
 
  • #5
Hi Theo, solving for lambda and you're right the equation is wrong, it should be:
[tex]
\lambda = \frac{g \cdot T^2}{2\, \pi} \cdot \tanh\left(\frac{2\, \pi \, h}{\lambda}\right)
[/tex]
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:
...
 
  • #6
Have you plotted the function f(λ) = 0 to investigate why it might be 'tricky' in some regions of its domain?
 
  • #7
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.
 

1. What is simple iteration in Matlab?

Simple iteration in Matlab is a method used to solve equations by repeatedly updating a variable or set of variables until a desired level of accuracy is reached. This process involves setting an initial guess for the solution and using a loop to update the guess based on the equation until the solution converges.

2. Why is it important to find a quicker method than simple iteration in Matlab?

Simple iteration can be time-consuming and may not always converge to an accurate solution. Therefore, finding a quicker method can save time and improve the accuracy of the results.

3. What are some alternative methods to simple iteration in Matlab?

Some alternative methods to simple iteration in Matlab include Newton's method, the Secant method, and the Bisection method. These methods use different techniques to update the solution guess and can often converge to a solution faster than simple iteration.

4. How can I determine which method is the quickest for my specific problem in Matlab?

The best method for a specific problem in Matlab can vary depending on the equation and initial guess. It is recommended to try out different methods and compare the time and accuracy of the results to determine the most efficient method for your problem.

5. Are there any built-in functions in Matlab that can help with finding a quicker method than simple iteration?

Yes, Matlab has built-in functions such as "fsolve" and "fzero" that can solve equations using methods such as Newton's method and the Secant method. These functions can be more efficient than simple iteration and can also handle more complex equations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
952
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
815
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top