Effacer les filtres
Effacer les filtres

Index in position 1 is invalid. Array indices must be positive integers or logical values. My Events

1 vue (au cours des 30 derniers jours)
I am currently working on an inverted pendulum problem. I have used the My event funtion to stop the penduum action at 45degree (please see code and function below). The highlighted line is causing problems. It is my understanding this is the strating conditions of the pendulum, but if i set to [0,0] it comes up with the above error. The current conditions are not correct but i am unable to change them and run the programme.
function zdot=crash(t,z,g,r,m,k,a)
zdot=zeros(2,1);
zdot(1)=z(2);
zdot(2)=(r^2*z(2)^2-r*a*cos(z(1))-r*g*sin(z(1)))/(k^2-2*r^2);
end
function [value,isterminal,direction] = dashboard(t,z)
minz = (pi/4); %for example
value = z(:,1) < minz;
isterminal = 1; %stops the integration
direction = 0; % The zero can be approached from either direction, -1 decreasing function, +1 increasing fucniton
end
clear;clc
%crash conditions
g=9.81; r=0.4; k=0.5; m=40; a=98.1;
tspan1=(0:0.002:5);
z0=[pi/2 0];
z0=[0, (a/r)^0.5]; <<<<<<<<<<<<<<<<<<<<----------------------------------------THIS LINE
Opt = odeset('Events', @dashboard);
[t,z]=ode45(@(t,z)crash(t,z,g,r,k,a,m),tspan1,z0,Opt);
ind = interp1(z(:,1),1:length(z(:,1)),pi/4,'nearest');
V_g=z(ind,2)*r
V_h=z(ind,2)*.75

Réponse acceptée

Ameer Hamza
Ameer Hamza le 12 Avr 2020
The ode solver does not cause the issue. When z0 = [0 0], the pendulum never reaches the angle of pi/4, and then you run the line
ind = interp1(z(:,1),1:length(z(:,1)),pi/4,'nearest');
and that outputs NaN because pi/4 is not in the range of elements in z(:,1). Then you try to index using NaN value
z(ind,2)*r
and MATLAB's gives an error. You need to consider how to specify the maximum angle (instead of pi/4) for a given initial condition.
  13 commentaires
Matt Egan
Matt Egan le 12 Avr 2020
Ameer my man thats done the trick! Really can't thnk you enough for your help.
Ameer Hamza
Ameer Hamza le 12 Avr 2020
Good to hear that it worked. I am glad to be of help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by