Why does using `solve` with a multivariate system of equations error unexpectedly? - matlab

While trying to solve a system of equations with 2 variables and 2 unknowns (Izhikevich nullclines), I encountered an unexpected error: Warning: 4 equations in 2 variables. and Warning: Explicit solution could not be found.
This is unexpected because as I stated, I was providing only 2 equations with the 2 variables, which should be a well-formed system of equations.
My relevant lines of code are as follows:
syms uu vv
[solvv, soluu] = solve([0.04*vv^2 + 5*vv + 140 - uu + I(t) == 0, a(t)*(b(t)*vv - uu) == 0], [vv, uu]);
The complete error trace is:
Warning: 4 equations in 2 variables.
\> In C:\Program Files\MATLAB\R2012b\toolbox\symbolic\symbolic\symengine.p>symengine at 54
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 160
In Q3_new at 37
In run at 64
Warning: Explicit solution could not be found.
\> In solve at 169
In Q3_new at 37
In run at 64
Confused, I went to MATLAB's documentation for solve and tried using the example snippet for solving a multivariate system of equations:
syms u v
[solv, solu] = solve([2*u^2 + v^2 == 0, u - v == 1], [v, u])
The expected output of this snippet, according to the documentation, is:
solv =
- (2^(1/2)*1i)/3 - 2/3
(2^(1/2)*1i)/3 - 2/3
solu =
1/3 - (2^(1/2)*1i)/3
(2^(1/2)*1i)/3 + 1/3
but the snippit instead returns:
Warning: 4 equations in 2 variables.
\> In C:\Program Files\MATLAB\R2012b\toolbox\symbolic\symbolic\symengine.p>symengine at 54
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 160
Warning: Explicit solution could not be found.
\> In solve at 169
solv =
[ empty sym ]
solu =
[]
as before.
Now I know I'm not making some beginner's mistake with my code because even the example code errors in the same way. Calling the singlevariate example snippit works as expected. I have tried this with MATLAB 2012a and MATLAB 2014a.
What could explain this unusual behaviour?

Can duplicate this on MATLAB 2014a. I found that if I already defined the variables using syms you can let solve resolve the variables automatically.
syms u v
[sv, su] = solve([2*u^2 + v^2 == 0, u - v == 1], [v, u]) % Doesn't work
% works but order-unspecified so this is not desirable
[su, sv] = solve([2*u^2 + v^2 == 0, u - v == 1])
Another user points out a mistake in using the incorrect documentation. MATLAB 2014a uses the following notation instead for re-ordered solutions. The other form seems to be for 2015. You should probably verify this holds true in 2012a but it seems to do so
syms u v
[sv, su] = solve([2*u^2 + v^2 == 0, u - v == 1], v, u)

Related

Matlab, cannot turn sym to double

I am trying to solve a variable in an equation (syms x), I've simplified the equation. I am trying to store the value in P_9, a 1x1000 matrix by converting from a symbol to a double and am getting the error below. It is giving me a symbol of 0x0, which is where I think my error lies.
Please help me troubleshoot my code. Many thanks!
number = 1000;
P_9 = zeros(1,number);
A_t=0.67;
A_e = linspace(0,10,number);
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = solve(eqn,x);
P_9(n) = double(solx);
end
Warning: Explicit solution could not be found.
In solve at 179
In HW4 at 74
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in HW4 (line 76)
P_9(n) = double(solx);
You certainly have an equation, where x can't be isolated.
For example it is impossible to isolate x in tan(x) + x == 1. So if you try to solve this equation analyticaly, matlab will tell you that x can't be isolated and therefore there is no explicit analytical solution.
So instead of using an analytical method to solve your equation, you need to use a numerical method, it's less "sexy" but this time you will be able to solve your equation.
Life is well done, matlab already integrate a numerical solver: vpasolve.
So your code will look like:
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = vpasolve(eqn,x);
P_9(n) = double(solx);
end

MATLAB : Solving non-polynomial equations numerically

I have an equation:
9.73*10^(3)*(x-y)/log((296-y)/(296-x)) - 15 = 0
1/(1+((x^2-3^2)*(x-3))^(-1))((x-y)/(log((x-3)/(y-3)))
and I tried to solve it for x in terms of y. I tried doing it both symbolically and numerically using functions solve, fsolve and vpasolve. Both failed (symbolic solver returned a conditional solution and the numerical one returned an empty object).
Any tips on solving an equation like that?
Edit:
Actual code (note some extra constants in the equations, but the overall structure of the equations remains the same) and the output:
syms T_i T_o
eqn1 = (9.73*10^(3))*(T_o - T_i)/(log((296-T_i)/(296-T_o))) - 15 == 0;
eqn2 = 1/(1.65*10^(-5)+1/(0.9*5.67*10^(-8)*0.01*(T_o^3-3^3)*(T_o-3)))*...
((T_o-T_i)/log((T_o-3)/(T_i-3))) - 15 == 0;
S = vpasolve([eqn1,eqn2],[T_o,T_i])
OUTPUT:
S =
T_o: [0x1 sym]
T_i: [0x1 sym]
i.e. no solutions were found. I tried solving for one variable and then insert it into the second equation, but this also fails (gives a conditional solution):
syms T_i T_o
eqn = (T_o - T_i)/(log((296-T_i)/(296-T_o))) == 15;
solT = solve(eqn,T_i);
OUTPUT:
Warning: The solutions are valid under the following conditions:
15*wrightOmega(T_o/15 - log(15/(T_o - 296)) - 296/15) + 296 ~= T_o.
To include parameters and conditions in the solution,
specify the 'ReturnConditions' option.
> In solve>warnIfParams (line 507)
In solve (line 356)

Solving an equation involving integral in Matlab

How to solve following question in Matlab.
Let α ∈ (−∞,∞).
Then how to solve: 1 + 4 {D1(α)−1} / α = 0.4615 in MATLAB?
where D1(α) is Debye function defined by:
D1(α) = 1/α ∫t/(et−1)dt
(integral from [0 α])
MATLAB won't give you an exact answer for this but you could use.
syms alpha t real
D = #(alpha)1/alpha * int(t / (exp(t)-1), t, 0, alpha);
solve(1 + 4*(D(alpha) - 1)/alpha == 0.4615, alpha)
output is
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
> In solve (line 303)
ans =
5.0770060488781283390761360077113
Edit: After the LaTeX was interpreted and posted I realized I read it wrong. I missed a pair of parenthesis. This issue has been fixed.

how can I solve a redundant symbolic system in matlab?

let
n0 =
nx*cos(a) + nz*cos(b)*sin(a) + ny*sin(a)*sin(b)
ny*cos(b) - nz*sin(b)
nz*cos(a)*cos(b) - nx*sin(a) + ny*cos(a)*sin(b)
in a and b,with the ns fixed (but of course,not assigned) values.
if I do
[a,b]=solve(n0-[1 0 0]',a,b,'IgnoreAnalyticConstraints',true)
i get
Error using solve>assignOutputs (line 257)
3 variables does not match 2 outputs.
Error in solve (line 193)
varargout = assignOutputs(nargout,sol,sym(vars));
then I wonder ''3 variables''?
Then I try
>> [a,b,c]=solve(n0-[1 0 0]',a,b,'IgnoreAnalyticConstraints',true)
that's the response
a =
cos(a)/(cos(a)^2 + sin(a)^2)
b =
(sin(a)*sin(b))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
c =
(cos(b)*sin(a))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
what is it doing? what's in c? I suppose he's solving with respect to nx ny nz,but why?every time I try to solve a problem with n+k equation in n variables I get strange errors,even if the rank of the system is just n.
that means even a=2 b=3 a+b=5 gives me problems.
how can I fix that?
I also cannot replicate the "Error in solve" error. What version of Matlab are you using? Also, I think some of the error message is missing – always list the entire error message. In any case, R2013a, solve does not find any solutions. Mathematica 9's Solve also does not find any.
I suspect why #DanielR and I can't exactly reduce your issue in the second case is that you may have a mistake in one of your lines above – it should be:
[a,b,c] = solve(n0-[1 0 0]','IgnoreAnalyticConstraints',true)
that produces
a =
cos(a)/(cos(a)^2 + sin(a)^2)
b =
(sin(a)*sin(b))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
c =
(cos(b)*sin(a))/((cos(a)^2 + sin(a)^2)*(cos(b)^2 + sin(b)^2))
What are the outputs a, b, and c (these simplify to cos(a), sin(a)*sin(b), and sin(a)*cos(b), by the way)? A big hint is that all of the solutions are in terms of your original variables a and b, but not nx, ny, or nz. When you don't specify which variables to solve for solve picks them. If you instead return the solutions in structure form, the nature of the output is made clear:
s = solve(n0-[1 0 0]','IgnoreAnalyticConstraints',true)
s =
nx: [1x1 sym]
ny: [1x1 sym]
nz: [1x1 sym]
But I think that you probably want to solve for a and b as a function of nx, ny, and nz, not the other way around. You're not correct about using solve to find solutions to overdetermined systems. Even when you have more equations then unknowns this is not always possible with nonlinear equations. If you can introduce some assumptions or even additional equations or specify numerical values for any of the nx, ny, or nz variables, solve may be able to separate and invert the equations.
And you shouldn't really use the term "rank" except for linear systems. In the case of the linear system example that you gave solve works fine:
[a,b] = solve([a==2 b==3 a+b==5],a,b)
or
[a,b] = solve(a==2,b==3,a+b==5,a,b)
or
[a,b] = solve([1 0;0 1;1 1]*[a;b]==[2;3;5],a,b)
returns
Warning: 3 equations in 2 variables.
> In /Applications/MATLAB_R2013a.app/toolbox/symbolic/symbolic/symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
a =
2
b =
3

comparing MATLAB fmincon and ga (genetic algorithm) results: issue with ga

I have a fairly complex optimization problem set up that I've solved through fmincon by calling it like this
myfun = #(x5) 0.5 * (norm(C*x5 - d))^2 + 0.5 * (timeIntervalMeanGlobal * powerAbsMaxMaxGlobal * sum(x5(28:128),1))^2;
[x5, fval] = fmincon(myfun, initialGuess, -A, b, Aeq, beq, lb, []);
The components are far to long to print here, but here are the dimensions
C: 49 x 128
x5: 128 x 1
d: 49 x 1
timeIntervalMeanGlobal, powerAbsMaxMaxGlobal: constants
initialGuess: 128 x 1
A: 44541 x 128
b: 44541 x 1
Aeq: 24 x 128
beq: 24 x 1
lb: 128 x 1
This works in code, but I don't get results that I'm completely happy with. I'd like to compare it with the built in ga function in MATLAB, which is called in a similar way, but I get an error when I try to run it like this
[x5, fval] = ga(myfun, nvars, -A, b, Aeq, beq, lb, []);
where nvars = 128. There's a long list of about 8 errors starting with
??? Error using ==> mtimes
Inner matrix dimensions must agree.
and ending with
Caused by:
Failure in user-supplied fitness function evaluation. GA cannot continue.
Can someone please instruct me on how to call ga properly, and give insight on why this error might occur with the ga call when the same code doesn't cause an error with fmincon? I've tried all the MATLAB help files and examples with a few different permutations of this but no better luck. Thanks.
UPDATE: I think I found the problem but I don't know how to fix it. The ga documentation says "The fitness function should accept a row vector of length nvars". In my case, myfun is the fitness function, but x5 is a column vector (so is lb). So while mathematically I know that C*x5 = d is the same as x5'*C' = d' even for non-square matrices, I can't formulate the problem that way for the ga solver. I tried - it makes it past the fitness function but then I get the error
The number of rows in A must be the same as the length of b.
Any thoughts on how to get this problem in the right format for the solver? Thanks!
Got it! I just had to manipulate the fitness function to make it use x5 as a row vector even though it's a column vector in all the constraints
myfun = #(x5) 0.5 * (norm(x5 * C' - d'))^2 + 0.5 * (timeIntervalMeanGlobal * powerAbsMaxMaxGlobal * sum(x5(28:128)))^2;
Phew!