MATLAB Answers

Solving differential equations with inital conditions

8 views (last 30 days)
Hadi Ahmed
Hadi Ahmed on 8 Mar 2020
Answered: Guru Mohanty on 11 Mar 2020
Attempting to solve a differential with inital conditions and a time range and then plot the function and it's derivative on a graph and to determine the time when the function first crosses zero.
x0 = [20;0]; %Inital conditions
tr = [0; 300]; %Time range for our function
[t,y]=ode45(@fun, tr, x0);
Pi=find(t<0); %Find elements of Z less than 0
Pi1=Pi(1); % the array index for first Z element<0
zz=Z(Pi1-4:Pi1+3); %Pick 8 elements from Z
tt=t(Pi1-4:Pi1+3); %Pick 8 elements from t
% (tt, zz) will be used for interpretation
plot(t,y)
plot(tt,zz)
function dydx = fun(t,x)
%dx(1)/dt = dy/dt = x(2)
%dx(2)/dt = d^2y/dt^2
dydx = [x(2);
0.375*sign(x(2))*(x(2))^2+0.00074*x(1)];
end
The error I get is shown below. If anyone can help that'd be great
Warning: Failure at t=2.082134e+01. Unable to meet
integration tolerances without reducing the step size below
the smallest value allowed (5.684342e-14) at time t.
> In ode45 (line 360)
In ME2602Ahmed_P4_1 (line 4)
Index exceeds the number of array elements (0).
Error in ME2602Ahmed_P4_1 (line 7)
Pi1=Pi(1); % the array index for first Z element<0

  1 Comment

Star Strider
Star Strider on 8 Mar 2020
The Warning:
Warning: Failure at t=2.082134e+01. Unable to meet
integration tolerances without reducing the step size below
the smallest value allowed (5.684342e-14) at time t.
is due to ‘fun’ becoming infinite at about that point.
It appears to simply be the nature of your differential equations. I see no specific problem (such as divide-by-zero) that could otherwise cause that.

Sign in to comment.

Answers (1)

Guru Mohanty
Guru Mohanty on 11 Mar 2020
Hi, I understand you are getting warning and error in solving the system of differential equation. The warning is due to the ode45 solver is getting a singularity or discontinuity at t=20.82 in your time range [0:300]. So, the solver could not find solution further. You can also visualize it by plotting ‘y’ vs ‘t’.
After changing the range to [0:20] The solver can solve the differential equation.
The error is due to the find function tries to find the index of negative values of t. But as there are no negative value in t, the command
Pi=find(t<0);
returns an empty array. So
Pi1=Pi(1); % the array index for first Z element<0
Will pass an error.

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by