Out of bounds, don't know why and unit step function. Additionally what is unit-ramp function?

3 次查看(过去 30 天)
So I'm receiving the following error and I don't know why: "Attempted to access ap1(2); index out of bounds because numel(ap1)=1." I could swear that everything should work...
Outside of that, I'm supposed to find the unit-step response and unit-ramp response. Now I think I can achieve that with the heaviside function but there are a variety of ways. From my understanding a unit-step function basically changes input into binary, except for 0. So all positive numbers will be 1s, all negatives will be 0s, and any zeros will be 0.5.
In the problem I've been provided, I just have "u" provide to me to represent the unit-step and unit-ramp functions. However, I wasn't given a timespan. I've omitted it just to try to troubleshoot this error.
t1=linspace(0,10,100);
function dap1=P1(t1,ap1)
dap1=zeros(3,1);
dap1=[-5 -25 -5; 1 0 0; 0 1 0].*[ap1(1);ap1(2);ap1(3)]+[1;0;0] %*heaviside(t1)
end
[~,ap1]=ode45(@P1,t1,1);
y1=[0 25 5].*[ap1(1);ap1(2);ap1(3)
In past problems this was achieved via a for loop as shown below with zb and zb(i). To me that's strange because in the situation with the for loop I interpret this as assuming that the unit step function is always one.
%% DOWN HERE for the loop I'm referring to VVVV
function Pother
L=2; %variables
g=9.8; %variables
thet0=0.2; %variables
dthet0=0; %variables
tspan2=linspace(0,10,100); %time variation
zb=[10,.2]; %function b(t) where b(t)=10u(t) and 0.2u(t)
%d2thet/dt2=(-g*sin(thet)-b(t)*cos(thet))/L
L=2; %variables
g=9.8; %variables
thet0=0.2; %variables
dthet0=0; %variables
tspan2=linspace(0,10,100); %time variation
zb=[10,.2]; %function b(t)
function Tprimea=TA(t,za,zb,L) %Defining a matrix Tprimea under TA
Tprimea=(zeros(size(za)));
Tprimea(1)=za(2);
Tprimea(2)=(-g*sin(za(1))-zb*cos(za(1)))/L;
end
for i=1:2 %The i changes the function b(t): represented here by zb
[~,za]=ode45(@TA,tspan2,[thet0;dthet0],[],zb(i),L); %ode solver
subplot(1,2,i) %This changes the placement of each new graph
plot(tspan2,za(:,1)) %time vs theta
end
end
Additionally, what is unit-ramp function? I'm beginning to wrap my head around the step function but I am completely lost with the ramp.

回答(1 个)

Philip Caplan
Philip Caplan 2015-4-27
When you called "ode45", you were only passing in a scalar value for the initial condition. However, it looks like your "odefun" expects a 3x1 solution vector at each time step and returns a 3x1 "dydt" vector. I don't know the details of the ODEs you are solving but the modified code below executes without errors:
function testODE()
t1=linspace(0,10,100);
[~,ap1] = ode45(@P1,t1,ones(3,1));
y1 = [0 25 5]*[ap1(1);ap1(2);ap1(3)]
end
function dap1 = P1(t1,ap1)
dap1 = [-5 -25 -5; 1 0 0; 0 1 0]*[ap1(1);ap1(2);ap1(3)]+[1;0;0]; %*heaviside(t1)
end
Also note that the element-wise operator (represented by ".") was removed to make the dimensions agree. For more information, please see:
The following MATLAB Answer gives a good example of how to compute the ramp response of a system:

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by