Plot a direction field whose vectors have the same size
5 ビュー (過去 30 日間)
古いコメントを表示
I want to plot the direction field of the ODE .
Here is my script.
% Clear previous figures, workspace, and command window
clc; clear; close all;
% Define the symbolic variable and the differential equation
syms y(t)
eqn = -y + exp(-t)*cos(10*t) == diff(y,t);
cond = y(0) == 0;
sol = dsolve(eqn, cond);
fplot(sol, [0, 2])
hold on % freeze axis
% Define the differential equation using a function handle for the
% direction field
dydt = @(t, y) -y + exp(-t) .* cos(10*t);
% Define the range of t and y values for the meshgrid
t = linspace(0, 2, 15); % Adjust it as needed
y = linspace(-0.1, 0.1, 15); % Adjust it as needed
% Create a meshgrid of t and y values
[T, Y] = meshgrid(t, y);
% Compute the direction values using the differential equation
dydt_values = dydt(T, Y);
% Normalize the direction values for better visualization
L = sqrt(1 + dydt_values.^2);
dydt_normalized = dydt_values./L;
% Plot the direction field using quiver
quiver(T, Y, 1./L, dydt_normalized, 0.3);
% Set axis labels and title with LaTeX interpretation
xlabel('t'); ylabel('y');
title('Direction Field for $y'' = -y + e^{-t} \cos(10t)$', 'Interpreter', 'Latex');
% Adjust axis limits and remove the box around the plot
axis tight; box off;
% Show the plot with grid lines and minor grid lines
grid on; grid minor;
% Move axis origin at (0,0)
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
hold off % stop freewing axis
How can one force the vectors to have the same size and have, if possibly, better looking heads?
Thanks a lot!
0 件のコメント
回答 (1 件)
Voss
2024 年 1 月 13 日
編集済み: Voss
2024 年 1 月 13 日
The vectors do have the same size, in terms of the x and y of the axes. However, the vectors with larger vertical component appear longer because the data aspect ratio of the axes is about 1:8 (Y:X), i.e., a unit in the y direction is the same distance on the screen as 8 units in the x direction.
Calculation and original plot:
syms y(t)
eqn = -y + exp(-t)*cos(10*t) == diff(y,t);
cond = y(0) == 0;
sol = dsolve(eqn, cond);
dydt = @(t, y) -y + exp(-t) .* cos(10*t);
t = linspace(0, 2, 15); % Adjust it as needed
y = linspace(-0.1, 0.1, 15); % Adjust it as needed
[T, Y] = meshgrid(t, y);
dydt_values = dydt(T, Y);
% Normalize the direction values for better visualization
L = sqrt(1 + dydt_values.^2);
dydt_normalized = dydt_values./L;
% original Plots
figure
fplot(sol, [0, 2])
hold on
quiver(T, Y, 1./L, dydt_normalized, 0.3);
xlabel('t'); ylabel('y');
title('Direction Field for $y'' = -y + e^{-t} \cos(10t)$', 'Interpreter', 'Latex');
axis tight; box off;
grid on; grid minor;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
Note the axes' DataAspectRatio:
ax.DataAspectRatio
If you want the vectors to all appear the same size on the screen, there are two options:
Option 1: Set the data aspect ratio of the axes to be 1:1, for which you can use axes equal.
% Plots
figure
fplot(sol, [0, 2])
hold on
quiver(T, Y, 1./L, dydt_normalized, 0.3);
xlabel('t'); ylabel('y');
title('Direction Field for $y'' = -y + e^{-t} \cos(10t)$', 'Interpreter', 'Latex');
axis equal; box off;
grid on; grid minor;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
ax.DataAspectRatio
Option 2: Keep the axes' apparent size more-or-less how it was, and scale the vertical component of the vectors by a factor. Here I use a factor of 1/9.7815, which is roughly equal to the axes Y:X aspect ratio it gives; this was found by iteratively varying the factor starting with 1/8.22 until the axes aspect ratio matched the factor, thus ensuring that the arrows are all the same length on the screen.
% Plots
figure
fplot(sol, [0, 2])
hold on
quiver(T, Y, 1./L, dydt_normalized/9.7815, 0.3);
xlabel('t'); ylabel('y');
title('Direction Field for $y'' = -y + e^{-t} \cos(10t)$', 'Interpreter', 'Latex');
axis tight; box off;
grid on; grid minor;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
ax.DataAspectRatio
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Line Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!