How do you solve a nonlinear ODE with Matlab using ode45?
조회 수: 59 (최근 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.
댓글 수: 6
추가 답변 (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!