how do i get a proper time plot for this qudratic equation?
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 properlythe 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 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 code generation, ode45, equation MATLAB Answers — New Questions