How to improve the Slope Field code?

4 views (last 30 days)
I am trying to create my slope fielld manually. Is there any advice how to improve my appearance of the Slope Field? I have read How to Draw a Slope Field in MATLAB but i could not understand how to use Use the slope_field() Function
syms y(x) % Define the symbolic function y(x)
% Define the differential equation
eq = diff(y, x) == x;
% Solve the general solution of the differential equation
gsol = dsolve(eq);
% Define the initial condition and solve the particular solution
cond = y(0) == 3;
psol = dsolve(eq, cond);
% Plot the particular solution
fplot(psol, [-5, 5]);
hold on;
% The slope field for the differential equation
[x, y] = meshgrid(-5:0.5:5, -5:0.5:15);
u = ones(size(x));
v = x; % Because dy/dx = x
quiver(x, y, u, v, 'r');
hold off;

Accepted Answer

Athanasios Paraskevopoulos
Edited: Athanasios Paraskevopoulos on 24 Mar 2024
I used the dirfield.m so I had better visualization
f=@(x,y)x
figure;
dirfield(f,-2:0.2:2,-2:0.2:2)
xlabel('$x$','interpreter','latex','fontsize',17);
ylabel('$y$','interpreter','latex','fontsize',17);
title('Slope Field for $\displaystyle\frac{dy}{dx}=x$',...
'interpreter','latex','fontsize',17);
hold on
[x,y] = ode45(f,[-2,2],3);
plot(x,y)
hold off

More Answers (0)

Community Treasure Hunt

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

Start Hunting!