MATLAB : Solving non-polynomial equations numerically - matlab

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)

Related

Matlab - vpasolve error when solving system of equations

I'm currently trying to solve a system of equations, where h2, eta, B, U are known but I have left them as symbolic variables for now:
clearvars
clc
syms x x0 h0 h1 h2 C2 eta B U
h(x) = h0 + h2*(x/(B/2))^2;
h_bar(x) = h(x)/h0;
hc_bar = 1+(h2/h0)*(x0/(B/2))^2;
x_bar(x) = x/(B/2);
x0_bar = x_bar(x0);
integration = int(((h_bar^2-hc_bar^2)/(h_bar^3)),x);
p(x) = ((3*eta*U*B)/(h0^2))*integration+C2;
p_d(x) = diff(p(x),x);
p_fun1 = p(x0_bar) == 0;
p_fun2 = p_d(x0_bar) == 0;
p_fun3 = p(-1) == 0;
p_fun4 = h1-h0 == h2;
Four equations, four unknowns so I should be able to solve this. However the system of equations is a bit overwhelming so I decided to try my luck with vpasolve:
vpasolve([p_fun1, p_fun2, p_fun3, p_fun4],[x0, h0, h1, C2])
This results in the following error:
Error using mupadengine/feval (line 166)
Symbolic parameters are not allowed in nonpolynomial equations.
Error in sym/vpasolve (line 172)
sol = eng.feval('symobj::vpasolve',eqns,vars,X0);
Error in Yke (line 35)
vpasolve([p_fun1, p_fun2, p_fun3, p_fun4],[x0, h0, h1, C2])
I have traced the error back to p_fun1 specifically, but I cannot see why this specific function would trigger the error. It is complicated, but p_fun2 is the derivative of p_fun1 and p_fun3 is simply p_fun1 with a different value substituted in the equation, yet these do not trigger the error.
I'm pretty sure the error is caused by replacing x with x_bar in the p_fun1 = p(x0_bar) == 0 call, but I don't know why and therefore I also don't know how to solve it. Does anyone see what I'm doing wrong at the moment?
vpasolve is a numerical solver (in fact, vpa stands for variable-precision arithmetic), and you cannot have symbolic parameters in the equations.
You should thus substitute numerical values to all the equation parameters.

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

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.

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

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)

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