ODE plot with 2nd order diff eqs

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

Torsten
Torsten 2015년 7월 14일
Your equation to be solved and your initial conditions are not consistent with project2.pdf.
Check again.
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2015년 7월 14일

0 개 추천

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')

카테고리

질문:

Sam
2015년 7월 14일

답변:

2015년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by