Can anyone tell me how to implement these timevarying state space equations in matlab

2 visualizzazioni (ultimi 30 giorni)

Risposta accettata

Ced
Ced il 22 Ott 2014
I would use one of the ode functions, e.g. ode45. You can then define an arbitrarily complex, nonlinear, time-varying, etc. system.
In your case, something like that (parameters are rubbish, but you have those)
function [Tout, Xout] = run_ode
% Some Parameters
Rm = 1000;
Ra = 1000;
% Time-varying parts:
C = @(t)(sqrt(t+1)^3);
dC = @(t)(3*sqrt(t+1)/2);
r = @(x)x*(-x<0); % Ramp function
p = @(x)[r(x(2)-x(1))/Rm ; r(x(1)-x(4))/Ra];
% Set up simulation
dx = @(t,x)ode_sys(t,x,C(t),dC(t),p(x));
Ts = 1e-12;
t_end = 1e-10;
t_sim = 0:Ts:t_end;
x0 = randn(5,1);
[ Tout, Xout ] = ode45(dx,t_sim,x0);
end
% Define system equations
function dy = ode_sys(t,x,C,dC,p)
% Some Parameters
Rs = 100;
Cr = 1e-6;
Cs = 2*Cr;
Ca = 0.1*Cr;
Ls = 6e-9;
Rc = 1e6;
b1 = 1/(Rs*Cr);
b2 = 1/(Rs*Cs);
% Defined time-varying system matrices elementwise
Ac = [ -dC/C 0 0 0 0 ;
0 -b1 b1 0 0 ;
0 b2 -b2 0 1/Cs;
0 0 0 0 -1/Ca;
0 0 -1/Ls 1/Ls -Rc/Ls ];
Pc = [ 1/C -1/C; -1/Cr 0; 0 0 ; 0 1/Ca ; 0 0 ];
dy = Ac*x + Pc*p;
end
Cheers
  8 Commenti
Ced
Ced il 22 Ott 2014
no, you don't need to give the range of tn for ode45. You just define E the same way you do for C, dC and the rest. When you then call e.g. E(3), then the function E(t) will be evaluated at t = 3;
The integration range is then passed to ode45. In my first answer, I had used
Ts = 1e-12;
t_end = 1e-10;
t_sim = 0:Ts:t_end;
x0 = randn(5,1);
[ Tout, Xout ] = ode45(dx,t_sim,x0);
t_sim is only passed to ode45, and does not appear anywhere else. Instead of defining t_sim as a time vector with all time steps, I could also only pass [ t_start t_end ], and then ode45 output it's own time steps. The reason I prefer passing t_sim as "full vector" is that it makes things easier to plot later, but that is a personal preference.
For the derivative: Yes, calculate the derivative by hand, and then hardcode the function. There are ways to compute derivatives symbolically using the symbolic math toolbox, but that would just make things unnecessarily complicated here. I think this derivative is still ok. If you want to check that your derivative is correct, you can always use some calculator or mathematica to check your answer. Also, note that "diff" does not compute derivatives as such, but only takes the difference between the elements in your vector, e.g. diff([ 1 2 3]) would return [ 1 1 ].

Accedi per commentare.

Più risposte (6)

Bilal
Bilal il 22 Ott 2014
yes you are right but the thing is E(t) is the re scaled version of En(tn) so it would definitely effect E(t) if i don't define tn anywhere if you run my code, you will see i m getting the write results as given in this paper. check the attachment
And yes if i accept the answer can we still continue communicating here. ?
  26 Commenti
Ced
Ced il 28 Ott 2014
I'm sorry, I don't understand your question(s). Why don't you just plot it? That way, you'll see how many cycles appear? So... just to e.g.
plot(Tout,Xout(3,:))
And if it's not enough/too many cycles, adapt your end time accordingly (or simply only plot a particular section of the results).
Bilal
Bilal il 29 Ott 2014
Modificato: Bilal il 29 Ott 2014
i have plotted them but i am getting only one cycle let me show u

Accedi per commentare.


Bilal
Bilal il 29 Ott 2014
<<
>>
  1 Commento
Bilal
Bilal il 29 Ott 2014
Modificato: Bilal il 29 Ott 2014
If u see there the time range is okay i.e 2secs in Xout plots the problem is that output doesn't repeat itself after 0.4 secs

Accedi per commentare.


Bilal
Bilal il 29 Ott 2014
  4 Commenti
Bilal
Bilal il 29 Ott 2014
okay, i got it thanks alot.
Did you find anything why i am getting only one cycle in Xout and then output gradually goes to zero
Ced
Ced il 30 Ott 2014
No, I'm afraid I'm a bit caught up in work myself at the moment. I'll have a look at it if I find the time. I haven't read the paper in detail, but maybe they have something like mod(t,HR) or so? Maybe you can try to find out what function is not periodic, and if not, why.

Accedi per commentare.


Bilal
Bilal il 31 Ott 2014
This is what exactly i have done in the generation of C(t) i have use mod(t,HR) but do i need to implement it for the state space as well ? okay please get back to me when u have time. thanks

Ced
Ced il 31 Ott 2014
I mean, that would certainly work. simply replace every instance of "t" by "mod(t,60/HR)" when calling ode_sys, so
dx = @(t,x)ode_sys(mod(t,60/HR),x,C(mod(t,60/HR)), ...
The question is if this makes sense. That's something you could ask your professor or discuss with some phd student. Skimming the paper, I cannot find any reference that they artificially periodicized (that's a word now ^^) their solutions. But then again, little details are often omitted (or forgotten) when describing simulations in papers.
  12 Commenti
Bilal
Bilal il 7 Nov 2014
I am unable to plot derivative of C that you told me to generate using syms. its getting mpower error

Accedi per commentare.


Bilal
Bilal il 13 Nov 2014
Please reply me back as soon as you get free. Thanks

Community Treasure Hunt

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

Start Hunting!

Translated by