Non linear pendulum code
Show older comments
I've been trying to run this code, for a non linear pendulum, but i get the following errors:
for the function----
Not enough input arguments.
Error in pend_l (line 4)
xdot = [x(2); -wsq*x(1)-C*x(2)];
for the script:
Error using feval
Unrecognized function or variable 'pend_n'.
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0,
options, varargin);
Error in pend_solve (line 10)
[t2,y] = ode45('pend_n',tspan,y0);
THE CODE
function xdot = pend_l(t1,x)
wsq = 13.56;
C = 0.1;
xdot = [x(2); -wsq*x(1)-C*x(2)];
% clf;
tspan = 0:0.01:50;
x0 = [1 ; 0];
y0 = [1 ; 0];
[t1,x] = ode45('pend_l',tspan,x0);
[t2,y] = ode45('pend_n',tspan,y0);
X1 = x(:,1);
X2 = x(:,2);
Y1 = y(:,1);
Y2 = y(:,2);
a = 2400;
% n = numel(t1);
n = 8000;
% plot(t1(a:n),0.69*X1(a:n),'linewidth',3);
% hold on;
% grid on;
%
% plot(t2(a:n)-1.81,Y1(a:n),'r','linewidth',3);
%
% legend('Linear', 'Nonlinear')
% hold off;
%
% figure(2);
% clf; % energies are scaled to 10% of its actual values
L = 1*20e-2*20e-2*Y2.*Y2 - 1*9.81*20e-2*(1-cos(Y1)); %lagrangian
E = 1*20e-2*20e-2*Y2.*Y2 + 1*9.81*20e-2*(1-cos(Y1)); %Total energy
D = 0.1*Y2.*Y2; %dissipated energy
% plot(t2,L,'linewidth',3);
% hold on;
% grid on;
% plot(t2,E,'r','linewidth',3);
% plot(t2,D,'g','linewidth',3);
% plot(t2,E-D,'k','linewidth',3); %net energy
b = 1;
f = 2;
figure(3);
clf;
plot(t1,1*20e-2*20e-2*Y2.*Y2,'linewidth',3);
hold on;
plot(t1,1*9.81*20e-2.*(1-cos(Y1)),'r','linewidth',3);
plot(t1,E,'c','linewidth',3);
% plot(t1,X1-b,'linewidth',3);
hold on;
grid on;
plot(t2,Y1-b,'r','linewidth',3);
plot(t2,L+f,'linewidth',3);
plot(t2,D+f,'g','linewidth',3);
plot(t2,(E-D)+f,'k','linewidth',3); %net energy
plot(t1,Y1*0-b-0.3,'k','linewidth',1);
plot(t1,Y1*0-b+0.3,'k','linewidth',1);
plot(t1*0+24,3.5*Y1+1,'k','linewidth',1);
text(30,-b+0.4,'\downarrow Linear Range');
% text(12,0,'\leftarrow Time instant @ which non-linear to linear');
% text(12,-0.5,'transition takes place');
% % legend('Linear', 'Nonlinear')
legend('T','V','Total Energy','Linear', 'Nonlinear','Lagrangian','Dissipated Energy','Net Energy');
figure(4);
clf;
ED = E-D;
[pks,locs] = findpeaks(L);
[pks1,locs1] = findpeaks(ED);
[pks2,locs2] = findpeaks(D);
TF = islocalmin(L);
TF1 = islocalmin(ED);
TF2 = islocalmin(D);
plot(locs*0.01,pks,'b');
hold on;
plot(locs1*0.01,pks1,'r');
plot(locs2*0.01,pks2,'k');
grid on;
plot(t1(TF),L(TF),'b')
plot(locs1*0.01,pks1,'r');
plot(t1(TF1),ED(TF1),'r');
plot(t1(TF2),D(TF2),'k');
plot(t1*0+24,1*Y1+0,'k','linewidth',1);
legend('Lagrangian','Net Energy','Dissipated Energy');
------------------------------------------------------------------------------------------------------
Any idea why it isn't working? thanks guys
Accepted Answer
More Answers (1)
KLETECH MOTORSPORTS
on 4 Nov 2020
0 votes
10 Comments
KLETECH MOTORSPORTS
on 4 Nov 2020
KLETECH MOTORSPORTS
on 4 Nov 2020
KLETECH MOTORSPORTS
on 4 Nov 2020
pend_n could be removed if not needed, but then also remove all the code related to this function. So everything where y0, t2 and y appear (which are the results of pend_n) should be removed also.
Also both functions are written in a way that they are suitable for the ode45 solver. The solver calls them with the input arguments, so that they run without errors. What you do is calling a function that expects 2 inputs without an input, so it will throw this error above.
KLETECH MOTORSPORTS
on 4 Nov 2020
KLETECH MOTORSPORTS
on 4 Nov 2020
KLETECH MOTORSPORTS
on 4 Nov 2020
KLETECH MOTORSPORTS
on 15 Nov 2020
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

