Index exceeds the number of array elements (2).

조회 수: 1 (최근 30일)
Journi Northorp
Journi Northorp 2021년 2월 15일
편집: James Tursa 2021년 2월 16일
function vibration_ode45(m,k,c,uG,t,u0)
% m = mass
% k = spring constant
% c = damper
% uG = force of gravity
% t = time
% u0 is the initial conditions
[T,Y] = ode45(@vibration,[0,t],u0);
function[dydt] = vibration(t,y)
m = 30000;
k = 10800;
c = 200000;
uG = sin(10*t);
% initialize
dydt = zeros(2,1);
% set of 1st order ODEs
dydt(1) = y(2);
dydt(2) = (2*k*uG-4*c*y(2)-2*c(y(2)-y(4))-2*k*y(1)-k*(y(1)-y(3)))/(2*m);
end
figure(1)
plot(T,Y(:,1),'b-',T,Y(:,2),'g-.')
xlabel('time')
ylabel('\theta')
figure(2)
plot(Y(:,1),Y(:,2),'-')
xlabel('\theta')
ylabel('d\theta/dt')
end

답변 (1개)

James Tursa
James Tursa 2021년 2월 15일
This line has y(3)
dydt(2) = (2*k*uG-4*c*y(2)-2*c(y(2)-y(4))-2*k*y(1)-k*(y(1)-y(3)))/(2*m);
What is the differential equation you are solving? What is the initial condition you are feeding ode45?
  댓글 수: 3
Journi Northorp
Journi Northorp 2021년 2월 15일
how would I increase the number of array elements to fit this?
James Tursa
James Tursa 2021년 2월 16일
편집: James Tursa 2021년 2월 16일
Yes, of course I can see that u0 is your initial condition, but you don't show us what u0 is. If it is a 2-element vector then y(3) will give you an indexing error. And you still haven't shown us the differential equation you are trying to solve.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by