how do i get a proper time plot for this qudratic equation?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
the script seems to be not behaving properly,as it is supposed to be ?
% Define the range for x
x = linspace(-5, 5, 10); % 10 points between -5 and 5
dx1 = 2*x.^2 - 8; % Compute the derivative (velocity field)
% Create a figure
figure;
hold on;
% Plot the real line (x-axis)
plot(x, zeros(size(x)), 'k'); % The x-axis (real line)
% Plot the vector field as arrows
for i = 1:length(x)
if dx1(i) > 0
% Arrow pointing to the right (positive dx)
quiver(x(i), 0, 0.5, 0, 'r', 'MaxHeadSize', 0.5);
elseif dx1(i) < 0
% Arrow pointing to the left (negative dx)
quiver(x(i), 0, -0.5, 0, 'b', 'MaxHeadSize', 0.5);
end
end
% Plot equilibria points
plot([-2, 2], [0, 0], 'bo', 'MarkerFaceColor', 'b', 'MarkerSize', 8); % Equilibria at x = -2 and x = 2
% Labels and title
xlabel('x');
ylabel('x-dot');
title('1D Vector Field for x-dot = 2x^2 - 8');
ylim([-1 1]); % Set y-axis limits to keep focus on x-axis
xlim([-4 4]); % Set x-axis limits
grid on; % Add grid
% Add text annotations for equilibrium points
text(-2, 0.2, 'Stable Equilibrium: x = -2', 'HorizontalAlignment', 'center');
text(2, 0.2, 'Unstable Equilibrium: x = 2', 'HorizontalAlignment', 'center');
hold off;
t = [-5 5];
ode_function = @(t,x) 2*x.^2-8;
x0 = [-2 2];
[time, x] = ode45(ode_function, t, x0);
figure;
plot(time, x, 'LineWidth',0.5);
title('Solution of dx/dt = 2x^2 - 8 vs Time');
xlabel('Time t');
ylabel('x(t)');
xlim([-t(2) t(2)]); % Adjust x-axis limits for clarity
ylim([-3 3]); % Set y-axis limits to see behavior around -2 and 2
grid on;
the first part works perfectly fine, but the 2nd part where i have to plot wrt time, is where i am having trouble? its supposed to be going from x-2 being stable , and 2 being unstable, the plot should depict that.
but I am unable to get that done properly
1 Kommentar
Walter Roberson
am 28 Sep. 2024
ode_function = @(t,x) 2*x.^2-8;
x0 = [-2 2];
[time, x] = ode45(ode_function, t, x0);
Your ode_function is written in a way that appears to be single input and single output. But you are passing in vector x0, so the input x to ode_function will be a 2 x 1 vector. Your 2*x.^2-8 is going to be calculated with that 2 x 1 vector, returning a 2 x 1 vector. We must ask whether this is really what you want.
Antworten (1)
William Rose
am 29 Sep. 2024
Bearbeitet: William Rose
am 29 Sep. 2024
Let us try different initial values for x: x0=[-2,...,2]. Use a different initial value on each pass. Store results in a cell array, because t,x will have different lengths on different passes, so an array would not work.
t = [-5 5];
x0 = [2,1.99,1.95,1.8,1.5,0,-2]; % initial values
N=length(x0);
C={1,N}; % allocate cell array for results
legstr={1,N}; % allocate cell array for plot legend strings
ode_function = @(t,x) 2*x.^2-8;
for i=1:N
[time, x] = ode45(ode_function, t, x0(i));
C{i}=[time,x];
legstr{i}=num2str(x0(i));
end
% Plot results
figure
hold on
linespec={'-r.','-g.','-b.','-c.','-m.','-y.','-k.'};
for i=1:N
data=C{i};
time=data(:,1); x=data(:,2);
plot(time, x, linespec{i});
end
title('dx/dt = 2x^2 - 8, at different initial values');
xlabel('Time t'); ylabel('x(t)');
legend(legstr); grid on
OK
0 Kommentare
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!