must return a column vector

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

Answers (1)

I am not certain what ‘ydot’ is supposed to be, however the element-wise multiplication is likely the problem.
Use array multiplication instead.
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.
.

5 Comments

where am i supposed to put syms l1 l2 F0 omega g t y
y = sym('y',[3,1])
You do not put it anywhere.
I used the symbolic approach here because I do not have the other variables (or the rest of the code), and I wanted to demonstrate the correct calculation.
Also, do not use global variables! Instead, declare the function as:
function ydot = assignment(t, y, l1, l2, F0, omega, g)
and in your ode45 call (or whatever function you choose to use to integrate it), refer to it as:
@(t,y)assignment(t, y, l1, l2, F0, omega, g)
The other arguments will be previously defined in your workspace, and will be passed to it.
See Passing Extra Parameters for details.
.
.
okat let me send you the rest of the code here
%initialisation
global l1 l2 F0 omega g
l1=0.3;
l2=0.2;
F0=10
omega=2
g=9.81
%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)
clear
clc
% Assign parameters
global l1 l2 F0 omega g
l1=0.3;
l2=0.2;
F0=10
omega=2
g=9.81
% Initial Conditions
global x_0 theta1_0 theta2_0 xdot_0
x_0=0.15;
theta1_0=0.1*pi;
theta2_0=pi/4
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 );
% 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)
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
i also tried what you mentioned above @Star Strider , but it still gives an error
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
.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Asked:

on 16 Nov 2022

Commented:

on 16 Nov 2022

Community Treasure Hunt

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

Start Hunting!