Clear Filters
Clear Filters

Having a system of three state I put the odes in a function and the script to solve the given function but now it is executing continuously, so tell me how to stop it?

1 view (last 30 days)
function xdot=func4(t,x)
A=[0 1 0;0 0 1;-1 -2 -3]
B=[0 0;0 1;1 0]
C1=[0 1]'
C2=[1 0 ;0 1]
C3=[1 0]'
s=[C1*x(1)+C2*[x(2);x(3)]+C3*x(1)^(3/4)]
u1=[1;0]*x(1)+[1 3;0 -1]*[x(2);x(3)]-3/5*[0 0;1 0]*[x(2);x(3)]*x(1)^-2/5-[0 0.1;0.1 0]*(s/norm(s))
u2=[1;0]*x(1)+[2 2.9;-0.1 -1]*[x(2);x(3)]
u=u1+u2
xdot=A*x+B*u
%and by using ode 45
clc; clear all close all;
tspan=[0 8]
x0=[0.15;-0.15;0.1]
[t,x]=ode45('func4',tspan,x0)
plot(t,x(:,1),t,x(:,2),t,x(:,3))
now it is not stopping, it runing from last hours continuously
please check the code and suggest what to do?

Accepted Answer

Sunil Ojwani
Sunil Ojwani on 11 Dec 2018
Capture45.JPG
sorry sir,
i tried the solution suggested by you but the result from your solution is not the same as required in this picture.Suggest me the solution to get the same result as in the picture.
  1 Comment
Stephan
Stephan on 11 Dec 2018
Hi,
i did not change the part of your code which has any influence on the solution - just made some optimization of the code. Due to this, i suspect that your matrices or your function contains an issue. Since I am not familiar with the details of your problem, i dont know how to help you.
Looking at the graph it would be enough to run the simulation with tspan = [0 4]. Do you get the result showed in the picture with your own code? I think you should check this and make the needed corrections - this apppears to be not a problem in matlab, but a problem of how you did the representation of your problem in the code.

Sign in to comment.

More Answers (2)

Stephan
Stephan on 11 Dec 2018
Edited: Stephan on 11 Dec 2018
Hi,
try:
calculate_system
function calculate_system
A=[0 1 0;0 0 1;-1 -2 -3];
B=[0 0;0 1;1 0];
C1=[0 1]';
C2=[1 0 ;0 1];
C3=[1 0]';
tspan=[0 6.5];
x0=[0.15, -0.15, 0.1];
[t,x]=ode23s(@func4,tspan,x0);
plot(t,x(:,1),t,x(:,2),t,x(:,3));
function xdot=func4(~,x)
s=C1*x(1)+C2*[x(2);x(3)]+C3*x(1)^(3/4);
u1=[1;0]*x(1)+[1 3;0 -1]*[x(2);x(3)]-3/5*[0 0;1 0]*[x(2);x(3)]*x(1)^-2/5-[0 0.1;0.1 0]*(s/norm(s));
u2=[1;0]*x(1)+[2 2.9;-0.1 -1]*[x(2);x(3)];
u=u1+u2;
xdot=A*x+B*u;
end
end
In general, there is nothing wrong with your code. Some operations are not needed to be calculated inside the function.
The main problem seems to be that tspan is quiet big and your problem appears to be stiff. Try tspan = [0 1] or [0 3] and see what happens with ode45. The values of x(3) increase to very big values very fast, so it will take a lot of iteration steps using a non stiff solver like ode45. Solvers like ode23s or ode15s work fast and without warning up to about tspan = [0 6.5]. For bigger values both solvers give warnings. Since your code is ok, you could also wait until ode45 has finished working, which could take a while.
Best regards
Stephan

Sunil Ojwani
Sunil Ojwani on 26 Dec 2018
now i can not plot control input u

Community Treasure Hunt

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

Start Hunting!