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!