How do you solve a nonlinear ODE with Matlab using ode45?
22 views (last 30 days)
Show older comments
I have done this with a linear ODE which had the equation x''+(c/m)*x'+(g/L)*x = 0 where x(0) = pi/6 and x'(0) = 0
This is the function file followed by the script file I used:
function output1 = linearode(t,y)
c = 2;
m = 1;
g = 10;
L = 1;
output1 = [ y(2); -1*(c/m)*y(2)-(g/L)*y(1)];
_________________________________________________________________________________________________________
Nt = 101; %Step Size of time
ti = 0; %Initial time (sec)
tf = 10; %Final time (sec)
t = linspace(ti,tf,Nt); %Time vector (sec)
x1 = pi/6; %Initial Position (radians)
v1 = 0; %Initial Velocity (radians/s)
%Initial position and velocity for ode45 routine
initial1 = [x1, v1];
%ode45 routine where y1 is the Angular Position (degrees) of Case 1, Method 3
[t1,y1] = ode45(@linearode,t,initial1);
plot(t1,y1(:,1)*180/pi),grid on
________________________________________________________________________________________________________
These two files represent a solution using ode45 for a linear ODE. I would like to do the same with a nonlinear ODE specifically x''+(c/m)*x'+(g/L)*sin(x) = 0 where x(0) = pi/6 and x'(0) = 0. (THE DIFFERENCE IS THE USE OF THE SIN FUNCTION). How can this be done?
0 Comments
Accepted Answer
Star Strider
on 6 Dec 2014
You have to describe your second-order ODE as two first-order ODEs, just as you have with your first ODE. That is all that is necessary. (That’s relatively easily done, and if you don’t want to do it yourself and if you have the Symbolic Math Toolbox, you can use the odeToVectorField function and matlabFunction to do it for you.) Then integrate it with ode45 just as you have with your current ODE.
The ode45 solver shouldn’t have any problems with it. If it does, it will let you know.
6 Comments
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!