How do I get this plot in Matlab?
2 次查看(过去 30 天)
显示 更早的评论
I am trying to get this particular plot from this general solution from the files attached.
I have some initial code and I understand that my general solution is wrongly types and I am trying to do it without the quiver function in Matlab is that possible?
Code 1:
% Define the general solution
X_general = @(c1, c2) [c1 * (2 - 1) * exp(-t)+ c2 * (1 2) * exp(-6 * t)]
y=-x/2;
xlabel('x');
ylabel('y');
% Substitute the values of c1 and c2 based on your calculations
c1 = 1/5;
c2 = 3/5;
hold on;
%plot([c1 * (2 - 1) * exp(-t)+ c2 * (1/2) * exp(-6 * t)], 'b-', 'LineWidth', 2, 'DisplayName', 'X(1)');
plot(y)
I plotted against time for code 2 to test out quiver:
t = linspace(0, 5, 20);
% Define the general solution
X_general = @(t, c1, c2) [c1 * (2 - 1) * exp(-t); c2 * (1/2) * exp(-6 * t)]
% Substitute the values of c1 and c2 based on your calculations
c1 = 1/5;
c2 = 3/5;
% Evaluate the solution for the given time vector
X_values_general = X_general(t, c1, c2);
% Create a meshgrid for the quiver plot
[T, C] = meshgrid(t, linspace(0, 5, 20));
% Evaluate the derivatives
dXdt = zeros(size(T));
dXdt(:, :, 1) = (2 - 1) * C * exp(-T);
dXdt(:, :, 2) = (1/2) * C * exp(-6 * T);
% Create a quiver plot
figure;
quiver(T, C, dXdt(:, :, 1), dXdt(:, :, 2), 'AutoScale', 'on', 'LineWidth', 1.5);
xlabel('Time');
ylabel('Constant Value');
title('Quiver Plot of the Vector Field');
% Overlay the trajectory
hold on;
plot(t, c1 * (2 - 1) * exp(-t), 'b-', 'LineWidth', 2, 'DisplayName', 'X(1)');
plot(t, c2 * (1/2) * exp(-6 * t), 'r-', 'LineWidth', 2, 'DisplayName', 'X(2)');
legend('Vector Field', 'X(1)', 'X(2)');
0 个评论
回答(1 个)
Chunru
2023-12-4
% Define the general solution
% [2 -1] and [1 2] are vectors. Use [ ].
X_general = @(t, c1, c2) (c1 * [2 -1] .* exp(-t)+ c2 * [1 2] .* exp(-6 * t))
t = linspace(0, 5, 20)'; % make this as column verctor
% Substitute the values of c1 and c2 based on your calculations
c1 = 1/5;
c2 = 3/5;
% Evaluate the solution for the given time vector
X_values_general = X_general(t, c1, c2);
% Create a meshgrid for the quiver plot
[T, C] = meshgrid(t, linspace(0, 5, 20));
% Evaluate the derivatives
dXdt = zeros(size(T));
dXdt(:, :, 1) = (2 - 1) * C * exp(-T);
dXdt(:, :, 2) = (1/2) * C * exp(-6 * T);
% Create a quiver plot
figure;
quiver(T, C, dXdt(:, :, 1), dXdt(:, :, 2), 'AutoScale', 'on', 'LineWidth', 1.5);
xlabel('Time');
ylabel('Constant Value');
title('Quiver Plot of the Vector Field');
% Overlay the trajectory
hold on;
plot(t, c1 * (2 - 1) * exp(-t), 'b-', 'LineWidth', 2, 'DisplayName', 'X(1)');
plot(t, c2 * (1/2) * exp(-6 * t), 'r-', 'LineWidth', 2, 'DisplayName', 'X(2)');
legend('Vector Field', 'X(1)', 'X(2)');
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Vector Fields 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!