Checking Symbolic Equation Equality in Matlab - 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)).

Related

How to use matlab to quickly judge whether a function is convex?

For example, FX = x ^ 2 + sin (x)
Just for curiosity, I don't want to use the CVX toolbox to do this.
You can check this within some interval [a,b] by checking if the second derivative is nonnegative. For this you have to define a vector of x-values, find the numerical second derivative and check whether it is not too negative:
a = 0;
b = 1;
margin = 1e-5;
point_count = 100;
f=#(x) x.^2 + sin(x);
x = linspace(a, b, point_count)
is_convex = all(diff(x, 2) > -margin);
Since this is a numerical test, you need to adjust the parameter to the properties of the function, that is if the function does wild things on a small scale we might not be able to pick it up. E.g. with the parameters above the test will falsely report the function f=#(x)sin(99.5*2*pi*x-3) as convex.
clear
syms x real
syms f(x) d(x) d1(x)
f = x^2 + sin(x)
d = diff(f,x,2)==0
d1 = diff(f,x,2)
expSolution = solve(d, x)
if size(expSolution,1) == 0
if eval(subs(d1,x,0))>0
disp("condition 1- the graph is concave upward");
else
disp("condition 2 - the graph is concave download");
end
else
disp("condition 3 -- not certain")
end

Why does MATLAB solve not solve a linear equation?

I am trying to do a basic linear equation using the symbolic toolbox. I set my equation, two initial conditions, and solve, but I only get an empty result:
syms y(x) x m c
eqn = y(x) == m*x + c;
cond1 = y(x==0) == 0;
cond2 = y(x==1) == 1;
sol = solve(eqn,cond1,cond2,m,c)
It returns sol.c as 0 but sol.m as y(x)/x
You could transform your conditions into vectors:
a = [0;1];
b = [0;1];
syms m c
eqn = a.*m + c -b ==0;
sol = solve(eqn,m,c)
This will result in a scalar value for m

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 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)

Algorithm to find a value that gives the minimum output for two or equations

Suppose I have two equations with only one variable (free parameter) x and that k1 and k2 are constants. I would like to solve for:
f(x) + k1 = 0
&
g(x) + k2 = 0
...
h(x) + kn = 0
Of course there is no value of x that satisfies all of these equations. I basically would like the value of x that minimizes the output of each of these equations.
'solve' in matlab looks for an exact answer and returns an error, here's an example to demonstrate:
syms x
solution = solve(0.5*(x-k1)/sqrt(2) == 0, 0.5*(x-k2)/sqrt(2) == 0);
You can try using Unconstrained Optimization method such as fminsearch, for example:
h=#(x) x^2;
g=#(x) x^3;
k1=2;
k2=4;
inital_guess=3;
f = #(x) sum(abs([h(x)+k1; g(x)+k2]));
[x,fval] = fminsearch(f,inital_guess)
Note that I represent both eq in matrix form, and the minimization is by looking at the sum of their absolute values.
For the values I entered the value of x that minmize these eq is given by the output x = -1.5874