What am I doing wrong? (Trying to construct a while loop)

7 ビュー (過去 30 日間)
Richard
Richard 2013 年 11 月 14 日
コメント済み: Walter Roberson 2013 年 11 月 14 日
V0=10
h_bar_SQ=0.076
E=0:0.1:10
a=0.3
Mass=1
alpha=sqrt(2.*Mass.*E/h_bar_SQ);
beta=sqrt(2.*Mass.*(V0-E)/h_bar_SQ);
F_even=beta.*cos(alpha.*a)-alpha.*sin(alpha.*a)
E=fzero(F_even)
while
E=fzero(F_even);
if
E=Nan;
break
else
a=a-0.010;
end
end
>> scriptmatlab Error: File: scriptmatlab.m Line: 10 Column: 7 Expression or statement is incomplete or incorrect.
>> scriptmatlab Error: File: scriptmatlab.m Line: 12 Column: 7 Expression or statement is incomplete or incorrect.
Line 10 is where is says while and line 12 is where it says if
  1 件のコメント
Richard
Richard 2013 年 11 月 14 日
I guess I need to add something beside while and if? What?

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

回答 (3 件)

Wayne King
Wayne King 2013 年 11 月 14 日
編集済み: Wayne King 2013 年 11 月 14 日
One thing is your missing an "end"
if true
% code
endV0=10
h_bar_SQ=0.076
E=0:0.1:10
a=0.3
Mass=1
alpha=sqrt(2.*Mass.*E/h_bar_SQ);
beta=sqrt(2.*Mass.*(V0-E)/h_bar_SQ);
F_even=beta.*cos(alpha.*a)-alpha.*sin(alpha.*a)
E=fzero(F_even)
while
E=fzero(F_even);
if
E=Nan;
break
else
a=a-0.010;
end
end
end
You were missing an "end" for your initial if statement. The other thing is what kind of while statement are you trying to construct here? fzero() returns a value. What condition are you trying to test?
How about something like
while ~isnan(fzero( ))
  1 件のコメント
Richard
Richard 2013 年 11 月 14 日
編集済み: Richard 2013 年 11 月 14 日
"Investigate the dependence of the lowest two eigenvalues of the square well upon V0 (with fixed a=0.3 /) and a(with fixed V0= 10), and plot your results. What is the smallest well width that will support two bound states?"
Thats the question to the assignment. Im trying to find the lowest value of a which has units of length for which two bound states(Energies) can be supported.
Energy would be the variable E.

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


Walter Roberson
Walter Roberson 2013 年 11 月 14 日
Your syntax
if
E=Nan;
break
else
a=a-0.010;
end
is not valid. "if" needs to be followed by a condition, and E=Nan; is not a condition.

Richard
Richard 2013 年 11 月 14 日
Can someone then rewrite it in the correct format?
  1 件のコメント
Walter Roberson
Walter Roberson 2013 年 11 月 14 日
Please use "Comment on this Answer" instead of creating new Answers.
while true
do a computation
if result of computation has some property
break
end
change something about one or more of the variables involved in the computation
end

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

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by