ode45 not able to solve one particular IVP

1 view (last 30 days)
Hello there,
the ode (attached) can be solved at three initial conditions (i=3 in the code). However when I change the i to i=4 the code fails. Is there anyway ode45 can be augmented with options to get the solution for the fourth initial condition. Other numerical programs like ode23, etc do not work either. I tried to use dsolve to get the symbolic solution, but output function involves bessel equation so that the plots look very weired. The problem is not a misprint because I am able to use RungeKutta4 to get the fourth solution on positive x-axis but the solution on the negative x-axis is still missing (trying to fix it).
please suggest how can the ode be resolved using ode45/23 etc.!
Thanks.
-Saurav Parmar
% © S. Parmar
% last modified 12-12-2023
% Zill Problem 2 page 38
clear
clc
y = -3:0.4:3; % pick values of y
x = -3:0.4:3; % pick values of x
[xg,yg] = meshgrid(x,y); % create a 2-d array of samples of x and y
dy = xg.^2 - yg.^2; % create dy/dx
dx = ones(size(dy)); % create corresponding time coordinates
figure
ax = gca;
quiver(xg,yg,dx,dy,1.0) % quiver displays the directional arrows
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
xlabel('$$\rm x $$','Interpreter','latex')
ylabel('$$ \rm y $$','Interpreter','latex')
axis tight
ax.FontSize = 16.5;
set(gca,'FontName','Times')
title('$$ \rm dy/dt=x^2 - y^2 $$','Interpreter','latex')
hold on
clear x y
syms y(x) C
% part (b)
xf = [-3.1 3.1];
xi = [-2 3 0 0];
yi = [1 0 0 2];
for i = 1:4
for j = 1:2
[xs,ys] = ode23(@ODEexpn,[xi(i) xf(j)],yi(i)); % get the symbolic solution
disp(xs)
disp(ys)
plot(xs,ys,'color','k',LineWidth=2.5) % plot the solution
% hold on
end
end
fig = gcf;
fig.Units = 'centimeters';
fig.Position = [1 2 25 15.4];
set(gcf,'PaperUnits','centimeters','PaperPosition',[1 2 25 15.4])
function dydx = ODEexpn(x,y)
dydx = x^2 - y^2;
end
  13 Comments
Tushal Desai
Tushal Desai on 18 Dec 2024
(Answers dev) The uploaded files should now be accessible when running your code. The issue has been fixed on our end. Thanks for reporting.

Sign in to comment.

Accepted Answer

Alan Stevens
Alan Stevens on 16 Dec 2024
How about:
y = -3:0.4:3; % pick values of y
x = -3:0.4:3; % pick values of x
[xg,yg] = meshgrid(x,y); % create a 2-d array of samples of x and y
dy = xg.^2 - yg.^2; % create dy/dx
dx = ones(size(dy)); % create corresponding time coordinates
figure
ax = gca;
quiver(xg,yg,dx,dy,1.0) % quiver displays the directional arrows
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
xlabel('$$\rm x $$','Interpreter','latex')
ylabel('$$ \rm y $$','Interpreter','latex')
axis tight
ax.FontSize = 16.5;
set(gca,'FontName','Times')
title('$$ \rm dy/dt=x^2 - y^2 $$','Interpreter','latex')
hold on
% part (b)
xf = [3.1 -3.1];
xi = [-2 3 0 0];
yi = [1 0 0 2];
for i = 1:4
y0 = yi(i);
for j = 1:2
if i==4 && j==2, y0 = -2; end
[xs,ys] = ode45(@ODEexpn,[xi(i) xf(j)],y0);
plot(xs,ys,'color','k','LineWidth',2.5) % plot the solution
end
end
fig = gcf;
fig.Units = 'centimeters';
fig.Position = [1 2 25 15.4];
set(gcf,'PaperUnits','centimeters','PaperPosition',[1 2 25 15.4])
axis([-3.1 3.1 -3 3])
function dydx = ODEexpn(x,y)
dydx = x^2 - y^2;
end
  1 Comment
Saurav
Saurav on 16 Dec 2024
This is very insightful. so it looks like ode45 is working. but the nature of solution is different from what i expected. but thanks a lot. it looks like a starting point for the 4th initial condition.

Sign in to comment.

More Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!