How to solve a second order nonlinear differential equations with a step function
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
for 0<=t<=10 solve
5 x'' + x' + t x^3 = t + 3 u(x) where u(x) = 1 for 1<=x<=2
=0 otherwise
Accepted Answer
Edit: The
is not signum-based, and the constraint for the velocity
is defined in an Event function called velocityEventsFcn.
options = odeset('Events', @velocityEventsFcn);
[t, x, te, xe, ie] = ode23s(@odefcn, [0 10], [0.5 1.5], options);
plot(t, x, 'linewidth', 1.5)
function dxdt = odefcn(t, x)
dxdt = zeros(2, 1);
u = max(0, min(min(100000*x(1) - 99999, 1), min(1, -100000*x(1) + 200001)));
dxdt(1) = x(2);
dxdt(2) = (t + 3*u - (x(2) + t*x(1)^3))/5; % 5*x" + x' + t*x^3 = t + 3*u(x)
end
function [position, isterminal, direction] = velocityEventsFcn(t, x)
position = x(2); % When velocity x(2) = 0,
isterminal = 1; % the integration stops,
direction = 0; % and the velocity cannot go into negative no matter what
end

13 Comments
Sudipta Mukherjee
on 15 Jun 2022
can you write u different way without using signum function ? and add one more thing that x(2) cant be negative for this whole simulation.
Walter Roberson
on 15 Jun 2022
ode45 and related functions are not compatible with discontinuous functions. You would need to stop at the boundaries and restart.
Effective clarification will improve the communication. I have two queries.
Query #1:
Perhaps I interpreted your u(x) incorrectly. It is true that ode45 does not work well with jump discontinuities. Would you enlighten exactly which part of the mathematics that incorrectly describes
? Or, the signum function is disallowed by your Professor? Or, a fancy continuous function is preferred?
x = 0:0.01:3;
u = max(0, min(min(1000*x - 999, 1), min(1, -1000*x + 2001)));
plot(x, u, 'linewidth', 1.5)
grid on
xlabel('x')
ylabel('u')
ylim([-0.5 1.5])

Query #2:
You have mentioned that
cannot be negative for the entire simulation. Does your ODE correctly reflect this? The ODE solver only integrates the differential equations that are given. If you can provide a little more critical information about the system that you want to simulate, then it will be helpful in the coding process.
Sudipta Mukherjee
on 17 Jun 2022
At u(1) and u(2) , It's not mandatory to use ode45 only.
Actually
is the velocity then how to put a condition that
is the velocity then how to put a condition that
Sam Chak
on 17 Jun 2022
@Sudipta Mukherjee, Still waiting for your technical reply on Query #1. Why would you want to avoid using signum function to describe
? Is the newly proposed function acceptable to describe
?
On
, we know nothing about the physical dynamics of your system. Either the differential equations are modeled by you, or they come from a reference (your supervisor, books, journal paper, etc.).
Sudipta Mukherjee
on 17 Jun 2022
then u(1)=u(2)=Thanks for your clarification. Now I see...
If
is strictly desired, and the proposed signum-based boxcar function gives
, then the requirement
is not met.
is strictly desired, and the proposed signum-based boxcar function gives However, thinking deeper, would x (position) reach
or
in finite time?
I'm just trying to help and I know that
is a piecewise function. What mathematical differentiable function do you suggest for
? Anyhow, I have edited the Answer to run the sim for
.
Sudipta Mukherjee
on 5 Jul 2022
if
then what will be the u(x) function in the matlab code?
Sam Chak
on 5 Jul 2022
I did't try, but you can use the non-signum template to design for the new
.
Sudipta Mukherjee
on 5 Jul 2022
Edited: Sudipta Mukherjee
on 5 Jul 2022
u=0
if x(1)>=0 && x(1)<=10^-9
u=1;
end
can i use this?
Walter Roberson
on 5 Jul 2022
Yes, go ahead. It can't hurt, at least not more than what you are already doing.
Walter Roberson
on 5 Jul 2022
Are you still working on the 5 x'' + x' + t x^3 = t + 3 u(x) just with a different range over which u(x) is 1?
Could you remind us what the boundary conditions are?
options = odeset('Events', @velocityEventsFcn);
[t, x, te, xe, ie] = ode23s(@odefcn, [0 10], [-0.5 1e-5], options);
plot(t, x, 'linewidth', 1.5)
legend({"x'", "x''"})

function dxdt = odefcn(t, x)
dxdt = zeros(2, 1);
u = x(1)>=0 && x(1)<=10^-9;
dxdt(1) = x(2);
dxdt(2) = (t + 3*u - (x(2) + t*x(1)^3))/5; % 5*x" + x' + t*x^3 = t + 3*u(x)
end
function [position, isterminal, direction] = velocityEventsFcn(t, x)
position = x(2); % When velocity x(2) = 0,
isterminal = 1; % the integration stops,
direction = 0; % and the velocity cannot go into negative no matter what
end
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)