What wrong in line 12 ?? help me please (Index exceeds matrix dimensions.)

4 ビュー (過去 30 日間)
Bui Thien Phuc
Bui Thien Phuc 2019 年 4 月 9 日
編集済み: Torsten 2019 年 4 月 10 日
clear
close all;
N=10;
h = 0.5;
u1(1)= 2 ;
u2(1)= 3 ;
u3(1)=0 ;
t(1) = 0;
for n=1:N
u3(n+1)=u1(n)+h*u2(1);
u1(n+1)=u1(n)+1/2*(4*exp(0.8*t(n))-0.5*u1(n)+4*exp(0.8*t(n+1))-0.5*u3(n));
t(n+1)= t(n) + h ;
end
plot(t,u1);

回答 (2 件)

Alex Mcaulley
Alex Mcaulley 2019 年 4 月 9 日
In this line:
u1(n+1)=u1(n)+1/2*(4*exp(0.8*t(n))-0.5*u1(n)+4*exp(0.8*t(n+1))-0.5*u3(n));
the length of t is n, then t(n+1) gives an error
  6 件のコメント
Bui Thien Phuc
Bui Thien Phuc 2019 年 4 月 9 日
just answer me I want to use u2(1) only one time in first circle so I did right ?
Torsten
Torsten 2019 年 4 月 10 日
編集済み: Torsten 2019 年 4 月 10 日
If you use your code: no.
If you use Alex's second code: yes.

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


Torsten
Torsten 2019 年 4 月 9 日
clear
close all;
N = 10;
h = 0.5;
u1(1) = 2 ;
u2(1) = 3 ;
u3(1) = 0 ;
t = 0:h:N*h;
for n = 1:N
u3(n+1) = u1(n)+h*u2(1);
u1(n+1) = u1(n)+1/2*(4*exp(0.8*t(n))-0.5*u1(n)+4*exp(0.8*t(n+1))-0.5*u3(n));
end
plot(t,u1);

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by