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

7 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Torsten
Torsten am 25 Mär. 2024
Bearbeitet: Torsten am 25 Mär. 2024
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 Kommentare
Torsten
Torsten am 25 Mär. 2024
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)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sam Chak
Sam Chak am 25 Mär. 2024
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 Kommentare
Sam Chak
Sam Chak am 25 Mär. 2024
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by