Equation exceeding matrix dimensions

I get a ??? Index exceeds matrix dimensions. error for the last line in the code. I am trying to define an equation u(i+1) that includes u(i) and u(i-1) (which is itself in a previous iteration) but ran into this problem.
i = 2:4000; p0 = 10; t1 = 5; dt = 0.05;
t = dt*i;
p(1) = p0*dt/t1;
p(i) = ...
(p0*t/t1) .* (0 <= t & t < t1 ) + ...
(p0) .* (t1 <= t & t < 2*t1 ) + ...
(-p0*t/t1+3*p0) .* (2.*t1 <= t & t <= 3*t1) + ...
(0) * (t > 3*t1);
u0 = 0; ud0 = 0; udd0 = 10;
un1 = u0 - dt*ud0 + (dt^2)*udd0/2;
u(1) = (p0 - un1 - u0);
u(2) = (p(1) - u0 - u(1));
u(i+1) = (p(i) - u(i-1) - u(i));
plot(t,u(i+1))

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Feb. 2012

0 Stimmen

Your problem is with
u(i+1) = (p(i) - u(i-1) - u(i));
MATLAB processes all of the entries simultaneously, so it is going to be trying to access u(3:4000) at a time when only u(1) and u(2) have been defined. MATLAB does not handle such situations iteratively. If you want a difference equation based upon previous values, write it as a loop (or find a way to code it in terms of filter())

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by