I added more elements to the code and also new conditions but this one worked for me.
function [xdot y] = HW5P3(t,x)
global a b B J K vo R Lo thetaout
theta=x(1);
h=x(2);
lambda=x(3);
v=(t<0.5).*(0)+((t>=0.5)&(t<4.5)).*(vo*sin(2*pi*(1/0.1)*t))+(t>=4.5).*(0);
L=(theta<-thetaout).*(Lo)+((theta>=-thetaout)&(theta<thetaout)).*(a+b.*abs(theta))+(theta>=thetaout).*(Lo);
Ldot=b*sign(theta);
lambdadot=v-R*(lambda/L);
hdot=((Ldot/(2*(L)^2))*lambda^2)-((B/J)*h)-K*theta;
thetadot=h/J;
xdot=[thetadot;hdot;lambdadot];
y=[L;v;Ldot];
end
global a b B J K vo R Lo thetaout
a=1;
b=-2/pi;
B=0.01;
J=0.001;
K=1;
Lo=0.1;
vo=120;
R=10;
thetaout=1.4137;
lambdai=0;
thetai=1.4137;
hi=0;
[t,x]=ode45('HW5P3',[0 5],[thetai,hi,lambdai]);
L=length(t);
N = length(t);
for i=1:N
[xd,y] = HW5P3(t(i),x(i,:));
Ln(i) = y(1);
vs(i) = y(2);
Lndot(i) = y(3);
end
theta=x(:,1);
h=x(:,2);
lambda=x(:,3);
thetadot=h/J;
is=lambda/L;
Trotspring=K*theta;
Tbearing=B*thetadot;
figure(1);
subplot(3,1,1)
plot(t,theta), xlabel('time[sec]'),ylabel('Angular displacement [rad]')
subplot(3,1,2)
plot(t,thetadot), xlabel('time[sec]'),ylabel('Angular velocity [rad/sec]')
subplot(3,1,3)
plot(t,Ln,t,theta), xlabel('time[sec]'),legend('Ln [H]','Angular displacement [rad]')
figure(2);
subplot(3,1,1)
plot(theta,Ln), xlabel('Angular displacement [rad]'),ylabel('L [H]')
subplot(3,1,2)
plot(t,theta,t,is*1000), xlabel('time[sec]'),legend('Angular displacement [rad]','Current [mA]')
subplot(3,1,3)
plot(t,Trotspring,t,Tbearing), xlabel('time[sec]'),legend('Spring Torque [N]','Bearings Tor
Best Answer