Integrate differential equations in Matlab - matlab

I am facing a problem in which I have to solve a system of equations:
\partial A \delta x = f(A) g(B)
\partial B \delta y = h(A) k(B)
in which f,g,h and k are some functions of A or B, and I have some initial conditions A(x=0) = a, B (y=0) = b. All previous variables are scalars.
I have solved this by making a little step in one variable and then integrating the other other equation for the whole range of the other variable (like an ode routine), then another step in the former variable and so on.
Do you have another idea more efficient? Is it possible to put one ode call running inside another call? Or perhaps there is something more elegant like combining both independent variables x and y, integrating a vector function and then undoing the change? I have the feeling that this could be done in some way.
Many thanks!

This looks to me like a partial differential equation system.
You can solve it with pdepe (here is the documentation) or you can discretize one dimension, let's say x, in Np points and have one y-equation on each point. You would complicate your system from 2 equations to Np, but turn it from PDE to ODE and therefore solvable with Matlab's ODEs.

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.

Using MATLAB's solve function to find solution to system of equations

I am having trouble solving a system of equations. I have three equations with a known solution and three unknowns in each equation. However, when I use the solve function in MATLAB, it returns with the error that I have six equations and three variables.
A snippet of my code:
syms V0 T0 X0
A=(g*X0/(2*V0^2*cos(T0)^2)-tan(T0))==a;
B=(tan(T0)-g*X0/(V0^2*cos(T0)^2))==b;
C=(-g/(2*V0^2*cos(T0)^2))==c;
soln=solve([A,B,C],[V0,T0,X0]);
I have already calculated scalar values for a, b, and c. g is a constant.
I am not sure why it is returning that I have six equations.
V0^2 means its a quadratic equation. You could solve for V0^2 as a variable. Set V0^2 = J0 and solve for J0 instead.
soln=solve([A,B,C],[J0,T0,X0]);
Then its three linear equations with three variables.
Once you get value of J0, then you need to solve for V0^2 = J0.

How to use matlab to solve ordinary differential equations with coupled intermediate variables inside?

I have a problem which can be simplified to the statement below (the real equations and functions are much more complicated):
System: dot_X1 = -A*X1; dot_X2 = B*(X2-5) + u;
dot_ means differentiate with time t, X1 and X2 are system states respectively, A and B are both intermediate variables, u is the system control input which can be a ramp or step input defined by myself.
I know how to use ode to solve normal similar problems when A and B and not coupled, for example, when A and B are only functions of X1, X2 and u respectively. But now in my problem, A and B are coupled, like:
A = f(X1, X2, u, B), B = g(X1, X2, u, A), f and g are very complicated functions.
Now in every time interval when solving the ode problem, for example, [0s, 0.01s], I have to calculate both A and B in order to use ode function in matlab. But since A and B are coupled and f and g are very complicated, I don't know how to deal with this.
Could you give me some help? Thanks a lot!
You either need to solve this coupling as a system of non-linear equations using Newton's method or a pre-packaged solver of matlab.
Or you can consider the system as a system of differential-algebraic equations
(X1',X2') = F(t,(X1,X2),(A,B),u)
0 = G(t,(X1,X2),(A,B),u)
and use the appropriate solver packages DASSL or DAEPACK or similar. See http://www.scholarpedia.org/article/Differential-algebraic_equations

how to solve first order of system of PDEs in Matlab

I have a set of 4 PDEs:
du/dt + A(u) * du/dx = Q(u)
where,u is a matrix and contains:
u=[u1;u2;u3;u4]
and A is a 4*4 matrix. Q is 4*1. A and Q are function of u=[u1;u2;u3;u4].
But my questions are:
How can I solve above equation in MATLAB?
If I solved it by PDE functions of Matlab,can I convert it to a
simple function that is not used from ready functions of Matlab?
Is there any way that I calculate A and Q explicitly. I mean that in
every time step, I calculate A and Q from data of previous time step
and put new value in the equation that causes faster run of program?
PDEs require finite differences, finite elements, boundary elements, etc. You can also turn them into ODEs using transforms like Laplace, Fourier, etc. Solve those using ODE functions and then transform back. Neither one is trivial.
Your equation is a non-linear transient diffusion equation. It's a parabolic PDE.
The equation you posted has the additional difficulty of being non-linear, because both the A matrix and Q vector are functions of the independent variable q. You'll have to start by linearizing your equations. Solve for increments in u rather than u itself.
Once you've done that, discretize the du/dx term using finite differences, finite elements, or boundary elements. You should start with a weighted residual integral formulation.
You're almost done: Next to integrate w.r.t. time using the method of your choice.
It's not trivial.
Google found this: maybe it will help you.
http://www.mathworks.com/matlabcentral/fileexchange/3710-nonlinear-diffusion-toolbox

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