0

I have a nonlinear dynamical system. I have found the equations of motion, which results in the following state vector: enter image description here

The variables I used are

enter image description here

I keep getting all kinds of errors. What am I doing wrong. Thank you!

function f = fun_1(t,x)
m0 = 0.25; m1 = 0.1; m2= 0.08;
L1 = 0.25; L2 = 0.2;
g = 9.81;
F = 0;
f=zeros(6,1);
f(1) =x(2);
f(2) =(-(L1*cos(x(3))*(m1+2*m2))/(2*(m0+m1+m2)))*f(4)+(-(m2*L2*cos(x(5)))/(2*(m0+m1+m2)))*f(6)+((L1*sin(x(3))*(m1+2*m2))/(2*(m0+m1+m2)))*x(4)^2+((m2*L2*sin(x(5)))/(2*(m0+m1+m2)))*x(6)^2+(2*F/(2*(m0+m1+m2)));
f(3) =x(4);
f(4) =(-(6*L1*cos(x(3))*(m1+2*m2))/(4*L1^2*(m1+3*m2)))*f(2)+(-(6*m2*L1*L2*cos(x(3)-x(5)))/(4*L1^2*(m1+3*m2)))*f(6)+(-(6*m2*L1*L2*sin(x(3)-x(5)))/(4*L1^2*(m1+3*m2)))*x(6)^2+((6*g*L1*(m1+2*m2)*sin(x(-(6*m2*L1*L2*sin(x(3)-x(5)))/3)))/(4*L1^2*(m1+3*m2)));
f(5) =x(6);
f(6) =(-(6*m2*L2*cos(x(5)))/(4*m2*L2^2))*f(2)+(-(6*m2*L1*L2*cos(x(3)-x(5)))/(4*m2*L2^2))*f(4)+(-(6*m2*L1*L2*sin(x(3)-x(5)))/(4*m2*L2^2))*x(4)^2+((6*m2*g*L2*sin(x(5)))/(4*m2*L2^2));
clc
clear all
close all
tspan = [0 10];
x0=[0;0;0;0;0;0];
[t, x] = ode23('fun_1', tspan, x0);
plot(t,x(:,3))
xlabel('t')
ylabel('theta(t)')
Missael
  • 15
  • 4
  • 1
    What error are you getting? Try to be a little more specific. – juju89 Oct 23 '20 at 01:43
  • Array indices must be positive integers or logical values. Error in fun_1 (line 11) f(4) =(-(6*L1*cos(x(3))*(m1+2*m2))/(4*L1^2*(m1+3*m2)))*f(2)+(-(6*m2*L1*L2*cos(x(3)-x(5)))/(4*L1^2*(m1+3*m2)))*f(6)+(-(6*m2*L1*L2*sin(x(3)-x(5)))/(4*L1^2*(m1+3*m2)))*x(6)^2+((6*g*L1*(m1+2*m2)*sin(x(-(6*m2*L1*L2*sin(x(3)-x(5)))/3)))/(4*L1^2*(m1+3*m2))); – Missael Oct 23 '20 at 02:15
  • That means in that line 11 you are using an index into an array that is not a positive integer or logical. I think it's here: `sin(x(-(6*m2*L1*L2*sin(x(3)-x(5)))/3)))` because I don't think `-(6*m2*L1*L2*sin(x(3)-x(5)))/3))` refers to an element of `x`. – David Oct 23 '20 at 05:27
  • Also, in the definition of `f(2)`, for example, you are using the value of `f(4)`, but `f(4)` is zero and you haven't defined it yet. `f` should be your derivatives, and they should be written in terms of the state variables `x`. – David Oct 23 '20 at 05:28
  • Apparantly your problem has the form `M*u'' = f(t,u,u')`. You need to solve explicitly or per some function call the linear system for `u''` so that no second derivative remains on the right side. – Lutz Lehmann Oct 23 '20 at 16:30

0 Answers0