Solving differential equations with inital conditions
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Star Strider
2020-3-8
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.
回答(1 个)
Guru Mohanty
2020-3-11
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!