DIfferences on Sloped field using 'dsolve' and 'ode45'
古いコメントを表示
I tried to create the slope field with 'dsolve' and I received the following results.
syms y(x) % Define the symbolic function y(x)
% Define the differential equation
eq = diff(y, x) == sin(y);
% Solve the general solution of the differential equation
gsol = dsolve(eq);
% Define the initial condition and solve the particular solution
cond = y(0) == 1;
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, -1:0.5:5);
title('Particular Solution and Slope Field of the Differential Equation')
axis tight
m=sin(y);
L=sqrt(1+m.^2);
quiver(x,y,1./L,m./L)
hold off;
However when I used 'ode45' the results it were not what i wanted, as you can see bellow. What should I change to my code?
%Define the function
f = @(u,v) sin(v);
% Solve the differential equation using ode45
[u,v] = ode45(f,[0:0.1:5],1);
% Plot the solution of the differential equation
plot(u, v, 'LineWidth', 2) % Increase line width for better visibility
hold on
% Create a meshgrid for the vector field
[x, y] = meshgrid(-5:0.5:5, -1:0.5:5);
% Calculate the vector field
m = sin(y);
L = sqrt(1 + m.^2);
% Plot the vector field using quiver
quiver(x, y, 1./L, m./L, 'r') % Use red color for vectors
% Set the axis limits to fit the data
axis tight
% Add title
title('Solution of the differential equation and vector field')
% Add a legend
legend('ode45 solution', 'Vector field')
% Turn off hold
hold off
% Improve the overall aesthetics
set(gca, 'FontSize', 7) % Set font size for readability
grid on % Add a grid for better readability of the plot
採用された回答
その他の回答 (1 件)
The reason for obtaining different results is due to the selection of an incorrect initial value for the ode45 run.
% Define the function
f = @(u,v) sin(v);
% Solve the differential equation using ode45
% [u, v] = ode45(f, [-5 5], 1); % pick wrong initial value f(-5) = 1
[u, v] = ode45(f, [-5 5], 7.35825e-3); % adjust initial value f(-5) so that f(0) = 1
% Plot the solution of the differential equation
plot(u, v, 'LineWidth', 2) % Increase line width for better visibility
hold on
% Create a meshgrid for the vector field
[x, y] = meshgrid(-5:0.5:5, -1:0.5:5);
% Calculate the vector field
m = sin(y);
L = sqrt(1 + m.^2);
% Plot the vector field using quiver
quiver(x, y, 1./L, m./L, 'r') % Use red color for vectors
% Set the axis limits to fit the data
axis tight
% Add title
title('Solution of the differential equation and vector field')
% Add a legend
legend('ode45 solution', 'Vector field')
% Turn off hold
hold off
% Improve the overall aesthetics
set(gca, 'FontSize', 7) % Set font size for readability
grid on % Add a grid for better readability of the plot
4 件のコメント
Athanasios Paraskevopoulos
2024 年 3 月 25 日
Yes, @Athanasios Paraskevopoulos. If you can actually analytically find the initial value at u = -5 because the differential equation has an analytical solution.
format long
v = @(u) 2*acot(exp(-u)*cot(1/2)); % analytical solution
u = -5:1e-4:5;
iv = v(-5) % initial value at u = -5
plot(u, v(u)), hold on
plot(0, v(0), 'p', 'markersize', 20), grid on
xlabel u, ylabel v
format long
syms y(x) % Define the symbolic function y(x)
% Define the differential equation
eq = diff(y, x) == sin(y);
% Define the initial condition and solve the particular solution
cond = y(0) == 1;
ySol(x) = dsolve(eq, cond)
% find initial value at x = -5
iv = double(subs(ySol, x, -5))
Athanasios Paraskevopoulos
2024 年 3 月 25 日
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





