How do you solve a nonlinear ODE with Matlab using ode45?
61 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2014-12-6
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.
更多回答(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!