How do i resolve this problem ??????

1 view (last 30 days)
cha seung woo
cha seung woo on 11 Nov 2015
Edited: Torsten on 11 Nov 2015
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);

Accepted Answer

Torsten
Torsten on 11 Nov 2015
Edited: Torsten on 11 Nov 2015
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.
  3 Comments
Torsten
Torsten on 11 Nov 2015
Edited: Torsten on 11 Nov 2015
No.
It's better to restart ODE45 each time there is a discontinuity in the first derivative of v.
You could alternatively try
function main
tspan = [0 9];
[t,x]=ode45(@example,tspan,[0 9]);
function xdot=example(t,x)
if t<1
v = 10*t;
elseif t>=1 && t<4
v = 10;
elseif t>=4 && t<5
v = -10*(t-4)+10;
else
v = 0;
end
xdot=zeros(2,1);
xdot(1)=x(2);
xdot(2)=(v-4*x(1)-7*x(2))/5;
end
but my guess is that the solver will behave much worse than in my previous suggestion.
Best wishes
Torsten.
cha seung woo
cha seung woo on 11 Nov 2015
Edited: cha seung woo on 11 Nov 2015
thank you very much !!, i will ask you something later , too . good bye

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!