Array indices must be positive integers or logical values. Midpoint Method help
1 回表示 (過去 30 日間)
古いコメントを表示
clc
clear all
close all
tic
h = 0.25 ; % Step size = 0.25
t = 0:h:2; % the range of x from 0 to 2 with a step size 0.25
y = zeros(1,length(t)); % matrix for allocate the y values
y_a = 2.02974 * 10^65; % analytical value at x=2
y(0) = 0 ; % initial value
n = numel(y);
func = @(x,y) (-200000 .* y + 200000 .* exp(-x) - exp(-x)); % the derivative function
for i=1:n-1
k_1 = func(t(i),y(i)); % to call the derivative function for k1
k_2 = func(t(i)+0.5.*h,y(i)+0.5.*h.*k_1); % to call the derivative function for k2
y(i+1) = y(i) + (h.*k_2);
end
E = (abs(y_a-y)/y_a)*100; %Relative error at x=2
toc
I try to solve the func equation with initial condition y(0)=0 but MATLAB gives me error which is
Array indices must be positive integers or logical values.
Error in Midpoint (line 10)
y(0) = 0 ; % initial value
Can you help asap?
0 件のコメント
採用された回答
Jonas
2022 年 5 月 3 日
編集済み: Jonas
2022 年 5 月 3 日
matlab's indexing starts with 1 and not with 0, the first element of y is y(1). maybe you wanted to write y(t==0)=0, which is the same as y(1)=0 here
その他の回答 (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!