MATLAB Answers

# Unexpected output when trying to plot the solution to a system of nonlinear differential equations

1 view (last 30 days)
Aleem Andrew on 3 Apr 2020
Commented: Aleem Andrew on 4 Apr 2020
The following Matlab code is meant to plot x(1) vs time and x(2) vs time separately, but only plots x(1) vs time. Despite there being two lines of code to generate each plot, one line is ignored. Does anyone have any suggestions as to how the code can be modified?
t = [0 10]; %time interval ranges from 0 to 10 s
ic = [pi/3; 0; 0; 0]; % initial conditions = [θ θ' x x']
[t,x] = ode45(ode, t, ic);
plot(t,x(:,1), 'o') %the y axis represents
plot( t,x(:,2),'o') %the y axis represents
xlabel('x(m)');
ylabel('θ(rad)');
function dydx = fun(t,x)
%[x(1) x(2) x(3) x(4)] = [angle angular velocity x position x velocity] respectively
L = 0.5; g = 9.81;
dydx = zeros(4,1);
dydx(1) = x(2);
dydx(2) = (4*sin(x(1))*(5*L*cos(x(1))*x(2)^2 + 6*g))/(L*(20*cos(x(1))^2 - 23));
dydx(3) = x(4);
dydx(4) = -(5*sin(x(1))*(23*L*x(2)^2 + 24*g*cos(x(1))))/(6*(20*cos(x(1))^2 - 23));
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Torsten on 4 Apr 2020
Use
hold on
between the two plot commands.
##### 2 CommentsShowHide 1 older comment
Aleem Andrew on 4 Apr 2020
Thank you for your responses

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!