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
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
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
Walter Roberson 2011 年 6 月 17 日

0 投票

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 件のコメント

Derek
Derek 2011 年 6 月 21 日
I was told to treat the majority of the code as a black box. the functions that determine beam1 and beam2 are,
Psb(N-x)=Psb(N-x+1)+gg1;
Psf(N-x)=Psf(N-x+1)+g1;
Psf(1) is beam1 in my output, and Psb(1) is beam2.
How would I go about using those functions with fmincon? Everything I have found online describing fmincon is very confusing. Thanks again!
Andrew Newell
Andrew Newell 2011 年 6 月 21 日
By itself your code doesn't determine beam1 or beam2. It is a recursive definition for Psb and Psf. Normally such definitions start with f(i) = f0 for i=1 and then work upwards to (say) i=N. You seem to be working downwards from some value. What is that value?
Walter Roberson
Walter Roberson 2011 年 6 月 21 日
If those are definitions for Psb and Psf, then Psb and Psf are linear. You can for example rewrite as
Psb(N-x+1) = Psb(N-x)-gg1
which makes it clear that Psb decreases by gg1 for each increase of 1 in its argument, and so Psb(s) = c(N) - gg1 * x for some constant c dependent upon N.
Derek
Derek 2011 年 6 月 21 日
I am working downwards from N with Psb(N)=Psf(N)=100e-6
Andrew Newell
Andrew Newell 2011 年 6 月 21 日
So what are you trying to vary to optimize |beam1-beam2|?
Walter Roberson
Walter Roberson 2011 年 6 月 21 日
Are gg1 and g1 known constants? Are they variables to be determined but which are not not dependent on N or x ? Or are they a series of values indexed by (N-x) ? Or ... ?
Andrew Newell
Andrew Newell 2011 年 6 月 21 日
To sum up the questions, you need a function that inputs x, or whatever your variable is, and outputs |beam1-beam2|.
Derek
Derek 2011 年 6 月 21 日
Yes that is correct. I need to find a "length" value which causes |Psf(1)-Psb(1)|<0.0001.
Currently I am looping through different length values,
for length=1.7:0.01:1.9
and have an if statement telling the program to output the proper length that satisfys the condition as well as outputting Psb(1) and Psf(1).
My loop is outputting length=1.86 (which i already know is correct), I just need a faster way to do this.
Also, I keep getting the error,
"??? Attempted to access Pb1(1880); index must be a positive integer or logical.
Error in ==> cutandpaste2 at 143
Pb1(N)=5.6e-3;"
when I run the loop. It is not always at Pb1(1880) and changes depending on where I begin the loop. I have tried doing a long format (no help). I still get the optimal length with this error but no output for Psb and Psf.
finally for Walter,
g1=nSf*h*(Sesf*n2(N-x+1)-Sasf*n1(N-x+1)-alphaSf/(nSf*Nt))*Psf(N-x+1)*Nt;
gg1=nSb*h*(Sesb*n2(N-x+1)-Sasb*n1(N-x+1)-alphaSb/(nSb*Nt))*Psb(N-x+1)*Nt;
Walter Roberson
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
Andrew Newell 2011 年 6 月 17 日

0 投票

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 件のコメント

Derek
Derek 2011 年 6 月 21 日
Sorry for the delay, it was the weekend and I just got back to the problem today. I am unable to use the optimization toolbox because I am student. I am looking into other ways of aquiring it.
Walter Roberson
Walter Roberson 2011 年 6 月 21 日
The Student Edition includes the Optimization Toolbox.
http://www.mathworks.com/academia/student_version/details.html
Derek
Derek 2011 年 6 月 21 日
whoops didn't notice that

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by