Method other than for loop?
8 ビュー (過去 30 日間)
古いコメントを表示
My code is quite long and outputs two values called beam1 and beam2. The values of these variables are dependant on another variable labeled length.
What I want my code to do is output a length value that allows beam1-beam2<0.0001. As of now I am using a for loop to cycle through a range of different length values to output the ones that fit my criteria. However this method takes an extremely long time to run.
Do you have any suggestions that I could use instead of a for loop that might help the program take less time to run?
2 件のコメント
Sean de Wolski
2011 年 6 月 17 日
Don't name your variable 'length' as that is a very useful MATLAB function that you don't want to have overwritten.
Sean de Wolski
2011 年 6 月 17 日
Other than that we're probably going to have to see the code (at least the relevant portions) to help.
採用された回答
Walter Roberson
2011 年 6 月 17 日
You could possibly use something like fmincon to minimize on (beam1-beam2).^2 with a tolerance of 0.0001^2 . Ummm -- can your code come out with beam2 > beam1 ?
Does your computation have a lot of local minima? If so then a more powerful minimizer might be needed.
9 件のコメント
Walter Roberson
2011 年 6 月 21 日
In Pb1(N)=5.6e-3; your N has been calculated as an expression involving non-integers. The calculated value is very close to 1880 (or whatever) but is not *exactly* an integer. You should avoid such calculations. Meanwhile, you can work-around using round()
その他の回答 (1 件)
Andrew Newell
2011 年 6 月 17 日
Your problem sounds like an optimization problem. Using the Optimization Toolbox to find the length that minimizes beam1-beam2 would probably be the best way to speed up your code.
3 件のコメント
Walter Roberson
2011 年 6 月 21 日
The Student Edition includes the Optimization Toolbox.
http://www.mathworks.com/academia/student_version/details.html
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!