How do I get this plot in Matlab?

1 view (last 30 days)
Yeen Kit Lee
Yeen Kit Lee on 3 Dec 2023
Commented: Yeen Kit Lee on 4 Dec 2023
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)');

Answers (1)

Chunru
Chunru on 4 Dec 2023
% 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 Comments
Yeen Kit Lee
Yeen Kit Lee on 4 Dec 2023
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 on 4 Dec 2023
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.

Sign in to comment.

Categories

Find more on Vector Fields in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!