Error using griddedInterpolant. The grid vectors are not strictly monotonic increasing.

I am trying to solve a System of differential equations for a torsional pendulum with euler forward finite difference method. (lines 37 to 51) After every step I need to find an specific value of time (tn) by interpolation with the angular position y(1,:) and the time t (lines 59 to 65). It keeps saying that the values are not strictly monotonic increasing but I am making sure they are by setting a constraint that angular speed y(2,:) is never negative or equal to zero. (lines 53 to 57)
If I run the code for a very small time It works but if I want to do it for a longer time it keeps giving me that error. Is there anything I can do?

 Accepted Answer

You initialize y to all 0. You loop over the column number, extending the non-zero portion one item at a time via
y(:,i+1) = y(:,i) + ydot*h;
You have a test
if y(1,i+1)<2*pi/N
and if that test is true then you do not do the interp1(). It so happens that the first time the test is false is at i = 536. At that point you will have filled in up to y(:,537) leaving y(:,538:end) as 0. But your interp1() call is
t_tn = interp1(y(1,:),t,PHIt_tn);
which uses all if y(1,:) including the part that is still 0. As the previous values were increasing and greater than 0, the sudden transition to 0 is non-monotonic.

1 Comment

Thanks so much for your help!
I just changed my code to: t_tn=interp1(y(1,1:i),t(:,1:i),PHIt_tn);
So it will just evaluate the interpolation in the previous points and not the part that is still 0. This seems to be working good!

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!