Boundary conditions in differential equations in Modelica - modelica

I need help with solving differential equations with modelica. particularly with the boundary conditions which in my case are not defined at z = 0.
The equation is really simple:
a*y'' - y' - b*y = 0
boundary conditions:
a*y' = y - 1 at z = 0;
y' = 0 at z = 1;
I know the 'initial equation' and 'start' commands but they are defined at z = 0. How do you define the boundary conditions at other z values.
Thank you!

What you are trying to solve is a boundary value problem (as far as I can tell, you didn't really define what z was). Modelica was designed to handle initial value problems. As such, I don't think you'll be able to solve this problem directly in Modelica. What you might be able to do is wrap an optimization loop around the initial value problem solutions to try and satisfy your additional constraint.

Related

Solve system of differential equation with embedded non diferential equations, using Octave/Matlab (see picture)

I have the following equation system (click to see picture)
, and would like to solve for X(t), Y(t), Z(t), hopefully using Octave/Matlab, which I'm familiar with, but I would not mind solving it by any other means necessary.
Now, Fsolve is useful for regular equation systems, and Ode45, Lsode are useful for differential equations. But, what about this particular system? Note that the differential equation at the bottom contains not only Y, but, also, both X and Z, and those are dependent on the two non-differential equations above.
To be honest, I'm not quite sure how to approach a basic code to solve this system, and after spending some time thinking, I decided to ask for help. I really appreciate any guidance to solve this problem somewhat efficiently, if it is even possible. Almost any reply would be of some use to me right now.
If you know y, you can solve for x, and this even unconditionally as the second equation in monotonous in x
x = fsolve(#(x) y^2-1413.7*x-1095.2*cos(x)+2169, 0)
Then once you know x, you can solve for z using the known inverse cosine
z = acos(0.20978-cos(x))
This may actually fail to give a result if cos(x) comes close to -1. One can artificially cut out that error, introducing a possibly wrong solution
z = acos(min(1,0.20978-cos(x)))
For simplicity, assemble these operations in a helper function
function [x,z] = solve_xz(y)
x = fsolve(#(x) y^2-1413.7*x-1095.2*cos(x)+2169, 0);
z = acos(min(1,0.20978-cos(x)));
end
Now use that to get the ODE for y
function dy = ode_y(t,y)
[x,z] = solve_xz(y(1));
dy = [ y(2); y(3); 6666.6667*(z-x)-333.3333*y(1)-33.3333*y(2)-5*y(3) ];
end
and apply the ODE solver of your choice. It is quite likely that the system is stiff, so ode45 might not be the best solver.

Optimization with an inherent differential equation

I am trying to solve an optimization problem where the given objective function is to minimize
norm(R - R*)
where R is obtained by solving a boundary value problem (BVP) and `R* is a known value.
For example:
R = (p1 + p2*p3);
The BVP is given by
p1dot = p2 + p3 + x1;
p2dot = x2 + p1*p2;
p3dot = x3 + p1*p2*p3;
where x1, x2 and x3 are the optimized variables.
I am trying to solve this in MATLAB using fmincon where the BVP is solved in the objective function at every guess by the solver to estimate norm(R-R*).
EDIT 1 : I have a non linear inequality constraint which is a function of pi where i = 1,2,3. This is the reason for choosing fmincon
The issue I am facing is that during some guesses the BVP doesn't converge and the optimization stops. I guess the issue is some guess value to solve the BVP doesn't satisfy. If I give bounds to the variables then it solves the optimization problem. I am not really aware of the bounds on variables beforehand.
In order to overcome this I want to detect the guess when BVP is not solved, save them and force the optimization routine to not try values when it cannot be solved by going back to its previous guess. How do we implement this in fmincon routine?
EDIT 2 : I figured out the issue is with initial guess provided to the BVP solver. For smaller values of the optimization variables x this guess is good but when the magnitude of x increase this guess is not good enough.
Is there a way to update the solution of 'BVP' in the previous iteration as the initial guess for the next iteration in optimization. Intuitively this should fix it. Please let me know if there are some loop holes in this method.

Solving ODE with Simulink in Matlab

I need to solve this ODE using Simulink and I don't know how to make it.
I only know how to do it using ODE solvers.
y'' - y' - 2y = e^(3x)
y(0)=1, y'(0)=2.
I rewrote the equation obtaining an ODEs:
y' = f(x,y)
y(x0) = y0
y'1 = y2
y2= e^(3*x) + y' + 2y
Using ODE solver.
If someone can help me to solve this using a Simulink Model I would appreciate it.
I know how to solve it in Matlab using ODE solvers as ode23 and ode23s but I don't know how to do it using a Simulink Model.
Thanks in advance
Can you solve it in closed form? Looks doable to me. I advise anyone to have the answer in hand if possible before you start a numerical solution.
Here's what I get. Check me:
y(x) = e^(-x)*(8e^3x + 3e^4x + 1)/12
Wolfram Alpha says this is correct.
(Note: Trouble for large values of x - this response will grow at e^3x rate.)
You need to express this as a set of coupled first order ODEs.
y' = z
z' = z + 2y + e^(3x)
Boundary conditions become:
y(0) = 1
z(0) = 2
You can set up the equation yourself term by term in Simulink and add the initial conditions to the integrators by double clicking and setting the corresponding field.
So a quick implementation looks like
I assumed that your x is a time-like quantity hence I placed a ramp function. Clock etc. would also do.
Alternatively you can form the state space system or the transfer function with explicitly taking the initial conditions into account.

Matlab: Is it possible to numerically solve a system of ode's with a mixture of initial and terminal conditions?

I'm trying to use ode45 to solve a system of ODE's:
[X,Y]= ode45(#sys,[0, T],y0);
where,
function dy = sys(t,y)
dy(1) = f_1(y)
dy(2) = f_2(y)
dy(3) = f_3(y)
end
The problem is that the function ode45 requires that y0 be initial values [y_1(0), y_2(0), y_3(0)], while in my system, I only have the values [y_2(0), y_3(0), y_3(T)] available.
Mathematically, this set of initial/terminal conditions should be enough to pin down the system, but is there any way I can work with that by ode45 or any other functions in MATLAB?
Thanks!
After digging in the Matlab documentation for a little bit, I think the more elegant way is to use the bvp4c function. bvp4c is a function specifically designed to handle boundary value problems like this, as opposed to ode**, which are really for initial value problems only. In fact, there's a whole set of other functions such as deval and bvpinit in Matlab that really facilitate the use of bvp4c. Here's the link to the Matlab documentation.
I'll post a brief (and perhaps a bit contrived) example here:
function [y1, y2, y3] = test(start,T)
solinit = bvpinit(linspace(0,3,10), [1,1,0]);
sol = bvp4c(#odefun,#bvpbc,solinit);
tspan = linspace(start,T,100);
S = deval(sol, tspan);
y1 = S(1,:);
y2 = S(2,:);
y3 = S(3,:);
plot (tspan,y1)
figure
plot (tspan,y2)
figure
plot (tspan,y3)
%% system definition & BVCs
function dydx = odefun(t,y)
dydx(1) = y(1) + y(2) + t;
dydx(2) = 2*y(1) + y(2);
dydx(3) = 3 * y(1) - y(2);
end
function res = bvpbc(y0,yT)
res= [y0(3) yT(2) yT(3)];
end
end
The test function outputs 3 vectors of solutions points for y1, y2 and y3 respectively, and also plots them.
Here are the variable paths plotted by Matlab:
Also, I found this video by professor Jake Blanchard from WMU to be very helpful.
One approach is to use the shooting method to iteratively solve for the unknown initial state y_1(0) such that the desired final state y_3(T) is satisfied.
The iteration proceeds by solving a nonlinear equation F = 0:
F(y_1(0)) = Y_3(T) - y_3(T)
where the uppercase function Y is the solution obtained by integrating the ODE's forward in time from a set of initial conditions. The task is to guess the value of y_1(0) to obtain F = 0.
Since we're now solving a nonlinear equation, all of the usual approaches apply. Specifically you could use either bisection or Newton's method to update your guess for the unknown initial state y_1(0). Note a couple of things:
The ODE's are integrated on [0,T] at each iteration of the nonlinear solve.
There may be multiple solutions for F = 0, depending on the structure of your ODE's.
Newton's method may converge faster than bisection, but may also be numerically unstable unless you can provide a good starting guess for y_1(0).
Using existing MATLAB functions, the bounded scalar nonlinear solver FMINBND might be a good choice as a nonlinear solver.
Hope this helps.

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