Info

This question is closed. Reopen it to edit or answer.

Storing values in a matrix

1 view (last 30 days)
Anna Zito
Anna Zito on 14 Oct 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
I am using the ode45 function to solve a linearized and nonlinearized vibration equation. I need to plot both of them on the same graph. The only difference between the linearized and nonlinearized is a sin function.
In order to plot the two diagrams, my professor told me that I need to first save the solved differential equations in my main file, saved in two different matricies, say x and y. And then plot them using plot(t, x, t, y).
I do not know how to save them in matricies in order to make this work. I also only have the first ODE shown because I do not know how to incorporate a second one into the mix. Any help you could give would be greatly appreciated!!!
(The second ODE has the following form: [theta(2);
-(wn^2)*(theta(1))];
function thetadot = Vib(t,theta)
m=15;
l=2;
g=9.81;
d=l/2;
Io=(1/3)*m*(l^2);
wn=sqrt((m*g*d)/Io);
thetadot = [theta(2);
-(wn^2)*sin(theta(1))];
end
------------------------------------------------------------------
m=15;
l=2;
g=9.81;
d=l/2;
Io=(1/3)*m*(l^2);
wn=sqrt((m*g*d)/Io);
theta0=5;
thetadot0=0;
t=[0:0.01:10];
[t,theta]=ode45(@Vib,t,[theta0 thetadot0]);
figure (1);
plot(t,theta(:,1));

Answers (1)

Stephan
Stephan on 15 Oct 2020
% Constants
m=15;
l=2;
g=9.81;
d=l/2;
Io=(1/3)*m*(l^2);
wn=sqrt((m*g*d)/Io);
% function handle for linearized ode
Vib_lin = @(~,theta,wn) [theta(2); -(wn^2).*(theta(1))];
% function handle for nonlinearized ode
Vib_nonlin = @(~,theta,wn) [theta(2); -(wn^2).*sin(theta(1))];
% input data for ode45
theta0=5;
thetadot0=0;
t=[0 10];
% solve linearized part
[t_lin,theta_lin]=ode45(@(~,theta)Vib_lin(t,theta,wn),t,[theta0 thetadot0]);
% solve nonlinearized part
[t_nonlin,theta_nonlin]=ode45(@(~,theta)Vib_nonlin(t,theta,wn),t,[theta0 thetadot0]);
% plot results
figure (1);
plot(t_lin,theta_lin(:,1),t_nonlin,theta_nonlin(:,1));

Community Treasure Hunt

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

Start Hunting!