assigning in system of differential equations - maple

when i solve numerically a system of two differential equations:
s1:=diff(n[Di](t), t)=...;
s2:=diff(n[T](t), t)=...;
ics:={...}; #initial condition.
sys := {s1, s2, ics}:
sol:=dsolve(sys,numeric);
with respect to "t",then the solution (for example)for "t=4" is of the form, sol(4):
[t=4, n1(t)=const1, n2(t)=const2].
now, how is possible to use values of n1(t) and n2(t) for all "t"'s in another equation, namely "p", which involved n1(t) or n2(t)(like: {p=a+n1(t)*n2(t)+f(t)},where "a" and "f(t)" are defined), and to plot "p" for an interval of "t"?

Perhaps an example will help.
s1:=diff(n1(t), t)=sin(t);
s2:=diff(n2(t), t)=cos(t);
ics:={n1(0)=1,n2(0)=0}:
sys := {s1, s2} union ics:
sol:=dsolve(sys,numeric,output=listprocedure);
N1:=eval(n1(t),sol);
N2:=eval(n2(t),sol);
N1(0);
N1(1.1);
N2(0);
N2(1.1);
p := 11.3 + N1*N2 + tan:
plot(p, 0..1.1);
You might look at the DEplot routine, which is specialized for plotting ode solutions.

Related

How to obtain and work with steady-state part of DE solution?

I'm trying to obtain resonance curves of the system. System can be described as
F,m,k:=2,1,4:
lambda:= beta/(2*m):
omega:=sqrt(k/m):
de:=diff(x(t),t$2)+2*lambda*diff(x(t),t)+omega^2*x(t)=F*cos(gamma1*t):
cond:=x(0)=0, D(x)(0)=0:
sol := dsolve({cond, de});
Solving gives sum of terms, some of which "die out" with time (since these terms have exp(-...*t)) and some of which form steady-state solution (solution for t -> ∞). This solution will be in form xstst=f(gamma1)*sin(...). In order to obtain resonance curves, I need to plot f(gamma1) (for chosen constant betas, say, 2,1,0.5,0.25,etc.).
I've solved this "by hand" and found f := F/(sqrt((-gamma1^2+omega^2)^2+4*lambda^2*gamma1^2)). Plotting this for any chosen beta gives the result needed, for example, for beta:=0.5 the plot is
I wonder if I can obtain these curves using maple functions only (without solving anything "by hand" at all).
[edited]
It does not make sense to expect Maple to represent the result in terms of either sin(theta) or cos(theta), using some formulas for those terms that appear nowhere in the problem specification and are entirely introduced by you.
The following is obtained using a radical (square root) in each of cond1 and cond2.
restart;
de := diff(x(t),t,t)+2*lambda*diff(x(t),t)+omega^2*x(t)=F*cos(gamma1*t):
cond := x(0)=0, D(x)(0)=0:
sol := dsolve({cond, de}):
E,T := selectremove(hastype,rhs(sol),specfunc(anything,exp)):
lprint(T);
F*(2*sin(gamma1*t)*gamma1*lambda-cos(gamma1*t)*gamma1^2
+cos(gamma1*t)*omega^2)/(gamma1^4+4*gamma1^2*lambda^2-
2*gamma1^2*omega^2+omega^4)
cond1 := cos(theta) = (-gamma1^2+omega^2)
/((-gamma1^2+omega^2)^2+4*lambda^2*gamma1^2)^(1/2):
cond2 := sin(theta) = (-2*lambda*gamma1)
/((-gamma1^2+omega^2)^2+4*lambda^2*gamma1^2)^(1/2):
frontend(algsubs, [numer(rhs(cond1))=lhs(cond1)*denom(rhs(cond1)),
numer(T)],
[{`+`,`*`,`=`},{}]):
frontend(algsubs, [numer(rhs(cond2))=lhs(cond2)*denom(rhs(cond2)),
%],
[{`+`,`*`,`=`},{}]):
ans := collect(combine(%, trig),cos)/denom(T):
lprint(ans);
F*cos(gamma1*t+theta)/(gamma1^4+4*gamma1^2*lambda^2-
2*gamma1^2*omega^2+omega^4)^(1/2)
subsans := eval(eval(ans,[lambda=beta/(2*m),omega=sqrt(k/m)]),
[F=2,m=1,k=4]):
lprint(subsans);
2*cos(gamma1*t+theta)
/(beta^2*gamma1^2+gamma1^4-8*gamma1^2+16)^(1/2)
I'm not sure I fully understand your question, but when I run your code I get some terms with exponentials, some with sines and some with cosines. You can grab the coefficient of the sine terms with
coeff( collect( rhs( sol ) , sin( gamma1 * t ) ) , sin( gamma1 * t ) , 1 )

Solving 2nd order ODE, Matlab- the acceleration in the equation needs its own value in order to include another different term

I have this 2nd order ODE to solve in Matlab:
(a + f(t))·(dx/dt)·(d²x/dt²) + g(t) + ((h(t) + i(t)·(d²x/dt² > b·(c-x)))·(dx/dt) + j(t))·(dx/dt)² + k(t)·(t > d) = 0
where
a,b,c,d are known constants
f(t),g(t),h(t),i(t),j(t),k(t) are known functions dependent on t
x is the position
dx/dt is the velocity
d²x/dt² is the acceleration
and notice the two conditions that
i(t) is introduced in the equation if (d²x/dt² > b·(c-x))
k(t) is introduced in the equation if (t > d)
So, the problem could be solved with a similar structure in Matlab as this example:
[T,Y] = ode45(#(t,y) [y(2); 'the expression of the acceleration'], tspan, [x0 v0]);
where
T is the time vector, Y is the vector of position (column 1 as y(1)) and velocity (column 2 as y(2)).
ode45 is the ODE solver, but another one could be used.
tspan,x0,v0 are known.
the expression of the acceleration means an expression for d²x/dt², but here comes the problem, since it is inside the condition for i(t) and 'outside' at the same time multiplying (a + f(t))·(dx/dt). So, the acceleration cannot be written in matlab as d²x/dt² = something
Some issues that could help:
once the condition (d²x/dt² > b·(c-x)) and/or (t > d) is satisfied, the respective term i(t) and/or k(t) will be introduced until the end of the determined time in tspan.
for the condition (d²x/dt² > b·(c-x)), the term d²x/dt² could be written as the difference of velocities, like y(2) - y(2)', if y(2)' is the velocity of the previous instant, divided by the step-time defined in tspan. But I do not know how to access the previous value of the velocity during the solving of the ODE
Thank you in advanced !
First of all, you should reduce your problem to a first-order differential equation, by substituting dx/dt with a dynamical variable for the velocity.
This is something you have to do anyway for solving the ODE and this way you do not need to access the previous values of the velocity.
As for realising your conditions, just modify the function you pass to ode45 to account for this.
For this purpose you can use that d²x/dt² is in the right-hand side of your ODE.
Keep in mind though that ODE solvers do not like discontinuities, so you may want to smoothen the step or just restart the solver with a different function, once the condition is met (credit to Steve).
The second conditional term k(t)*(t>d) should be simple enough to implement, so I'll pass over that.
I would split up your equation into two part:
F1(t,x,x',x'') := (a+f(t))x'x'' + g(t) + (h(t)x'+j(t))x'' + k(t)(t>d),
F2(t,x,x',x'') := F1(t,x,x',x'') + i(t)x'x'',
where prime denote time derivatives. As suggested in this other answer
[...] or just restart the solver with a different function
you could solve the ODE F1 for t \in [t0, t1] =: tspan. Next, you'd find the first time tstar where x''> b(c-x) and the values x(tstar) and x'(tstar), and solve F2 for t \in [tstar,t1] with x(tstar), x'(tstar) as starting conditions.
Having said all that, the proper implementation of this should be using events, as suggested in LutzL's comment.
So, I should use something that looks like this:
function [value,isterminal,direction] = ODE_events(t,y,b,c)
value = d²x/dt² - b*(c-y(1)); % detect (d²x/dt² > b·(c-x)), HOW DO I WRITE d²x/dt² HERE?
isterminal = 0; % continue integration
direction = 0; % zero can be approached in either direction
and then include in the file (.m), where my ode is, this:
refine = 4; % I do not get exactly how this number would affect the results
options = odeset('Events',#ODE_events,'OutputFcn',#odeplot,'OutputSel',1, 'Refine',refine);
[T,Y] = ode45(#(t,y) [y(2); ( 1/(a + f(t))*(y(2)))*( - g(t) - ((h(t) + i(t))·(y(2)) - j(t)·(y(2))² - k(t)*(t > d)) ], tspan, [x0 v0], options);
How do I handle i(t)? because i(t)*(d²x/dt² > b*(c-y(1))))*(y(2)) has to be included somehow.
Thank you again

Solving system of second order ODEs in MATLAB

I have the system of second order ODEs:
A*u(t) + B*u''(t) = q(t) + b_A + b_B.
Here, A and B are known matrices, b_A is a known vector, b_B is a known vector, and q(t) is a time dependent vector which I can compute for a given t-value.
The goal of my problem is to numerically approximate the functions u_1 , ... , u_n, which are entries in u(t). Also, u''(t) denotes the second time derivative of u(t). I also have the initial condition vector:
u0 = zeros(n,1).
How would I solve this problem using MATLABs built in ode solvers (ode45)?
All of the examples I have seen thus far involve converting the system of second order ODEs into a system of first order ODEs, but they have all been very small examples. Thanks for the help.
To convert this into a system of first order ODEs, I would do
y_1 = u
y_2 = u', so :
y_1' = y_2, and
y_2' = A^(-1)*(q(t) + b_A + b_B - A*y_1).
How should I implement this in MATLAB?

Solving a nonlinear ODE 2. Order in Matlab numerically

I have a nonlinear-ODE of the second order with trigonometric functions such that I cannot formulate it depending of the second derivation. For example:
ay'' + b arctan(y'') + cy' + dy=0
y'(0)=0, y''(0)=0
Without existence of a term like arctan(y'') I could write my ode function like
function output=myodefunc(u,t){
y(1)=u(2);
y(2)=(-c*u(2)-d*u(1))/m;
output=y';
}
Unfortunately the nonlinear term of the second order (=> b*arctan(y'') ) makes me unable to write the ode in dependence of y'' .
Is there any way to solve such a trigonometric ode numerically in Matlab?
One can evaluate the y'' with a nonlinear solver (fsolve), within the ode function:
function output=myodefunc(u,t){
y(1)=u(2);
x0=0;
x=fsolve('a*x + b*atan(x) + c*u(2) + d*u(1)',x0);
y(2)=x;
output=y';
}

difference equations in MATLAB - why the need to switch signs?

Perhaps this is more of a math question than a MATLAB one, not really sure. I'm using MATLAB to compute an economic model - the New Hybrid ISLM model - and there's a confusing step where the author switches the sign of the solution.
First, the author declares symbolic variables and sets up a system of difference equations. Note that the suffixes "a" and "2t" both mean "time t+1", "2a" means "time t+2" and "t" means "time t":
%% --------------------------[2] MODEL proc-----------------------------%%
% Define endogenous vars ('a' denotes t+1 values)
syms y2a pi2a ya pia va y2t pi2t yt pit vt ;
% Monetary policy rule
ia = q1*ya+q2*pia;
% ia = q1*(ya-yt)+q2*pia; %%option speed limit policy
% Model equations
IS = rho*y2a+(1-rho)*yt-sigma*(ia-pi2a)-ya;
AS = beta*pi2a+(1-beta)*pit+alpha*ya-pia+va;
dum1 = ya-y2t;
dum2 = pia-pi2t;
MPs = phi*vt-va;
optcon = [IS ; AS ; dum1 ; dum2; MPs];
Edit: The equations that are going into the matrix, as they would appear in a textbook are as follows (curly braces indicate time period values, greek letters are parameters):
First equation:
y{t+1} = rho*y{t+2} + (1-rho)*y{t} - sigma*(i{t+1}-pi{t+2})
Second equation:
pi{t+1} = beta*pi{t+2} + (1-beta)*pi{t} + alpha*y{t+1} + v{t+1}
Third and fourth are dummies:
y{t+1} = y{t+1}
pi{t+1} = pi{t+1}
Fifth is simple:
v{t+1} = phi*v{t}
Moving on, the author computes the matrix A:
%% ------------------ [3] Linearization proc ------------------------%%
% Differentiation
xx = [y2a pi2a ya pia va y2t pi2t yt pit vt] ; % define vars
jopt = jacobian(optcon,xx);
% Define Linear Coefficients
coef = eval(jopt);
B = [ -coef(:,1:5) ] ;
C = [ coef(:,6:10) ] ;
% B[c(t+1) l(t+1) k(t+1) z(t+1)] = C[c(t) l(t) k(t) z(t)]
A = inv(C)*B ; %(Linearized reduced form )
As far as I understand, this A is the solution to the system. It's the matrix that turns time t+1 and t+2 variables into t and t+1 variables (it's a forward-looking model). My question is essentially why is it necessary to reverse the signs of all the partial derivatives in B in order to get this solution? I'm talking about this step:
B = [ -coef(:,1:5) ] ;
Reversing the sign here obviously reverses the sign of every component of A, but I don't have a clear understanding of why it's necessary. My apologies if the question is unclear or if this isn't the best place to ask.
I think the key is that the model is forward-looking, so the slopes (the partial derivatives) need to be reversed to go backward in time. One way to think of it is to say that the jacobian() function always calculates derivatives in the forward-time direction.
You've got an output vector of states called optcon = [IS;AS;dum1;dum2;MPs], and two vectors of input states [y2 pi2 y pi v]. The input vector at time t+1 is [y2a pi2a ya pia va], and the input vector at time t is [y2t pi2t yt pit vt]. These two are concatenated into a single vector for the call to jacobian(), then separated after. The same thing could have been done in two calls. The first 5 columns of the output of jacobian() are the partial derivatives of optcon with respect to the input vector at time t+1, and the second 5 columns are with respect to the input vector at time t.
In order to get the reduced form, you need to come up with two equations for optcon at time t+1. The second half of coef is just what is needed. But the first half of coef is the equation for optcon at time t+2. The trick is to reverse the signs of the partial derivatives to get linearized coefficients that take the input vector at t+1 to the output optcon at t+1.