How to solve a system of equations which contains symbols in MATLAB? - matlab

I am trying to solve those four equations for "p, x, y, z," , which contains two parameters "b", "a", and I have used solve command as shown in the code:
clc,clear;
syms p x y z b a
eqn1 = (1)*p + (exp(0.5*b^(1/3)))*x + (exp(-0.25*b^(1/3)) * cos(0.25*sqrt(3)*b^(1/3)))*y + (exp(-0.25*b^(1/3)) * sin(0.25*sqrt(3)*b^(1/3)))*z == 0;
eqn2 = (1)*p + (exp(-0.5*b^(1/3)))*x + (exp(0.25*b^(1/3)) * cos(0.25*sqrt(3)*b^(1/3)))*y - (exp(0.25*b^(1/3)) * sin(0.25*sqrt(3)*b^(1/3)))*z == 0;
eqn3 = (b^(1/3)*exp(0.5*b^(1/3))+a*b^(2/3)*exp(0.5*b^(1/3)))*x + (-.866*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(-.25*b^.333) + a(-.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(-.25*b^.333)-.5*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)+a(-.5*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)))*z == 0;
eqn4 = (b^(1/3)*exp(-0.5*b^(1/3))-a*b^(2/3)*exp(-0.5*b^(1/3)))*x + (.866*b^.333*sin(.433*b^.333)*exp(.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(.25*b^.333) - a(-.5*b^.666*cos(.433*b^.333)*exp(.25*b^.333)-.866*b^.666*sin(.433*b^.333)*exp(.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(.25*b^.333)+.5*b^.333*sin(.433*b^.333)*exp(.25*b^.333)-a(.5*b^.666*sin(.433*b^.333)*exp(.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(.25*b^.333) ))*z == 0;
sol = solve([eqn1, eqn2, eqn3, eqn4 ], [p, x, y, z]);
pSol = sol.p
xSol = sol.x
ySol = sol.y
zSol = sol.z
, but it keeps showing errors, would you please suggest any solution to this issue.

Let's look at this :
eqn3 = (b^(1/3)*exp(0.5*b^(1/3))+a*b^(2/3)*exp(0.5*b^(1/3)))*x + (-.866*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)-.5*cos(.433*b^.333)*b^.333*exp(-.25*b^.333) + a(-.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)))*y + (.866*cos(.433*b^.333)*b^.333*exp(-.25*b^.333)-.5*b^.333*sin(.433*b^.333)*exp(-.25*b^.333)+a(-.5*b^.666*sin(.433*b^.333)*exp(-.25*b^.333)-.866*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)))*z == 0;
I pick one of the wrong parts out, 4 in total.
a(-.5*b^.666
You see, if a(index) exists, index must be a positive integer. However, in your code the value of -.5*b^.666*cos(.433*b^.333)*exp(-.25*b^.333)+.866*b^.666*sin(.433*b^.333)*exp(-.25*b^.333) might not be a positive integer. And A may not even be a matrix, let alone an index.
Plus, before you post your question, you' d better check the basic grammar issues to save each other's time.

Related

How to make the response of the solve function symbolic?

I am solving a fourth order equation in matlab using the solve function.My script looks like this:
syms m M I L Bp Bc g x
m = 0.127
M = 1.206
I = 0.001
L = 0.178
Bc = 5.4
Bp = 0.002
g = 9.8
eqn = ((m + M)*(I + m*L^2) - m^2*L^2)*x^4 + ((m + M)*Bp + (I + m*L^2)*Bc)*x^3 + ((m + M)*m*g*L + Bc*Bp)*x^2 + m*g*L*Bc*x == 0
S = solve(eqn, x)
In the answer, I should get 4 roots, but instead I get such strange expressions:
S =
0
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 1)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 2)
root(z^3 + (34351166180215288*z^2)/7131724328013535 + (352922208800606144*z)/7131724328013535 + 1379250971773894912/7131724328013535, z, 3)
The first root, which is 0, is displayed clearly. Is it possible to make the other three roots appear as numbers as well? I looked for something about this in the documentation for the solve function, but did not find it.

ODE solver using Lobatto IIIA with table of coefficients

So I'm trying to figure out how to solve a given equation y'=-y+2te^(-t+2) for t in [0,10], step of 0.01 and y(0)=0.
I am supposed to solve it using the Lobatto IIIA method following a Butcher tableau:
Coefficients table
So far, this is what I got:
function lob = Lobatto_solver()
h = 0.01;
t = 0:h:10;
y = zeros(size(t));
y(1) = 0;
f = #(t,y) -y + (2*t*(exp(-t+2)));
% Lobatto IIIA Method
for i=1:numel(y)-1
f1 = f(t(i), y(i));
f2 = f(t(i)+(1/2)*h, y(i) + (5/24)*h*f1 + (1/3)*h*f2 + (-1/24)*h*f3);
f3 = f(t(i)+h, y(i) + (1/6)*h*f1 + (2/3)*h*f2 + (1/6)*h*f3);
y(x) = y(i) + h*((-1/2)*f1 + (2)*f2 + (-1/2)*f3);
end
end
It obviously makes no sense from the point when I equal f2 to itself, when the variable is still undefined.
Any help would be much appreciated :)
Cheers
You will need a predictor-corrector loop, in the simple case the corrector uses the slope-equations as basis of a fixed-point iteration. In the code below I use also the value of an explicit Euler step, in principle you could initialize all slopes with f1.
function lob = Lobatto_solver()
h = 0.01;
t = 0:h:10;
y = zeros(size(t));
y(1) = 0;
f = #(t,y) -y + (2*t*(exp(-t+2)));
% Lobatto IIIA Method
for i=1:numel(y)-1
f1 = f(t(i), y(i));
f3 = f(t(i)+h, y(i)+h*f1)
f2 = (f1+f3)/2;
for k=1:3
f2 = f(t(i)+(1/2)*h, y(i) + (5/24)*h*f1 + (1/3)*h*f2 + (-1/24)*h*f3);
f3 = f(t(i)+h, y(i) + (1/6)*h*f1 + (2/3)*h*f2 + (1/6)*h*f3);
end;
y(i+1) = y(i) + h*((-1/2)*f1 + (2)*f2 + (-1/2)*f3);
end
plot(t,y,t,0.05+t.^2.*exp(-t+2))
end
The graph shows that the result (blue) is qualitatively correct, the exact solution curve (green) is shifted so that two distinct curves can be seen.

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)

Is this function handle too long for MATLAB to integrate?

I have written the following code in order to try to plot the following integral against values of r, but MATLAB gives me an error -- is fun too long? am I going wrong somewhere else?
figure(1); %f_1
r = 0:0.001:50;
q = zeros(1, size(r));
for m = 1:numel(r)
fun = #(t) ((-3*(r(m).^3)*sin(3*t) + 2*(r(m)^2)*cos(2*t) + 7*r(m)*cos(t) -2*sin(t))*(-6*(r(m)^3)*sin(3*t) + 2*(r(m)^3)*cos(3*t) - 3*(r(m)^4)*cos(4*t) - 2*(r(m)^3)*sin(3*t) + 2*(r(m)^2)*cos(2*t) + 7*(r(m)^2)*sin(2*t))) - ((3*(r(m).^3).*cos(3*t) + 2*(r(m).^2).*sin(2*t) + 7*r(m).*sin(t) - 2*r(m).*cos(t))*(-6*(r(m).^3).*cos(3*t) + 2*(r(m).^3).*sin(3*t) + 3*(r(m).^4).*sin(4*t) - 2* (r(m).^3).*cos(3*t) - 2*(r(m).^2).*sin(2*t) + 7*(r(m).^2).*cos(2*t)))./((-3*(r(m).^3).*sin(3.*t) + 2*(r(m).^2)*cos(2.*t) + 7*r(m).*cos(t) - 2*sin(t)).^2 + (3*(r(m).^3).*cos(3*t) + 2*(r(m).^2).*sin(2*t) + 7*r(m).*sin(t) - 2*r(m).*cos(t)).^2);
q(m) = quad(fun, 0, 2*pi);
end
The error I get is
Error using * Inner matrix dimensions must agree.
Error in #(t)......
Error in quad (line 76) y = f(x, varargin{:});
I'll show you a way you may proceed, based on a retained r and fun (I did not pick all the terms of the native function):
r = 0:0.1:50;
q = zeros(size(r));
for ii = 1:numel(r)
fun = #(t) (-3.*(r(ii).^3).*sin(3.*t) + 2.*(r(ii).^2).*cos(2.*t) + 7.*r(ii).*cos(t) -2.*sin(t));
q(ii) = quad(fun,0,2.*pi);
end
Since your r is quite huge (50001 elements if I remember right), I'd go for parfor insted of simple for loop, too.
EDIT
As alternative solution, you could achieve the same results without any anonymous function, by following this way:
r = 0:.01:50;
fun1 = zeros(size(r));
t = 0:.001:(2*pi);
for ii = 1:numel(r)
fun1(ii) = trapz(t,(-3.*(r(ii).^3).*sin(3.*t) + 2.*(r(ii).^2).*cos(2.*t) + 7.*r(ii).*cos(t) -2.*sin(t)));
end

Matlab solve gives answer that isn't correct when calculated by hand

smys x y A;
L = A*(-2*x -y -3) + 2*x^2 - 2*x*y + 3*y^2;
Lx = diff(L, x);
Ly = diff(L, y);
LA = diff(L, A);
a = solve(Lx, Ly, LA);
a = [a.x, a.y, a.A]
a =
[ -7/6, -2/3, -5/3]
Now this piece looks ok and doing
subs(Lx, {x,y,A}, a)
Gives the expected 0, however, if I were to take the values and manually calculate the Lx/Ly/LA I get anything but 0. Why is it so?
It is correct. If I didn't break the calculations it looks like
Lx = -2*A+4*x-2*y
and thus
Lx = -2*(-5/3)+4*(-7/6)-2*(-2/3) = 10/3 - 14/3 + 4/3 = 0
The same holds for Ly and LA.