Clear Filters
Clear Filters

I managed to change the angle with fixed numbers, but I need it to change over time.

1 view (last 30 days)
I managed to make the angle change to the specified angle (perturb_magnitude) after a defined time (perturb_time). However, I need the "perturb_magnitude" to change based on a matrix "jj = [ds ddd]" where ds = the time interval at which the angle should change (perturb_time), and ddd = the angle to which it should change (perturb_magnitude). The jj matrix is attached.
Finally, I need the angle to update over time, where both time and angle are contained in the matrix "jj = [ds ddd]." ; "jj = [time angle]."
.
.
.
perturb_magnitude = pi/2;
perturb_time = 30; % Time in seconds
perturb_index = find(t >= perturb_time, 1);
x(perturb_index:end, :) = x(perturb_index:end, :) + repmat([perturb_magnitude, perturb_magnitude, perturb_magnitude], numel(t) - perturb_index + 1, 1);
[t_after_perturb, x_after_perturb] = ode45(@(t, x) (equation), t(perturb_index:end), x(perturb_index, :));

Answers (1)

Walter Roberson
Walter Roberson on 20 Oct 2023
Any time you have a value needed for ode calculation change discretely, you need to stop executing the ode*() call at the boundary of the change. If necessary then adjust the boundary conditions. Then call the ode*() function starting from the time you left off.
If the change in parameter happens at particular times, then this can be handled by looping using tspan that end at the scheduled time of the next change. If the change in parameter is not at predictable times (for example it happens when a rotation angle is detected to cross a particular boundary) then you need to use event functions to signal the stop.
It is common for people to use try to use interp1() inside the ode function. Doing that violates the mathematical conditions for the ode*() call to be valid, unless a non-default interpolation method is used that has continuous second derivatives -- for example you can request cubic spline interpolation and that would satisfy the mathematical constraints. But if you do that, remember that the implication of any interpolation method is that the system is "anticipating" the change and moving towards it, such as starting to rotate so that at the given time it will already reach the known angle. (As compared to a rotation that does not start until a mechanism is tripped.)
  11 Comments
Daniel santiago Umaña
Daniel santiago Umaña on 23 Oct 2023
Hello, it didn't solve the issue. When I added 'perturb_index(i) = find((t*10) >= perturb_time(i), 1);' the program worked, but not in the way I wanted.
When I added:
't(perturb_index(end)+1:end) = [];
t(end+1) = t(end) + duration_after_last_perturbation'
I encountered this error:
Arrays have incompatible sizes for this operation. Error in controles6 (line 39) x(perturb_index(i):end, :) = x(perturb_index(i):end, :) + repmat([perturb_magnitude(i), perturb_magnitude(i), perturb_magnitude(i)], numel(t) - perturb_index(i) + 1, 1); Related documentation

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!