How do I plot a direction field for x'=y and y'=-sinx?
8 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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!
0 Comments
More Answers (0)
See Also
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!
