MATLAB: How do i resolve this problem ?????

ode45

this is state space model of dynamical system. in this problem, v is input of system. i'd like to put a function ( i made a function v below ) on v what should i do?
function xdot=example(t,x,v)
xdot=zeros(2,1);
xdot(1)=x(2);
xdot(2)=(v-4*x(1)-7*x(2))/5;
end
//////////////////////////////////////////////////////
tspan=0:0.01:20;
k=0;
for time=0:0.01:20;
k=k+1;
if time<1
v(k)=10*time;
elseif time<=4
v(k)=10;
elseif time<=5
v(k)=-10*(time-4)+10;
else
v(k)=0;
end
end
[t,x]=ode45(@example,tspan,[0 9],[],v);

Best Answer

function main
tspan = [0 1];
[t1,x1]=ode45(@(t,x)example(t,x,1),tspan,[0 9]);
tspan = [1 4];
[t2,x2]=ode45(@(t,x)example(t,x,2),tspan,[x1(end,1) x1(end,2)]);
tspan = [4 5];
[t3,x3]=ode45(@(t,x)example(t,x,3),tspan,[x2(end,1) x2(end,2)]);
tspan = [5 20];
[t4,x4]=ode45(@(t,x)example(t,x,4),tspan,[x3(end,1) x3(end,2)]);
function xdot=example(t,x,flag)
if flag==1
v = 10*t;
elseif flag==2
v = 10;
elseif flag==3
v = -10*(t-4)+10;
elseif flag==4
v = 0;
end
xdot=zeros(2,1);
xdot(1)=x(2);
xdot(2)=(v-4*x(1)-7*x(2))/5;
end
Best wishes
Torsten.
Related Question