필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 12일
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
Matt Egan
Matt Egan 2020년 4월 12일
Ameer my man thats done the trick! Really can't thnk you enough for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by