Index exceeds matrix dimensions
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
clear all; close all;
tend = 0.1;
h = 0.025;
n = tend/h; %number of timesteps
w = 1; %y(0)=1
wE = w;
t = 0;
for j = 1:n
wE = wE + h*(1+(t-wE)^2); %Forward Euler Method
t = t + h;
disp([num2str(t,10),' & ', num2str(wE,10), '\\']); %display every iteration
end
disp([ num2str(wE(2)-wE(1))]);
I want to display the difference between the first and the second iteration of the for loop, but I get the "Index exceeds matrix dimensions" error. What is wrong with this code?
0 件のコメント
回答 (1 件)
James Tursa
2019 年 4 月 9 日
You don't index wE in your loop when you are doing the iterations, so each iteration overwrites the previous iteration. Looks like you need something like this:
wE(j+1) = wE(j) + h*(1+(t-wE(j))^2); %Forward Euler Method
t = t + h;
disp([num2str(t,10),' & ', num2str(wE(j+1),10), '\\']);
And consider pre-allocating wE.
0 件のコメント
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!