How do I get this plot in Matlab?

조회 수: 2 (최근 30일)
Yeen Kit Lee
Yeen Kit Lee 2023년 12월 3일
댓글: Yeen Kit Lee 2023년 12월 4일
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)]
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
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)');

답변 (1개)

Chunru
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))
X_general = function_handle with value:
@(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)');
  댓글 수: 2
Yeen Kit Lee
Yeen Kit Lee 2023년 12월 4일
Thanks I can't believe i forgot to use [] for vectors! Let me try this out thank you for helping.
Yeen Kit Lee
Yeen Kit Lee 2023년 12월 4일
Im trying to get as close to this image as possible, let me try to figure out how to mess with the code and if there's anything else I will post another thread thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Vector Fields에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by