Solve a second order differential equation with ODE45
1 view (last 30 days)
Show older comments
Hello,
I had managed to make this file work but recently it's very slow and I'm not able to run it properly. I think the issue might come form the vectors. Here is the script and function file:
function xdot = OWC (t,x)
global g h a
xdot = [((g*h-((x(2).^2)/2)-g*x(1))/(x(1)+a)); x(1)];
and the script:
global g h a;
g=9.81;
h=0;
for a=0;
[t,x] = ode45('OWC',[0 10],[1.0 1.0]);
plot (t,x)
hold on
end
for a=1;
[t,x] = ode45('OWC',[0 10],[1.0 1.0]);
plot (t,x)
hold on
end
The original equation is: y"=(g*h-(y'^2/2)-g*z)/(z+a)
I haven't used Matlab much so I wouldn't be surprised if I'm using the wrong technique but I did manage to make this script work before and somehow I've ruined it.
Thank you.
Accepted Answer
Mischa Kim
on 25 Jan 2014
Hello Peter, there are a couple of issues:
- It looks like you want to use if rather than for statements to execute either of the blocks depending on the value of a , correct?
- When you set the initial conditions to [0; 0] and with h = 0 the derivatives are all zero at all time so you get a flat line (provided that a != 0).
- When you set the initial conditions to [0; 0] and with a = 0, h = 0 you have a devide by zero the differential equations resulting in NaN.
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!