must return a column vector
Mostra commenti meno recenti
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
Risposte (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])
% 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
ydot= A.*y-FVecor;0;
.
5 Commenti
hisamuddin
il 16 Nov 2022
Star Strider
il 16 Nov 2022
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.
.
.
hisamuddin
il 16 Nov 2022
hisamuddin
il 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
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 );
[t,y] = ode45(@(t,y)assignment(t, y, l1, l2, F0, omega, g),[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, 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
.
Categorie
Scopri di più su Function Creation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
