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!