How to make a live plot with changing variables

17 ビュー (過去 30 日間)
Yuanhang LUO
Yuanhang LUO 2021 年 5 月 26 日
回答済み: Abhas 2024 年 2 月 14 日
In this example, I am using forward euler method to solve dy/dt=a*y, I am typically interesting in the time steps dt chosen for the approximation. How can I make a live plot with changing data in terms of many dt values. Below is the orignal code for this example.
clear all;
t0=0;
tf=3;
dt=0.08;
t=t0:dt:tf;
y(1)=1;
a=-10; % dy/dt=a*y
for i=1:length(t)-1
y(i+1)=y(i)+dt*a*y(i);
end
x=linspace(t0,tf);
y_exact=exp(a*x);
figure;
hold on
plot(t,y,'bo-')
plot(x,y_exact,'Linewidth',1)
xlim([0 1.6])
ylim([-1.5 1.5])
xlabel('time(s)')
ylabel('y(t)')
title('forward euler')
legend('forward euler','exact solution')
hold off

回答 (1 件)

Abhas
Abhas 2024 年 2 月 14 日
Hello Yuanhang,
To create a live plot that visualizes the solution of the differential equation dy/dt = a * y using the Forward Euler method for different time step sizes (dt), you can follow these steps in MATLAB:
  1. Initialize parameters 't0', 'tf', 'y0', 'a', and an array 'dt_values' for different time steps.
  2. Set up a figure for plotting with appropriate labels and title.
  3. Loop over each 'dt' in 'dt_values', using the Forward Euler method to compute the solution.
  4. In each iteration, plot the numerical solution and the exact solution on the same graph using "plot" function.
  5. Clear the previous plot with "cla" before plotting the new one.
  6. Use "drawnow" to update the plot with the new data.
  7. Include a "pause(1)" to allow each plot to be displayed for a short time.
Here's the MATLAB code implementing these steps:
% Initialize parameters
t0 = 0;
tf = 3;
y0 = 1;
a = -10; % dy/dt = a * y
dt_values = [0.08, 0.05, 0.02, 0.01]; % Different dt values for testing
% Prepare the figure for live plotting
figure;
xlabel('time(s)');
ylabel('y(t)');
title('Forward Euler Method');
% Loop over each dt value
for dt_idx = 1:length(dt_values)
dt = dt_values(dt_idx); % Current dt value
t = t0:dt:tf; % Time vector for current dt
y = zeros(1, length(t)); % Initialize y vector
y(1) = y0; % Initial condition
% Forward Euler method to solve the ODE
for i = 1:(length(t) - 1)
y(i + 1) = y(i) + dt * a * y(i);
end
% Exact solution for comparison
x = linspace(t0, tf, 1000); % Fine grid for plotting exact solution
y_exact = y0 * exp(a * x);
% Update the plot
cla; % Clear the current axes
hold on;
plot(t, y, 'bo-'); % Plot numerical solution
plot(x, y_exact, 'r', 'LineWidth', 1); % Plot exact solution
hold off;
xlim([t0, tf]);
ylim([min(y_exact), max(y_exact)]);
legend('Forward Euler', 'Exact Solution');
drawnow; % Update the plot immediately
pause(1); % Pause for 1 second before the next iteration
end
This code will produce a sequence of plots, each showing the numerical solution for a different 'dt' value, alongside the exact solution for comparison. The "pause(1)" command ensures that each plot is displayed for one second before moving on to the next 'dt' value. Adjust this value as necessary to control the visualization speed.
You may refer to the following documentation link to read more about updating the plot with new data: https://in.mathworks.com/help/matlab/ref/drawnow.html

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by