# must return a column vector

1 view (last 30 days)
hisamuddin on 16 Nov 2022
Commented: Star Strider on 16 Nov 2022
function ydot = assignment(t,y)
global l1 l2 F0 omega g
% Mass matrix
theta2dot=[1.8 (0.8.*l1)/l2;0.8 (0.8.*l1)/l2];
% Stiffness matrix
theta=[(1.8.*g)/l1;(0.8.*g)/l2];
% Calculate the coefficient matrix
MDK = theta2dot./theta;
A = [0 1 0 ;
0 0 1 ;
MDK(1,1) MDK(1,2) 0 ;
MDK(2,1) MDK(2,2) 0] ;
% Force vector
FVecor = [0; 0; F0.*sin(omega.*t);0];
% Output
ydot= A.*y-FVecor;0;
end

Star Strider on 16 Nov 2022
I am not certain what ‘ydot’ is supposed to be, however the element-wise multiplication is likely the problem.
syms l1 l2 F0 omega g t y
y = sym('y',[3,1])
y = % global l1 l2 F0 omega g
% Mass matrix
theta2dot=[1.8 (0.8.*l1)/l2;0.8 (0.8.*l1)/l2];
% Stiffness matrix
theta=[(1.8.*g)/l1;(0.8.*g)/l2];
% Calculate the coefficient matrix
MDK = theta2dot./theta;
A = [0 1 0 ;
0 0 1 ;
MDK(1,1) MDK(1,2) 0 ;
MDK(2,1) MDK(2,2) 0] ;
% Force vector
FVecor = [0; 0; F0.*sin(omega.*t);0];
% Output
A*y-FVecor
ans = ydot= A.*y-FVecor;0;
Error using .*
Array sizes must match.
.
Star Strider on 16 Nov 2022
Something is wrong with the code.
The ‘A’ matrix should be (4x4), however it is (4x3).
I am not certain what you are doing, so I can only point this out. You need to fix it.
%initialisation
% global l1 l2 F0 omega g
l1=0.3;
l2=0.2;
F0=10
F0 = 10
omega=2
omega = 2
g=9.81
g = 9.8100
%set up the matrices
theta2dot=[1.8 (0.8*l1)/l2;0.8 (0.8*l1)/l2];
theta=[(1.8*g)/l1;(0.8*g)/l2];
%pre divide. this the blacklash operator
A=theta2dot.\theta;
%calculate the eigenvalues and eigenvectors of matrix A
[V, D]=eig(A)
V = 2×2
0.7071 -0.7071 0.7071 0.7071
D = 2×2
81.7500 0 0 -16.3500
clear
clc
% Assign parameters
% global l1 l2 F0 omega g
l1=0.3;
l2=0.2;
F0=10
F0 = 10
omega=2
omega = 2
g=9.81
g = 9.8100
% Initial Conditions
% global x_0 theta1_0 theta2_0 xdot_0
x_0=0.15;
theta1_0=0.1*pi;
theta2_0=pi/4
theta2_0 = 0.7854
xdot_0=0;
theta1dot_0=0;
theta2dot_0=0;
Y0=[x_0;theta1_0;theta2_0;xdot_0];
% Solve system of first order differential equations
% [t,y]=ode45('@assignment',[0 100],Y0 );
[t,y] = ode45(@(t,y)assignment(t, y, l1, l2, F0, omega, g),[0 100],Y0)
y = 4×1
0.1500 0.3142 0.7854 0
A = 4×3
0 1.0000 0 0 0 1.0000 0.0306 0.0204 0 0.0204 0.0306 0
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix
individually, use TIMES (.*) for elementwise multiplication.

Error in solution>assignment (line 71)
ydot=A*y-FVecor

Error in solution (line 35)
[t,y] = ode45(@(t,y)assignment(t, y, l1, l2, F0, omega, g),[0 100],Y0)

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
% Visualize
figure(1)
% Linear Displacement
plot(t,y(:,1))
xlabel('Time [s]')
ylabel('Displacement [m]')
figure(2)
% Angular Displacement
% Visualize
plot(t,y(:,2))
xlabel('Time [s]')
ylabel('Angular Displacement [m]')
function ydot = assignment(t, y, l1, l2, F0, omega, g)
y
% function ydot = assignment(t,y)
% global l1 l2 F0 omega g
% Mass matrix
theta2dot=[1.8 (0.8.*l1)/l2;0.8 (0.8.*l1)/l2];
% Stiffness matrix
theta=[(1.8.*g)/l1;(0.8.*g)/l2];
% Calculate the coefficient matrix
MDK = theta2dot./theta;
A = [0 1 0 ;
0 0 1 ;
MDK(1,1) MDK(1,2) 0 ;
MDK(2,1) MDK(2,2) 0]
% Force vector
FVecor = [0; 0; F0.*sin(omega.*t);0];
% Output
ydot=A*y-FVecor
% ydot=A.*y-FVecor;0;
end
.