In the program below, the loop stops at theta(4). It will be a great help if you can please help me to rectify the error.

clear
theta(1)= input('Enter your guess:')
theta(3)= theta(1)
S1=0.5
S2=.32
S3=0.56
dx=(1/30)
for i= 2:30
theta(i)=(theta(i+1)+theta(i-1)+S3*(dx)^2)/(S1*theta(i)*(dx)^2+S2*(dx)^2+2)
end
theta(i)
The error message is-----
??? Attempted to access theta(4); index out of bounds because numel(theta)=3.
Error in ==> discrtization at 9. theta(i)=(theta(i+1)+theta(i-1)+S3*
(dx)^2)/(S1*theta(i)*(dx)^2+S2*(dx)^2+2)

 採用された回答

Ben11
Ben11 2014 年 8 月 26 日
Initialize your array theta and change the upper limit of your for loop like so:
theta = zeros(1,30);
theta(1)= input('Enter your guess:')
theta(3)= theta(1)
S1=0.5
S2=.32
S3=0.56
dx=(1/30)
for i= 2:size(theta,2)-1
theta(i)=(theta(i+1)+theta(i-1)+S3*(dx)^2)/(S1*theta(i)*(dx)^2+S2*(dx)^2+2)
end
theta(i)

2 件のコメント

Tuhin
Tuhin 2014 年 8 月 26 日
Dear Ben, Thanks a ton for the reply. It is working now.
Ben11
Ben11 2014 年 8 月 26 日
great then glad to help!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 8 月 26 日

コメント済み:

2014 年 8 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by