Error in Euler's method code.
2 ビュー (過去 30 日間)
古いコメントを表示
Why am I getting Error:Invalid array indexing?I need help in order to code it correctly , I am a newbie.
% Euler
% y'(t)=cos(t+y) με y(0)=0
clc
clear all
close all
h=0.01;
N=100; % number of steps
y(0)=0;
for n=1:N
y(n+1)=y(n)+h*((cos(t+y)(n)));
t(n+1)=t(h+1);
end
hold on
t=0:0.01:3;
y=(-t)+2*atan(t); % exact solution
plot(t,y)
0 件のコメント
回答 (2 件)
Claire Fan
2021 年 6 月 18 日
matlab is an one-based array indexing programming language, hence array indices must be positive integers.
0 件のコメント
VBBV
2022 年 11 月 28 日
You can use the below approach for implementing your problem
clc
clear all
close all
h=0.01;
N=120; % number of steps
y(1)=0;
t(1) = 0;
for n=1:N
y(n+1)=y(n)+cosd(t(n)+y(n)); %change (use degree mode)
t(n+1)=t(n)+h; % change
end
hold on
T=0:0.01:1.2;
Y=(-T)+2*atand(T); % exact solution
plot(T,Y)
plot(t,y)
legend('exact soln','approx soln','location','best')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!