How do I plot a direction field for x'=y and y'=-sinx?

8 views (last 30 days)
I have tried:
[x,y] = meshgrid(-5,5:-5,5);
x=-5:.1:5;
y=-sin(x);
u=x;
v=y;
quiver(x,y,u,v)
  2 Comments
Dyuman Joshi
Dyuman Joshi on 8 Nov 2023
"x'=y and y'=-sinx"
What does this mean? Are those differential equations?
Also, you initialize x and y using meshgrid, but you over-write them in the next lines. Which values are the correct ones?
Sophie
Sophie on 8 Nov 2023
I need to plot a direction field for the given planar autonomous system, and those are the equations for the system. I do not really know how to use matlab so I'm not sure which values are the correct one? I think what I was supposed to do is create a new variable when inputting the actual equation part?

Sign in to comment.

Accepted Answer

Taylor
Taylor on 8 Nov 2023
Edited: Taylor on 8 Nov 2023
% Define the range of x and y values
x = linspace(-pi, pi, 20);
y = linspace(-2, 2, 20);
% Create a grid of x and y values
[X, Y] = meshgrid(x, y);
% Calculate the derivatives dx/dt and dy/dt
dXdt = Y;
dYdt = -sin(X);
% Plot the direction field
quiver(X, Y, dXdt, dYdt);
xlabel('x');
ylabel('y');
title('Direction Field');
% Adjust the aspect ratio and grid
axis tight;
grid on;
If you're not feeling comfortable with MATLAB I recommend the free MATLAB Onramp courseto get started!

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!