Solving inequalities in matlab - 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)

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.

Checking Symbolic Equation Equality in Matlab

How can one check if symbolic eq1 is equal to symbolic eq2 in Matlab:
syms a x y
eq1 = a*(x+y)
eq2 = a*x + a*y
You could use simplify on the difference, then logical to test if it is equal to 0:
eqAreEqual = logical(simplify(eq1-eq2) == 0);
You could also use simplify on each and isequal for comparison:
eqAreEqual = isequal(simplify(eq1), simplify(eq2));
For example:
>> syms a x y
>> eq1 = a*(x+y);
>> eq2 = a*x + a*y;
>> eqAreEqual = logical(simplify(eq1-eq2) == 0)
eqAreEqual =
logical
1 % True!
>> eqAreEqual = isequal(simplify(eq1), simplify(eq2))
eqAreEqual =
logical
1 % Also true!
For symbolic math and Matlab R2012a+, in general it's best to use isAlways to check if an equality or inequality holds. For your example,
isAlways(eq1==eq2)
returns logical true (1). The isAlways function considers assumptions and can gracefully handle undecidable conditions. The solution from #gnovice can be used in older versions of Matlab, but will fail by throwing an error when an expression can't be proved, e.g., logical(simplify(2*x >= x)).

Dot product of symbolic vector

I am trying to take the dot product of a symbolic vector and another vector. I did the following:
>> rac = sym('rac',[3 1])
rac =
rac1
rac2
rac3
>> i = [1;0;0]
i =
1
0
0
>> dot(rac,i)
ans =
conj(rac1)
However my desired outcome is rac1. Why is it not behaving like I want it to? And how do I achieve this output?
You need to specify that your symbolic vector is real:
rac = sym('rac', [3 1], 'real');
dot(rac, [1; 0; 0])
ans =
rac1

Unexpected result on solving some inequality in Matlab symbolic computation

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)