DIfferences on Sloped field using 'dsolve' and 'ode45'
7 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
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)
その他の回答 (1 件)
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
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)
% find initial value at x = -5
iv = double(subs(ySol, x, -5))
参考
カテゴリ
Help Center および File Exchange で Particle & Nuclear Physics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!