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

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.

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]);

Define and solve nested non-linear ODE in 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.

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.

Finding the roots of a polynomial with symbolic coefficients

As part of an assignment, I had to derive the equations of motion for a car's suspension system. Essentially it is a spring mass damper problem. The values for the car mass, M1, wheel mass, M2, the spring constant, k1 & k2 and the damping constant, c have not been given. I have derived the equations of motion and derived a transfer function relating the road surface (input) to the resulting car body displacement (output).
I must determine the poles of this transfer function, hence I need to find the roots of the characteristic equation (denominator). The problem is that I do not have any values for the aforementioned variables and I am trying to either factorise my 4th order polynomial in MATLAB symbolically or calculate the roots straight away.
I cannot assume any values, and it must be solved symbolically, however I do not know if this is possible in MATLAB.
I do not have a lot of experience with MATLAB so I am not aware of all its capabilities.
The characteristic equation I am trying to solve is :
(M1*M2)*s^4 + c*(M1+M2)*s^3 + ((M1*k1)+(M1*k2)+c^2+(M2*k2)-c)*s^2 + k1*c*s + ((k1*k2)-(k2^2))
Thank You in advance.
You have some errors in your equation;
c(M1+M2)*s^3 -> c*(M1+M2)*s^3
+ +k1*c*s -> + k1*c*s
But if you want to solve multivariate equations you can do it like this;
syms M1 M2 c k1 k2 s
eqn = (your equation) == 0;
roots = solve(eqn, s);
More information here: solve
Now you only need to follow this steps in case you only want to calculate the roots of the equation , which are similar to the previous comment:
1. syms c s
2. roots=solve((M1*M2)*s^4 + c*(M1+M2)*s^3 + ((M1*k1)+(M1*k2)+c^2+(M2*k2)-c)*s^2 + k1*c*s + ((k1*k2)-(k2^2)),s)
or
roots=solve((M1*M2)*s^4 + c*(M1+M2)*s^3 + ((M1*k1)+(M1*k2)+c^2+(M2*k2)-c)*s^2 + k1*c*s +((k1*k2)-(k2^2)),c)
or
roots=solve((M1*M2)*s^4 + c*(M1+M2)*s^3 + ((M1*k1)+(M1*k2)+c^2+(M2*k2)-c)*s^2 + k1*c*s + ((k1*k2)-(k2^2)),s,c)
depending on the solution you want

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.