DIfferences on Sloped field using 'dsolve' and 'ode45'

조회 수: 7 (최근 30일)
Athanasios Paraskevopoulos
Athanasios Paraskevopoulos 2024년 3월 25일
댓글: Athanasios Paraskevopoulos 2024년 3월 25일
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

채택된 답변

Torsten
Torsten 2024년 3월 25일
편집: Torsten 2024년 3월 25일
The dsolve solution satisfies y(0) = 1:
cond = y(0) == 1;
the ode45 solution satisfies y(-5) = 1:
[u,v] = ode45(f,[-5 5],1);
The slopefield looks the same to me.
  댓글 수: 3
Torsten
Torsten 2024년 3월 25일
f = @(u,v) sin(v);
[ur,vr] = ode45(f,[0 5],1);
[ul,vl] = ode45(f,[0 -5],1);
u = [flipud(ul);ur];
v = [flipud(vl);vr];
plot(u, v, 'LineWidth', 2)
Athanasios Paraskevopoulos
Athanasios Paraskevopoulos 2024년 3월 25일
@Torsten Cool thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sam Chak
Sam Chak 2024년 3월 25일
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
Sam Chak
Sam Chak 2024년 3월 25일
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)
ySol(x) = 
% find initial value at x = -5
iv = double(subs(ySol, x, -5))
iv =
0.007361881194388
Athanasios Paraskevopoulos
Athanasios Paraskevopoulos 2024년 3월 25일
@Sam Chak That was really helpful

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by