フィルターのクリア

??? Attempted to access G(2); index out of bounds because numel(G)=1.

2 ビュー (過去 30 日間)
Bennie
Bennie 2011 年 12 月 8 日
Hello, I am trying to do the below calculation and I keep getting this error. I need it to compare the difference of subsuquent calculations and repeat the loop until their difference is less than 0.0001. clear all; R=2.4; P=.1; eta= 10e-19; k=1; G(k)= R* eta /.1 Q=.5 k=k+1 while(G(k) - G(k- 1) > .0001) G(k) = G(k+1)*{1-(G(k)+1)/(G(k+1)+G(k))}+ Q k=k+1;
end
??? Attempted to access G(2); index out of bounds because numel(G)=1.
Error in ==> homework4 at 9 while(G(k) - G(k- 1) > .0001)

採用された回答

Robert Cumming
Robert Cumming 2011 年 12 月 8 日
do you know how to bebug? highlight the line before the while loop in editor and hit F12 on keyboard. Run your code. Inspect your variables - see if you can understand the error message.
As this is homework I'm not going to tell you the answer - this is an attempt to guide you to find the answer yourself - you will learn more that way...

その他の回答 (2 件)

Sven Schoeberichts
Sven Schoeberichts 2011 年 12 月 8 日
You are trying to access G(k+1) before it exists
clear all;
R = 2.4;
P = 0.1;
eta = 10e-19;
k = 1;
G( k ) = R * eta / 0.1;
Q = 0.5;
k = k + 1;
while( G(k) - G(k-1) > 0.0001 )
G(k) = G(k+1)*{1-(G(k)+1) / (G(k+1)+G(k))}+ Q;
k = k + 1;
end
  1 件のコメント
Bennie
Bennie 2011 年 12 月 8 日
I kind of new that: I think the function as given in the problem is flawed. Thanks for your help.

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


Wayne King
Wayne King 2011 年 12 月 8 日
Hi, You should format your code so that people can read it.
Your problem is that you have G as a scalar:
R=2.4; P=.1; eta= 10e-19;
k=1; G(k)= R* eta /.1;
Then you try to access G(2) in your while statement
k = k+1;
while(G(k)-G(k-1)>0.0001)
but you have never provided a value for G(2). You increment k up to 2 with k = k+1, so your while statement attempts to evaluate G(2)

カテゴリ

Help Center および File ExchangeControl Flow についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by