Solving mulitple nonlinear equations in MATLAB - matlab

Hi I am very new to MATLAB. I was trying to solve these equations to either get an analytical solution or solve them numerically. For the analytical solution, I get the following error:
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
In solve (line 305)
Here is my code:
syms A B Ph Pl
delta = 0.1;
mu = 0.02;
sigma = 0.2;
w = 1;
k = 3;
l = 2;
beta = (0.5 - mu/sigma^2) + ((mu/sigma^2 - 0.5)^2 + 2*delta/sigma^2)^0.5;
alpha = -((0.5 - mu/sigma^2) - ((mu/sigma^2 - 0.5)^2 + 2*delta/sigma^2)^0.5);
eqn1 = (A*(Ph^(-alpha)) + (Ph/delta-mu)) -(B*Ph^beta)-k;
eqn2 = (A*Pl^(-alpha) + Pl/(delta-mu) -w/delta) - B*Pl^beta + l;
eqn3 = -alpha*A*(Ph^(-alpha-1)) + 1/(delta-mu) - (beta*B*Ph^(beta-1));
eqn4 = alpha*A*Pl^(-alpha-1)- (beta*B*Pl^(beta-1));
sol = solve([eqn1==0, eqn2==0, eqn3==0, eqn4==0], [A, B, Ph, Pl]);

Matlab is telling you it can't find an analytic solution, but it is definitely finding numerical solutions when I run it, however, they're all complex. Type:
sol.A
in your command window to see what A looks like, same with B, Ph and Pl.

Related

How to solve a system of three first-order ODEs in Matlab

I am using Matlab to try and solve a system of three first-order ODEs, but the error message I get is 'syms' requires Symbolic Math Toolbox.
Error in spiders (line 1)
syms f(t) s(t) v(t) r W c h q a k b H K e
On a previous occasion, I received an error saying that this system of ODEs cannot be solved explicitly (i.e. in closed form). I think that numerical integration is the only way. The r,W,c,h, etc are parameters. Could someone please tell me how I can simulate/solve and plot the ODEs below?
syms f(t) s(t) v(t) r W c h q a k b H K e
r = 1;
W = 0.5;
c = 0.4;
h = 0.9;
q = 9;
a = 5;
k = 0.8;
b = 6;
H = 3;
K = 1.3;
e = 2;
ode1 = diff(f) == r*f*(1 - f/W) - c*s*f - h*(1 - q)*f;
ode2 = diff(s) == s*(-a + k*b*v/(H + v) + k*c*f) - h*K*q*s;
ode3 = diff(v) == v*(e - b*s/(H + v)) - h*q*v;
odes=[ode1;ode2;ode3]
S = dsolve(odes)
plot(S);
Looks like you where previously using a different MATLAB license, which included the Symbolic Math Toolbox. I assume you no longer have it available and you are now looking for numeric alternatives.
You have to define functions for the equations you are trying to solve. Then you can call one of the ode solvers. An example from the documentation:
y0 = [1; 0; 0];
tspan = [0 4*logspace(-6,6)];
M = [1 0 0; 0 1 0; 0 0 0];
options = odeset('Mass',M,'RelTol',1e-4,'AbsTol',[1e-6 1e-10 1e-6]);
[t,y] = ode15s(#robertsdae,tspan,y0,options);
Where robertsdae is the equation to solve. The full example including further explanations is available here.

Solving integral for x in MATLAB, where x is bound and part of the integrand

I am trying to solve an equation for x in Matlab, but keep getting the error:
Empty sym: 0-by-1
The equation has an integral, where x is the upper bound and also part of the integrand 1. The code I use is the following:
a = 0.2; b= 10; c = -10; d = 15; mu = 3; sig = 1;
syms x t
eqn = 0 == a + b*normcdf(x,mu,sig)+c*int( normcdf(d + x - t,mu,sig)*normpdf(t,mu,sig),t,0,x);
A = vpasolve(eqn,x)
Any hints on where I am wrong?
I believe that the symbolic toolbox may not be good enough to solve that integral... Maybe some assume or some other trick can do the job, I personally could not find the way.
However, to test if this is solvable, I tried Wolfram Alpha. It gives a result, that you can use.
eq1=a + b*normcdf(x,mu,sig);
resint=c*(t^3*(d - t + x)*erfc((mu - x)/(sqrt(2)*sig)))/(4*sig*exp((-mu + x)^2/(2*sig^2))*sqrt(2*pi));
A=vpasolve(eq1+subs(resint,t,x)-subs(resint,t,0) ==0)
gives 1.285643225712432599485355373093 in my PC.

MATLAB System of Dependent Differential Equations

Given a system of differential equations such as:
dy/dt = f(t)
dx/dt = g(t)
A solution can be found using dsolve, such as:
dsolve(diff(y) == f(t), diff(x) == g(t), y(0) == 1, x(0) == 1);
But what about a system where all the variables depend on each other:
dy/dt = f(y,z)
dx/dt = g(x,y)
dz/dt = h(z,x)
When approached in the same way, with initial conditions, for a system which does have a solution, I cannot find a solution.
I know the system I have tried can produce solutions as I have used a stochastic/deterministic simulator - think there's probably some strange syntax to use.
I'm specifically looking for the solution where the derivatives are all zero, if that helps.
EDIT:
Here is an example:
PX/dt = (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PZ^n))))/kd_mRNA)-kd_prot*PX;
PY/dt = (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PX^n))))/kd_mRNA)-kd_prot*PY;
PZ/dt = (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PY^n))))/kd_mRNA)-kd_prot*PZ;
with the coefficients:
eff = 20;
KM = 40;
tau_mRNA=2.0;
tau_prot=10;
ps_a=0.5;
ps_0=5.0E-4;
t_ave = tau_mRNA/log(2);
k_tl=eff/t_ave;
a_tr=(ps_a-ps_0)*60;
a0_tr=ps_0*60;
kd_mRNA = log(2)/tau_mRNA;
kd_prot = log(2)/tau_prot;
beta = tau_mRNA/tau_prot;
alpha = a_tr*eff*tau_prot/(log(2)*KM);
alpha0 = a0_tr*eff*tau_prot/(log(2)*KM);
n=2;
And the initial conditions:
PX0 = 20;
PY0 = 0;
PZ0 = 0;
This produces a response:
This clearly has a steady state solution (all derivatives 0).
In MATLAB I have tried:
%%
syms PX(t) PY(t) PZ(t);
z = dsolve(diff(PX) == (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PZ^n))))/kd_mRNA)-kd_prot*PX, diff(PY) == (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PX^n))))/kd_mRNA)-kd_prot*PY, diff(PZ)==(k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PY^n))))/kd_mRNA)-kd_prot*PZ,PX(0)==20)
and:
%%
eq1 = (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PZ^n))))/kd_mRNA)-kd_prot*PX;
eq2 = (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PX^n))))/kd_mRNA)-kd_prot*PY;
eq3 = (k_tl*(a0_tr + ((a_tr*KM^n)/((KM^n) + (PY^n))))/kd_mRNA)-kd_prot*PZ;
dsolve(diff(PX)==eq1,PX(0)==20,diff(PY)==eq2,PY(0)==0,diff(PZ)==eq3,PZ(0)==0)
Both produce no errors but return an empty sym.
Your numeric solution appears to have an oscillatory component. The "steady state" may be a zero amplitude limit cycle, which is a non-trivial solution. You definitely shouldn't expect a system like this to have an easy-to-find analytic solution. The cyclic relations between your three variables also doesn't help. For what it's worth, Mathematica 10's DSolve also is unable to find a solution.
Though it won't get you to a solution, the way you're using symbolic math is less than optimal. When you use something like log(2) in a symbolic math equation, 2 should be converted to a symbolic value first. For example, sym(log(2)) yields the approximation 6243314768165359/9007199254740992, whereas log(sym(2)) returns the exact log(2). This latter form is much more likely to lead to solutions if they exist. Here's a modified version of your code, which unfortunately still returns "Warning: Explicit solution could not be found":
eff = 20;
KM = 40;
tau_mRNA=2;
tau_prot=10;
ps_a=1/sym(2);
ps_0=5/sym(10000);
ln2 = log(sym(2));
t_ave = tau_mRNA/ln2;
k_tl=eff/t_ave;
a_tr=(ps_a-ps_0)*60;
a0_tr=ps_0*60;
kd_mRNA = ln2/tau_mRNA;
kd_prot = ln2/tau_prot;
beta = tau_mRNA/tau_prot;
alpha = a_tr*eff*tau_prot/(ln2*KM);
alpha0 = a0_tr*eff*tau_prot/(ln2*KM);
n=2;
PX0 = 20;
PY0 = 0;
PZ0 = 0;
syms PX(t) PY(t) PZ(t);
eq1 = (k_tl*(a0_tr + a_tr*KM^n/(KM^n + PZ^n))/kd_mRNA)-kd_prot*PX;
eq2 = (k_tl*(a0_tr + a_tr*KM^n/(KM^n + PX^n))/kd_mRNA)-kd_prot*PY;
eq3 = (k_tl*(a0_tr + a_tr*KM^n/(KM^n + PY^n))/kd_mRNA)-kd_prot*PZ;
s = dsolve(diff(PX,t)==eq1,diff(PY,t)==eq2,diff(PZ,t)==eq3,PX(0)==20,PY(0)==0,PZ(0)==0)

Matlab - Unexpected Results from Differential Equation Solver Ode45

I am trying to solve a differential equation with the ode solver ode45 with MATLAB. I have tried using it with other simpler functions and let it plot the function. They all look correct, but when I plug in the function that I need to solve, it fails. The plot starts off at y(0) = 1 but starts decreasing at some point when it should have been an increasing function all the way up to its critical point.
function [xpts,soln] = diffsolver(p1x,p2x,p3x,p1rr,y0)
syms x y
yp = matlabFunction((p3x/p1x) - (p2x/p1x) * y);
[xpts,soln] = ode45(yp,[0 p1rr],y0);
p1x, p2x, and p3x are polynomials and they are passed into this diffsolver function as parameters.
p1rr here is the critical point. The function should diverge after the critical point, so i want to integrate it up to that point.
EDIT: Here is the code that I have before using diffsolver, the above function. I do pade approximation to find the polynomials p1, p2, and p3. Then i find the critical point, which is the root of p1 that is closest to the target (target is specified by user).
I check if the critical point is empty (sometimes there might not be a critical point in some functions). If its not empty, then it uses the above function to solve the differential equation. Then it plots the x- and y- points returned from the above function basically.
function error = padeapprox(m,n,j)
global f df p1 p2 p3 N target
error = 0;
size = m + n + j + 2;
A = zeros(size,size);
for i = 1:m
A((i + 1):size,i) = df(1:(size - i));
end
for i = (m + 1):(m + n + 1)
A((i - m):size,i) = f(1:(size + 1 - i + m));
end
for i = (m + n + 2):size
A(i - (m + n + 1),i) = -1;
end
if det(A) == 0
error = 1;
fprintf('Warning: Matrix is singular.\n');
end
V = -A\df(1:size);
p1 = [1];
for i = 1:m
p1 = [p1; V(i)];
end
p2 = [];
for i = (m + 1):(m + n + 1)
p2 = [p2; V(i)];
end
p3 = [];
for i = (m + n + 2):size
p3 = [p3; V(i)];
end
fx = poly2sym(f(end:-1:1));
dfx = poly2sym(df(end:-1:1));
p1x = poly2sym(p1(end:-1:1));
p2x = poly2sym(p2(end:-1:1));
p3x = poly2sym(p3(end:-1:1));
p3fullx = p1x * dfx + p2x * fx;
p3full = sym2poly(p3fullx); p3full = p3full(end:-1:1);
p1r = roots(p1(end:-1:1));
p1rr = findroots(p1r,target); % findroots eliminates unreal roots and chooses the one closest to the target
if ~isempty(p1rr)
[xpts,soln] = diffsolver(p1x,p2x,p3fullx,p1rr,f(1));
if rcond(A) >= 1e-10
plot(xpts,soln); axis([0 p1rr 0 5]); hold all
end
end
I saw some examples using another function to generate the differential equation but i've tried using the matlabFunction() method with other simpler functions and it seems like it works. Its just that when I try to solve this function, it fails. The solved values start becoming negative when they should all be positive.
I also tried using another solver, dsolve(). But it gives me an implicit solution all the time...
Does anyone have an idea why this is happening? Any advice is appreciated. Thank you!
Since your code seems to work for simpler functions, you could try to increase the accuracy options of the ode45 solver.
This can be achieved by using odeset:
options = odeset('RelTol',1e-10,'AbsTol',1e-10);
[T,Y] = ode45(#function,[tspan],[y0],options);

linear two-equation system, two variables to second derivative in both

Forgive me if this is considered reposting, but I've been advised I might have given a bad format.
I'm trying to solve the two linear, second order differential equations. I want to break them into single order equations, but I can't see how as both variables have second order derivatives in both problems.
(m*a)u” + (I + m*a^2 )θ” + (d*a^2 )θ’ + (K - m*g*a)θ = 0
(M + m)u” + (m*a)θ” = -F
I've gone to some lengths with both dsolve and ode45
This is my dsolve code:
M =70-5.876;
m =5.876;
a =(((0.05)^2)+((0.13^2))^0.5);
IG = 0.0233;
d = 500;
k = 500;
g = 9.81;
f = 628;
%y is u, x is theta
syms M m a IG d k g y(t) x(t)
Dy = diff(y);
Dx = diff(x);
eqn1 = (M+m)*diff(y,2) + M*diff(x,2) == -f;
eqn2 = m*a*diff(y,2) + (IG + m*a*a)*diff(x,2) + (d*a*a)*diff(x) + (k - m*g*a)*x == 0;
t=0:0.01:10;
z = dsolve(eqn1,eqn2, y(0)==0, Dy(0)==0, x(0)==0, Dx(0)==0, 't');
z.x
z.y
It does give me very, very long equations that I can't seem to plot with respect to time and I don't know why. If anyone can advise me I'd be very grateful. Thanks for looking!
In this case u" can be replaced out of the general equation with simple algebraic manipulation. The resulting equation will be of the order:
Eq1: K1*θ” + K2*θ’ + K3*θ = -F
u can be solved directly with the solution of Eq1. Try to avoid placing u in the equation. If you must, you'll need to rewrite the equations so as to solve with the third differential of θ.