How do I get this plot in Matlab?

2 Ansichten (letzte 30 Tage)
Yeen Kit Lee
Yeen Kit Lee am 3 Dez. 2023
Kommentiert: Yeen Kit Lee am 4 Dez. 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)');

Antworten (1)

Chunru
Chunru am 4 Dez. 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 Kommentare
Yeen Kit Lee
Yeen Kit Lee am 4 Dez. 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 am 4 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Vector Fields finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by