How to animate background flow field that an object is moving through?

17 views (last 30 days)
Hi there!
I now have a decent code that solves an ode model of mine, using ode45. It is about a rigid plate falling freely through a fluid. It is a 2D problem, so the background flow field is pretty simple, modeled with, say, the velocity field vf = 0i + 5*j, for a uniform, vertical wind updraft that the plate can hover in. I'm also cooking up some more involved 2D flows: for instance, flows that might be periodic in time.
Currently, I wrote a live animation for the trajectory of the plate falling through the fluid, by using a while loop and also manipulating plot( ) and quiver( ) graphics objects. However, I'd like to add to this animation by also animating the flow field, in order to gain intuition about what's going on with both the plate and the flow.
How can I animate a simple flow field?
I'm thinking that using arrows could be nice; I'm also thinking of using dots that trace out the path of the flow.
Thanks!

Accepted Answer

Umar
Umar on 20 Oct 2024

Hi @ Noob,

After going through your comments, in order to achieve a comprehensive animation of both the falling plate and the flow field in MATLAB, you can utilize the quiver function as mentioned in your comments by using arrows representing the flow and plot for the trajectory of the plate. Below is a detailed explanation and the complete code to implement this animation. First create a grid of points in the 2D space where the flow field will be evaluated. For a uniform vertical wind updraft, the velocity field. The plate's motion will be governed by the ordinary differential equations (ODEs) that describe its falling behavior through the fluid by using ode45 to solve these equations, create a loop that updates the position of the plate and the flow field at each time step, using quiver to display the flow and plot to show the plate's trajectory. To visualize the flow path, you can store the previous positions of the flow and plot them as dots.

% Parameters
g = 9.81; % Acceleration due to gravity (m/s^2)
h = 0.1; % Height of the plate (m)
rho_f = 1.225; % Density of fluid (kg/m^3)
A = 0.1; % Cross-sectional area of the plate (m^2)
Cd = 1.0; % Drag coefficient
v_updraft = 5; % Velocity of the updraft (m/s)
% Initial conditions
y0 = [0; 0]; % Initial position [x; y]
v0 = [0; -1]; % Initial velocity [vx; vy]
% Time span
tspan = [0 10]; % Time from 0 to 10 seconds
% ODE function
odefun = @(t, y) [y(2); -g + (0.5 * rho_f * Cd * A * (v_updraft - y(2))^2) / 
(rho_f * A)];
% Solve ODE
[t, y] = ode45(odefun, tspan, y0);
% Create a grid for the flow field
[x, y_grid] = meshgrid(-5:0.5:5, -5:0.5:5);
u = zeros(size(x)); % x-component of velocity
v = v_updraft * ones(size(y_grid)); % y-component of velocity
% Create figure for animation
figure;
hold on;
axis equal;
xlim([-5 5]);
ylim([-5 5]);
title('Plate Falling Through Fluid with Flow Field');
xlabel('X Position (m)');
ylabel('Y Position (m)');
% Initialize plot objects
h_plate = plot(y(:,1), y(:,2), 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
 % Plate
h_flow = quiver(x, y_grid, u, v, 'b'); % Flow field arrows
h_path = plot(nan, nan, 'g.'); % Path tracing
% Animation loop
for i = 1:length(t)
  % Update plate position
  set(h_plate, 'XData', y(i,1), 'YData', y(i,2));
    % Update flow field arrows
    set(h_flow, 'UData', u, 'VData', v);
    % Update path tracing
    set(h_path, 'XData', [get(h_path, 'XData'), y(i,1)], ...
                 'YData', [get(h_path, 'YData'), y(i,2)]);
    % Pause for animation effect
    pause(0.1);
  end
hold off;

Please see attached.

Again, utilizing quiver for the flow representation and plot for the plate's trajectory, you can gain valuable insights into the dynamics of the system.

Feel free to modify the parameters and flow characteristics to explore different scenarios!

Hope this helps.

  7 Comments
Noob
Noob on 21 Oct 2024
Edited: Noob on 21 Oct 2024
Hi Umar!
I think I'll hold off on this, for now, and keep focus on the mathematical modeling and simulations, since there is so much to do there (and animating a flow field seems hard to do). So, for now, plate trajectories using live animation is sufficient, I think. Thanks so much for your time and patience!
Umar
Umar on 22 Oct 2024
Hi @Noob,
Thank you for your thoughtful feedback regarding the project direction. I completely understand your decision to prioritize mathematical modeling and simulations at this stage, as it is indeed a crucial area with much to explore. Focusing on plate trajectories using live animation sounds like a practical approach, and I appreciate your clarity in outlining the current priorities. Should you need any assistance or further discussion on this topic, please do not hesitate to reach out. Thank you once again for your insights and collaboration.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!