ODE plot with 2nd order diff eqs

1 view (last 30 days)
Sam
Sam on 14 Jul 2015
Answered: Star Strider on 14 Jul 2015
I wanted to check to make sure I am creating the correct graph for problem 1 of the attached project file. It is similar to a previous problem I had to do , so I used that code and just changed the equation and conditions to match this problem.
Fp = @(t,x,w) [x(2); 10*cos(t.*w)-x(1).*4-x(1).*5]; % Spring ODE
ww = [0, 0.5, 1, 2, 4, 8, 16]; % ‘epsilon’ Vector
ts = linspace(0,80, 500); % Time Vector
for k1 = 1:length(ww)
[~, x{k1}]=ode45(@(t,x) Fp(t,x,ww(k1)), ts, [0,1]);
end
figure(1)
for k1 = 1:length(ww)
subplot(length(ww), 1, k1)
plot(ts,x{k1})
legend(sprintf('\\epsilon = %.1f', ww(k1)))
grid
axis([xlim -5 +5])
  1 Comment
Torsten
Torsten on 14 Jul 2015
Your equation to be solved and your initial conditions are not consistent with project2.pdf.
Check again.
Best wishes
Torsten.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Jul 2015
The problem is that the loop code isn’t updated to match your new ODE. (Your ODE code and ode45 call are correct.)
Use this loop instead of the old one:
figure(1)
for k1 = 1:length(ww)
subplot(length(ww), 1, k1)
plot(ts,x{k1}(:,1))
hold on
legend(sprintf('\\omega = %.1f', ww(k1)))
grid
axis([xlim -2 +2])
xv = x{k1}(:,1);
zxix = find((xv.*circshift(xv, [1 0])) <= 0); % Approximate Zero-Crossing Indices
if length(zxix) > 2
ixrng = max(1,zxix(3)-1):min(zxix(3)+1,length(ts)); % Index Range
tzx{k1} = interp1(xv(ixrng), ts(ixrng), 0); % ‘tx’ Values At Zero-Crossings
plot(tzx{k1}, zeros(size(tzx{k1})), 'bp') % Plot Zero Crossings
end
hold off
end
xlabel('Time')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!