PDEs in MATLAB - controlling constant boundary values throughout the process - matlab

I am trying to solve a system of partial differential equations (PDEs) for a 1D flow (along Z-axis) with several phases defined by different boundary conditions (their duration is set by the simulation time of the phase).
I have the following problem: at the beginning I define initial conditions (constant values) for t = 0, which I put in the resulting vector of unknown parameters. Next, I implement boundary conditions for the first phase for Z = 0 and Z = L and execute the ODE solver. The problem is that during time integration, the ODE solver overwrites the boundary values for Z = 0, which should remain constant.
After looking at the ODE solver in detail, it returns a non-zero values for the time derivatives of the variables for Z = 0, thus "changing" the boundary values. Arguably, the time derivatives calculation is not done correctly and returns non-zero values for the time derivatives on the Z = 0 boundary where the values are constant.
To "fix" this error, I have not found a better solution than to overwrite the time derivatives of the variables for Z = 0, after the ODE solver function has been computed, to zero values, thus achieving constant values at this boundary throughout all phases.
Principally, the algorithm should work by defining the time derivatives only once at the beginning and then changing only the boundary conditions, in my opinion.
Would you please have any recommendations on how to keep the boundary values constant in the ODE solver without overwriting the time derivatives within the run of the algorithm?

Related

Matlab optimization stopping criteria

I am trying to solve a system of 12 equations in Matlab. Because I have constraints on the minimum and maximum values of the variables I use lsqnonlin rather than fsolve. However, I would like the optimizer to stop once the output (sum of squared deviations from the point where each equation holds) is sufficiently close to zero. Is there a way to specify such a stopping criterion?
The standard stopping criteria are about comparing the change in the output value compared to previous iteration but this is less relevant for me.
Use the fmincon function to solve the equations with bounded constraints.
Because you have not provided anything, follow the example provided by MATLAB:
fun = #(x)1+x(1)/(1+x(2)) - 3*x(1)*x(2) + x(2)*(1+x(1)); % objective function
lb = [0,0]; % lower bounds
ub = [1,2]; % upper bounds
x0 = (lb + ub)/2; % initial estimate
x = fmincon(fun,x0,[],[],[],[],lb,ub)
This specifies the range 0<x(1)<1 and 0<x(2)<2 for the variables.
The fmincon function also lets you change the default options. To specify the tolerance for the output, set it:
options = optimoptions('fmincon','Display','iter','FunctionTolerance',1e-10);
This sets fmincon options to have iterative display, and to have a FunctionTolerance of 1e-10. Call the fmincon function with these nonstandard options:
x = fmincon(fun,x0,[],[],[],[],lb,ub,[],options)

Equation system with derivatives

I have to solve the following system:
X'(t) = -D(t)x(t)+μ(s(t), p(t))x(t);
S'(t) = D(t)(s(t)^in - s(t)) - Yxsμ(s(t), p(t))x(t)
p'(t) = -D(t)p(t)+(aμ(s(t), p(t))+b)x(t)
where
μ(s(t), p(t)) = Μmax ((1 - (p(t)/pm))s(t)) / (km+s(t)+(s(t) ^ 2)/ki)
where Yxs, a,b, Mmax, Pm, km, ki are constant variables, then I have to linearizate the system and find the balance Points of thiw system. any suggestion how to do it with Matlab or Mathematica??
Matlab can help with some steps, but there might be few where you do have to write down some equations yourself.
To start with a simple side note: Matlabs ODE45 function allows to simulate any function of the form dx/dt = f(x,u), regardless of how non-linear or time variant they might become.
to linearize such a system, you need to derive a jacobian matrix and substitute the linearization point in this matrix. This linearization point is any point where all state derivatives equal 0, it does not need to be a balance point. However, it is desired to have it a balance point as this means the linearization point is a stable equilibrium.
So in MATLAB:
create symbolic variables for all states and inputs (so x(t), s(t) and p(t))
create symbolic state equations dx/dt = f(x,u) and output equation y = g(x,u)
derive symbolic State space matrices A,B,C,D using the "jacobian" function
substitute linearization point in these symbolic state space matrices using "subs"
use eval(symbolic matrix) to retrieve a numeric matrix.
Depending on the non-linear complexity and the chosen linearization point, the linearized system might only within acceptable bounds from the actual system for a very tight region, so be aware of that.

Matlab ode15s: postive dx/dt, decreasing x(t)

In my script, I call the ODE solver ode15s which solves a system of 9 ODE's. A simplified structure of the code:
[t, x] = ode15s(#odefun,tini:tend,options)
...
function dx = odefun(t,x)
r1=... %rate equation 1, dependent on x(1) and x(3) for example
r2=... %rate equation 2
...
dx(1) = r1+r2-...
dx(2) = ...
...
dx(9) = ...
end
When reviewing the results I was curious why the profile of one state variable was increasing at a certain range. In order to investigate this, I used conditional debugging within the ode function so I could check all the rates and all the dx(i)/dt equations.
To my big surprise, I found out that the differential equation of the decreasing state variable was positive. So, I simulated multiple rounds with the F5-debug function, and noticed that indeed the state variable consistently decreased, while the dx(i)/dt would always remain positive.
Can anyone explain me how this is possible?
It is not advisable to pause the integration in the middle like that and examine the states and derivatives. ode15s does not simply step through the solution like a naive ODE solver. It makes a bunch of calls to the ODE function with semi-random states in order to compute higher-order derivatives. These states are not solutions to system but are used internally by ode15s to get a more accurate solution later.
If you want to get the derivative of your system at particular times, first compute the entire solution and then call your ODE function with slices of that solution at the times you are interested in.

Error in `ode45` matlab

The main function of ode is given as follows.
function dxdt = state( t,x,vgth,vgval1,vgval2)
vgval=vgval1+vgval2;
p=1;
k=10^0.7;
window1=1-((2*x)-1).^(2*p);
dxdt=k*(vgval-vgth+1.2)*window1;
end
The Script is given below.
step=0.01;
t = 0:step:10;
f=4*0.157;
vgate1= #(t) abs(5*sin(2*f*t)).*heaviside(5-t);
vgate2=#(t) -abs(5*sin(2*f*t)).*heaviside(t-5);
The Function call part is as follows.
x0=0.01;
vgth=1.9;
[t,x] = ode45(#(t,x) state1 (t,x,vgth,vgate1(t),vgate2(t)), t, x0);
plot(t,x)
Issue
It gives me error when I use the negative sign with vgate2 .
It works fine If i remove the negative sign of vgate2.
Desired Result
I want my plot with a negative sign in vgate2.Actually I want to use two positive sine pulses and two negative sign pulses.That is why I have used negative value of vgate2.
ODE integrators of order p expect the differential equation to be p+2 times continuously differentiable with decent sized derivatives.
Any deviation from that is seen as "stiffness" by step-size adaptation strategies, and reacted to by increasingly violent reductions in step-size. Any kink or jump in the lower derivatives is seen as similar to wild oscillations in the higher derivative, throwing the step-size adaptor for a loop.
However, if the integration is stopped directly at an event and then restarted using the values there as initial values, the integrator does not "see" the event, and thus has no need to react to it.

ODE System, IVP with differential initial condition

I am trying to model a system of three differential equations. This is a droplet model, parametrized vs the arc length, s.
The equations are:
dx/ds=cos(theta)
dz/ds=sin(theta)
(theta)/ds=2*b+c*z-sin(theta)/x
The initial conditions are that x,z, and theta are all 0 at s=0. To avoid the singularity on d(theta)/ds, I also have the condition that, at s=0, d(theta)/ds=b. I have already written this code:
[s,x]=ode23(#(s,x)drpivp(s,x,p),sspan,x0);
%where p contains two parameters and x0 contains initial angle theta, x, z values.
%droplet ODE function:
function drpivp = drpivp(s,x,p);
%x(1)=theta
%x(2)=x
%x(3)=z
%b is curvature at apex
%c is capillarity constant
b=p(1);
c=p(2);
drpivp=[2/p(1)+p(2)*x(3)-sin(x(1))/x(2); cos(x(1)); sin(x(1))];
Which yields a solution that spirals out. Instead of creating one droplet profile, it creates many. Of course, here I have not initialized the equation properly, because I am not certain how to use a different equation for theta at s=0.
So the question is: How do I include the initial condition that d(theta)/ds=b instead of it's usual at s=0? Is this possible using the built-in solvers on matlab?
Thanks.
There are several ways of doing this, the easiest is to simply add an if statement into your equation:
function drpivp = drpivp(s,x,p);
%x(1)=theta
%x(2)=x
%x(3)=z
%b is curvature at apex
%c is capillarity constant
b=p(1);
c=p(2);
if (s == 0)
drpivp=[b; cos(x(1)); sin(x(1))];
else
drpivp=[2/p(1)+p(2)*x(3)-sin(x(1))/x(2); cos(x(1)); sin(x(1))];
end