Numerical solution

2 ビュー (過去 30 日間)
Ali Ihsan
Ali Ihsan 2012 年 3 月 6 日
Hello
I´m working on a numerical solution for an equation. Therefore I wrote the following function:
function test(dBL,i)
for n=1:i
BL=0;
while (1+cos(BL)*cosh(BL))*(1+cos(BL+dBL)*cosh(BL+dBL))>0
BL=BL+dBL;
fc=1+cos(BL)*cosh(BL);
end
vfc(n,1)=fc;
vBL(n,1)=BL;
end
vfc
vBL
end
My aim is that the matlab function produce as many solutions as n and the important output is vBL, which is a vector with the solutions.
I want the while loop to start with BL=0 for n=1, but for n=2 I want the loop to start with the last BL from the previous while loop and so on. Anybody can help??
Thanks in advance

採用された回答

Dr. Seis
Dr. Seis 2012 年 3 月 6 日
Unless I misunderstood something, I think all you need to do is change:
BL = 0;
to
if n == 1
BL = 0;
end
Then for n > 1, "BL" will be whatever value it was at the end of the previous iteration.
In your case (now that I am looking at it more closely), if "BL" starts off as the same value as the previous iteration, then the "while" loop probably will not execute... since "BL" and "dBL" are not changing, (1+cos(BL)*cosh(BL))*(1+cos(BL+dBL)*cosh(BL+dBL))>0 will also not change. Should that expression be some sort of function of "n" as well?
  1 件のコメント
Ali Ihsan
Ali Ihsan 2012 年 3 月 6 日
Hello Elige
I found out a solution. I did exactly what you mentioned, what are the odds :)
The function is now:
function test(dBL,i)
for n=1:i
if n==1
BL=0;
elseif n>1
BL=result+dBL;
end
while (1+cos(BL)*cosh(BL))*(1+cos(BL+dBL)*cosh(BL+dBL))>0
BL=BL+dBL;
fc=1+cos(BL)*cosh(BL);
end
result=BL;
vBL(n,1)=BL;
end
vBL
end
By adding dBL to the result, the loop starts in the other side of the BL axes and it works now.
Thank you very much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by