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

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

TO begin with, thank you very much . it's very helpful for me. but do u know another method by using matrix or extra . here is my second code .
function v=v1(time)
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
end
function xdot=example(t,x,time,v1)
xdot=zeros(2,1);
v1=interp1(time,v1,t);
xdot(1)=x(2);
xdot(2)=(v1-4*x(1)-7*x(2))/5;
end
////////////////////////////////////////
tspan=0:0.01:20;
time=0:0.01:20;
[t,x]=ode45(@example,tspan,[0 9],[],time,v1);
plot(t,x(:,1)'--')
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

Asked:

on 11 Nov 2015

Edited:

on 11 Nov 2015

Community Treasure Hunt

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

Start Hunting!