Define and solve nested non-linear ODE in MATLAB - matlab

I'm given the differential equation y'' = -g + a(t)/m with a(t) = k*y'^2 where y is a function of t (time). My initial conditions are y(0) = 600; and y'(0) = 0;
In MATLAB I know how to define y'' with
ydd = diff(y,t,2) == -g + a(t)/m;
but I'm lost at the fact that this is a 'nested' non linear differential equation and I'm not quite sure how to define it, let alone, solve it in MATLAB.

The better first order system is
v' = -g + k/m*v^2
y' = v
as there is no longer a third unknown function a(t) involved.
Challenge: Solve the first equation manually via separation of variables and partial fraction decomposition or identifying the scaling for the Area tangent hyperbolicus as the integral for the side of v.

This is two coupled first order ODEs.
Let z = y'. Then you have:
z' = -g + a(t)/m
y' = sqrt(a(t)/k)
You need initial conditions y(0)=600 and z(0)=0.
That equation z(0)=0 implies that a(0)/m = g. Solve for a(0) = gm.
These are the equations you need to solve.

Related

Solve complex matrix equation

I have a complex equation involving matrices:
R = expm(X)*A + (expm(X)-I)*inv(X)*B*U;
where R, B and U are known matrices.
I is an identity matrix.
I need to solve for X. Is there any way to solve this in MATLAB?
If your equation is nonlinear and you have access to MATLAB optimization toolbox you can use the fsolve function (You can still use it for a linear equation, but it may not be the most efficient approach). You just need to reformat your equation into the form F(x) = 0, where x is a vector or a matrix. For example, if X is a vector of length 2:
Define your function to solve:
function F = YourComplexEquation(X)
Fmatrix = expm(X)*A + (expm(X)-I)*inv(X)*B*U - R
% This last line is because I think fsolve requires F to be a vector, not a matrix
F = Fmatrix(:);
Then call fsolve with an initial guess:
X = fsolve(#YourComplexEquation,[0;0]);

Differential Equation ODE

I have a problem with some differential equations of first-order.
I'm trying to solve them with ode23 and ode23s.
The differential equations are:
y'+3y+z=0
z'-y+z=0
with the initial values:
y(0)=1 and z(0)=1
I also want to compare it with the analytical solution:
y=exp(-2x)(1-2x)
z=exp(-2x)(1+2x)
I want to do it this way because I need to do the comparison in order to choose the better solver: ode23 or ode23s, whichever one is closer to the analytical solution.
My code is:
function dy=projectb1(t,y)
%y'=-4y
%z'= 0
%y(1)=y'
%y(2)=z'
dy = [-4*y(2); 0*y(1)];
and:
% Comparison of analytical solution
clear
options= odeset('RelTol',1e-4,'AbsTol', [1e-4 1e-4]);
%figure
%t1=cputime;
[t23,y23]= ode23('projectb1',[0 12],[1 1],options);
[t23s,y23s]= ode23s('project1',[0 20],[1 0],options);
%tobl = cputime -t1
figure
ya=exp(-2*t23).*(1-2*t23);
za=exp(-2*t23).*(1+2*t23);
plot(t23,ya,za,'r',t23,y23(:,1),'g-.',t23s,y23s(:,1),'b');
%legend('ya','ode23','ode23s',0)
text(3.4,-1.7,'ya')
title('\bf{Analytical and numerical solutions using} \it{ode23s, ode23}')
But it doesn't work. Could someone help me?
The error Matlab throws right away has to do with the line
plot(t23,ya,za,'r',t23,y23(:,1),'g-.',t23s,y23s(:,1),'b');
This should be
plot(t23,ya,t23,za,'r',t23,y23(:,1),'g-.',t23s,y23s(:,1),'b');
You missed that extra t23.
Another problem appears to be in the definition of the differential equation.
For systems of differential equations, the Matlab ODE suite passes a vector x whose components are the values of the functions you are attempting to approximate.
Therefore, as in the example below, the first component of x is the value of y at time t, and the second component of x is the value of z at time t:
function dx = projectb1(t,x)
y = x(1);
z = x(2);
dy = -3*y - z;
dz = y - z;
dx = [dy;dz];
end
I changed the input y to x to make it clear that what is input is a vector of values of y and z.
Also, note that while ode23 has the initial conditions [1,1], ode23s has [1,0], which means it is solving a different initial value problem.

ODE solver of a system with adaptive law

I have an unknown non-linear system and I want to model it using another system with some adaptable parameters (for instance, a neural network). So, I want to fix an online learning structure of the unknown system without knowing its dynamics, I can only interact with it through inputs-outputs. My problem is that I can not make it work in MATLAB using ode solvers. Lets say that we have this real system (my actual system is more complicated, but I will give a simple example in order to be understood):
function dx = realsystem(t, x)
u = 2;
dx = -3*x+6*u;
end
and we solve the equations like this:
[t,x_real] = ode15s(#(t,x)realsystem(t,x), [0 1], 0)
We suppose that is an unknown system and we do not know the coefficients 3 and 6 so we take an adaptive system with the 2 adaptive laws:
dx(t) = -p1(t)*x(t) + p2(t)*u(t)
dp1(t) = -e(t)*x(t)
dp2(t) = e(t)*u(t)
with e(t) the error e(t) = x(t) - x_real(t).
The thing is that I cannot find a way to feed the real values for each t to the ode solver in order to have online learning.
I tried with something like this but it didn't work:
function dx = adaptivesystem(t, x, x_real)
dx = zeros(3,1);
e = x_real - x;
u = 2;
dx(1) = -x(2)*x(1)+x(3)*u;
dx(2) = -e*x(1); %dx(2) = dp1(t)
dx(3) = e*u; %dx(3) = dp2(t)
end
You should be aware that your problem is ill-posed as it is. Given any trajectory x(t) obtained via sampling and smoothing/interpolating, you can choose p1(t) at will and set
p2(t) = ( x'(t) - p1(t)*x(t) ) / u.
So you have to formulate restrictions. One obvious is that the functions p1 and p2 should be valid for all trajectories of the black-box system. Do you have different trajectories available?
Another variant is to demand that p1 and p2 are constants. Actually, in this case and if you have equally spaced samples available, it would be easier to first find a good difference equation for the data. With the samples x[n] for time t[n]=t0+n*dt form a matrix X with rows
[ -u, x[n], x[n+1], ... ,x[n+k] ] for n=0, ... , N-k
and apply QR decomposition or SVD to X to determine the right hand kernel vectors. QR may fail to show a usable rank deficiency, so use the SVD on the top square part of R = USV^T, S diagonal, ordered as usual, U,V square and orthogonal, and use the last row of V, with coefficients
[b, a[0], ..., a[k] ],
corresponding to the smallest eigenvalue, to form the difference equation
a[0]*x[n]+a[1]*x[n-1]+...+a[k]*x[n-k]=b*u.
If the effective rank of R resp. S is not (k-1), then reduce k to be the effective rank plus one and start again.
If in the end k=1 is found, then you can make a differential equation out of it. Reformulate the difference equation as
a[0]*(x[n]-x[n-1])/dt = -(a[0]+a[1])/dt * x[n-1] + b/dt * u
and read off the differential equation
x'(t) = -(a[0]+a[1])/(a[0]*dt) * x(t) + b/(a[0]*dt) * u
One may reject this equation if the coefficients become uncomfortably large.

Solve linear equation system by given LU decomposition and vector of constants

Given L and U LU decomposition and vector of constants b such that LU*x=b , is there any built in function which find the x ? Mean something like -
X = functionName(L,U,b)
Note that in both L and U we are dealing with triangular matrices which can be solved directly by forward and backward substitution without using the Gaussian elimination process.
Edit :
Solving this linear equation system should be according to the following steps -
1. define y - s.t Ux=y
2. solve Ly=b by forward substitution
3. solve Ux=y by backward substitution
4. return y
Edit 2 :
I found linalg::matlinsolveLU but I didn't try it cause I have too old version (R2010a) . Is it working for anyone ?
If you have:
A = rand(3);
b = rand(3,1);
then the solution to the system can be simply computed as:
x = A\b
Or if you already have an LU decomposition of A, then:
[L,U] = lu(A);
xx = U\(L\b)
the mldivide function is smart enough to detect that the matrix is triangular and chose an algorithm accordingly (forward/backward substitution)
I think this is what you're looking for:
A = rand(3,3); % Random 3-by-3 matrix
b = rand(3,1); % Random 3-by-1 vector
[L,U] = lu(A); % LU decomposition
x = U\(L\b) % Solve system of equations via mldivide (same as x = A\b or x = (L*U)\b)
err = L*U*x-b % Numerical error
The system of equations is solved using mldivide. You might also look at qr which implements QR decomposition instead of using LU decomposition. qr can directly solve A*x = b type problems and is more efficient. Also look at linsolve. For symbolic systems you may still be able to use mldivide, or try linalg::matlinsolveLU in MuPAD.

Is there any Good library for solving differentiation equations using differentiation operator?

Good library for solving differentiation equations (not only 1rst order) using differentiation operator?Better written in C/C++/PHP/C#/Actionscript/Javascript
(1) Generic analytic ODE solver is not possible.
(2) If you're given an n-th order ODE you can convert it into n 1st order ODE, e.g.
y'' + 2y' + 3y + 4 = 0
now let z = y', you've got a coupled 1st order ODE:
z' = -2z - 3y - 4
y' = z
(3) For C, try GSL: http://www.gnu.org/software/gsl/manual/html_node/Ordinary-Differential-Equations.html.