How to improve the Slope Field code?

4 ビュー (過去 30 日間)
Athanasios Paraskevopoulos
Athanasios Paraskevopoulos 2024 年 3 月 23 日
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;

採用された回答

Athanasios Paraskevopoulos
Athanasios Paraskevopoulos 2024 年 3 月 24 日
編集済み: Athanasios Paraskevopoulos 2024 年 3 月 24 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by