Unexpected result on solving some inequality in Matlab symbolic computation - matlab

Please consider this example. I would like to solve x^3 - 2x > 0. I try the following commands:
syms x;
f = #(x) x^3-2*x;
solve(f(x)>0,x)
and Matlab returns this
ans = solve([0.0 < x^3 - 2.0*x], [x])
which is not what I expect. Therefore I use
solve(f(x)+x>x,x)
which returns
ans = Dom::Interval(2^(1/2), Inf) Dom::Interval(-2^(1/2), 0)
Can someone explain that why solve works successfully only in the second case?

Try adding the Real option to solve:
solve(f(x)>0,x,'Real',1)
ans =
Dom::Interval(2^(1/2), Inf)
Dom::Interval(-2^(1/2), 0)

Related

How to solve for a system of equations in MATLAB when the variables are actually symbolic functions?

>> syms x v(x) w(x);
>> eq1 = 2*v + 3*w == 4;
>> eq2 = 5*v + 4*w == 3;
>> sol = solve([eq1,eq2],[v,w])
I tried to implement this code in MATLAB, but error flashes out as "The second argument must be a vector of symbolic variables." I have tried similar things in Python using SymPy, but never such error comes. How to correct out this?
Have a look at the help file for the multivariate case of solve and the example in the help file
openExample('symbolic/SolveMultivariateEquationsAndAssignOutputsToStructureExample')
Applied to your problem
syms v w;
eq1 = [2*v + 3*w == 4;5*v + 4*w == 3];
sol = solve(eq1)
sol.v
sol.w
but if you just want to solve for v w then you could use e.g.
[2 3;5 4]\[4;3]

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.

Solving inequalities in matlab

I'm trying to solve simple inequalities using matlab and got stuck.
solve(x^2>0,x)
ans =
-1
1
solve(x^2>5,x)
ans =
5^(1/2)+1
-5^(1/2)-1
which is totally wrong. What is the problem? My matlab version is R2014b.
If you are solving an equation, e.g. x^2 == 1, then you are looking for specific points, i.e. the points x=1 and x=-1. Luckily, MATLAB returns exactly this:
>> syms x
>> solve(x^2 == 1, x)
ans =
-1
1
Now, you are looking for solutions to an inequality. You really don't want to find all points for which the inequality holds, as this are infinitely many in your case. As stated in the documentation to solve, the 'ReturnConditions' flag will make solve return the conditions under which the (in-) equality holds.
>> syms x
>> S = solve(x^2>0,x, 'ReturnConditions', true)
>> S.conditions
ans =
x < 0
0 < x
or for the second example
>> syms x
>> S = solve(x^2 > 5, x,'ReturnConditions',true)
>> S.conditions
ans =
5^(1/2) < x
x < -5^(1/2)

Solve equation with known variables and one unknown

I'm trying to solve this equation in Matlab
dT=((-A-B*C+D*./E)
where C=sin(dT). dT is unknown. A, B, D, and E are known variables. Using Matlab's solve function:
Ans=solve(dT==((-gra-H_vap*m_lg+grb*./ro_cp),dT);
But I receive an error message. How do I solve this equation?
You haven't given us any specifics on the values of your known parameters, and I also believe that D*/E in your example were intended to be a more valid expression.
Anyway, here is an example of how you make use of the symbolic solver solve:
syms dT
A = 1
B = 2
D = [1 2]
E = [3 4]
eqn = -A - B*sin(dT) + D/E - dT == 0
soldT = solve(eqn,dT)
which produces the following output
% ...
eqn =
- dT - 2*sin(dT) - 14/25 == 0
% ...
soldT =
-0.18739659458654612052194305796251
See also the language docs for solve.

Error while solving system of equations

I received a negative MATLAB response while inserting the following requests:
syms x y z
solve (x+y==z,x-2*y==z,[x,y],'ReturnConditions', false)
I wanted to get x, and y in terms of z but, alas, I got the following error:
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 160
vc = char(v);
Error in ==> solve at 84
[eqns,vars] = getEqns(varargin{:});
Any help please?
Try using a cell array to wrap x and y instead:
>> syms x y z
>> X = solve (x+y==z,x-2*y==z,{x,y},'ReturnConditions', false);
>> X.x
ans =
z
>> X.y
ans =
0
This was required for previous versions of MATLAB. However, I'm using R2015a currently and your code works for me. I can't replicate your error. You may be using a previous version.
I think i found the hit-back to my problem.
As for solving system of two equations and two variables, we use a matrix of inputs, and outputs must be sprecified.
b=[x+y-z;x-3*y-z;]
b =
x + y - z
x - 3*y - z
[e t]=solve(b,x,y)
e =
z
t =
0
Thnk you all.