Rewrite a symbolic expression in terms of a specific subexpression - matlab

I need to rewrite a symbolic expression in terms of a specific subexpression.
Consider the following scenario:
expression f with 2 variables a, b
subexpression c = a / b
syms a b c
f = b / (a + b) % = 1 / (1 + a/b) = 1 / (1 + c) <- what I need
Is there a way to achieve this?
Edit:
The step from 1 / (1 + a/b) to 1 / (1 + c) can be achieved by calling
subs(1 / (1 + a/b),a/b,c)
So a better formulated question is:
Is there a way to tell MATLAB to 'simplify' b / (a + b) into 1 / (1 + a/b)?
Just calling simplify(b / (a + b) makes no difference.

Simplification to your desired form is not automatically guaranteed, and in my experience, isn't likely to be achieved directly through simplify-ing as I've noticed simplification rules prefer rational polynomial functions. However,
if you know the proper reducing ratio, you can substitute and simplify
>> syms a b c
>> f = b / (a + b);
>> simplify(subs(f,a,c*b))
ans =
1/(c + 1)
>> simplify(subs(f,b,a/c))
ans =
1/(c + 1)
And then re-substitute without simplification, if desired:
>> subs(simplify(subs(f,a,c*b)),c,a/b)
ans =
1/(a/b + 1)
>> subs(simplify(subs(f,b,a/c)),c,a/b)
ans =
1/(a/b + 1)

Related

Express an expression in terms of other expressions in MATLAB

Is there any way to express an expression in terms of other expressions in MATLAB?
For example, the following expressions have been written as sum (X + Y) and product (XY)
1/X + 1/Y = (X + Y)/XY
1/X^2 + 1/Y^2 + 2/(XY) = (X + Y)^2/(XY)
2*X/Y + 2*Y/X = 2*((X + Y)^2 - 2*X*Y)/(XY)
I know about the rewrite() function but I couldn't find how it can be used to do what I want?
There are a few different functions you can try to change the format of your symbolic expression:
collect: collects coefficients (can specify an expression to collect powers of):
>> collect(1/X + 1/Y)
ans =
(X + Y)/(Y*X)
simplify: perform algebraic simplification:
>> simplify(1/X^2 + 1/Y^2 + 2/(X*Y))
ans =
(X + Y)^2/(X^2*Y^2)
numden: convert to a rational form, with a numerator and denominator:
>> [n, d] = numden(2*X/Y + 2*Y/X)
n =
2*X^2 + 2*Y^2
d =
X*Y
>> n/d
ans =
(2*X^2 + 2*Y^2)/(X*Y)

How to convert a symbolic expression to a Octave function from the Symbolic Package?

How to convert a symbolic expression to a Octave function from the Symbolic Package?
After installing the symbolic package on octave with pkg install -forge symbolic. Using the symbolic package on octave I may write this:
octave> pkg load symbolic;
octave> a = sym( "a" );
octave> int ( a^2 + csc(a) )
which will result in:
ans = (sym)
3
a log(cos(a) - 1) log(cos(a) + 1)
-- + --------------- - ---------------
3 2 2
But how to do this Integral (int(1)) symbolic result just above to became a valuable function like this below?
function x = f( x )
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
end
f(3)
# Which evaluates to: 11.6463 + 1.5708i
I want to take the symbolic result from int ( a^2 + csc(a) ), and call result(3), to compute it at 3, i.e., return the numeric value 11.6463 + 1.5708i, from the symbolic expression integral a^2 + csc(a). Basically, how to use the symbolic expression as numerically evaluable expressions? It is the as this other question for Matlab.
References:
http://octave.sourceforge.net/symbolic/index.html
How do I declare a symbolic matrix in Octave?
Octave symbolic expression
Julia: how do I convert a symbolic expression to a function?
What is symbolic computation?
You can use pretty.
syms x;
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2;
pretty(x)
which gives this:
3
log(cos(x) - 1) log(cos(x) + 1) x
--------------- - --------------- + --
2 2 3
Update (Since the question is edited):
Make this function:
function x = f(y)
syms a;
f(a) = int ( a^2 + csc(a) );
x = double(f(y));
end
Now when you call it using f(3), it gives:
ans =
11.6463 + 1.5708i
it seems like you answered your own question by linking to the other question about Matlab.
Octave has an implementation of matlabFunction which is a wrapper for function_handle in the symbolic toolbox.
>> pkg load symbolic;
>> syms x;
>> y = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
y = (sym)
3
x log(cos(x) - 1) log(cos(x) + 1)
-- + --------------- - ---------------
3 2 2
>> testfun = matlabFunction(y)
testfun =
#(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2
testfun(3)
>> testfun(3)
ans = 11.6463 + 1.5708i
>> testfun([3:1:5]')
ans =
11.646 + 1.571i
22.115 + 1.571i
41.375 + 1.571i
>> testfun2 = matlabFunction(int ( x^2 + csc(x) ))
testfun2 =
#(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2
>> testfun2(3)
ans = 11.6463 + 1.5708i
>> testfun2([3:1:5]')
ans =
11.646 + 1.571i
22.115 + 1.571i
41.375 + 1.571i
I'm sure there are other ways you could implement this, but it may allow you to avoid hardcoding your equation in a function.

How to create, solve and plot conditional function in matlab

For example,
I have a
f(x)=
9+4(x+3), if -4<=x<-1 (subf1)
7-9(x-0.4), if -1<=x<1 (subf2)
How can I create a function of f(x) in matlab?
I tried
f=0
syms x
f=f+ subf1 with heaviside+ subf2 with heaviside
But I cannot give a v to solve f(v) and I cannot plot f(x) only from -4 to 1.
So is there another way to write conditional function?
Sorry my description is a little hard to follow. If you don't understand what I am asking, please let me know and I will try to rephrase. Thank you!
Depends on what you want to do with it. If for some reason you need symbolic, here is one way to write your symbolic function:
syms x
f1 = (9 + 4 * (x + 3)) * heaviside(x + 4) * (1 - heaviside(x + 1));
f2 = (7 - 9 * (x - 0.4)) * heaviside(x + 1) * (1 - heaviside(x - 1));
f = symfun(f1 + f2, x);
Otherwise, you can write your function in a file as:
function out = f(x)
out = (9 + 4 * (x + 3))*(x>=-4)*(x<-1) + (7 - 9 * (x - 0.4))*(x>=-1)*(x<1);
Or you can define it as an anonymous function:
f = #(x) (9 + 4 * (x + 3))*(x>=-4)*(x<-1) + (7 - 9 * (x - 0.4))*(x>=-1)*(x<1);
Then, you can plot any of the functions using, for instance, fplot:
fplot(f, [-4, 1])

How to simplify functions in matlab?

Hello Let's say I have theres two functions
F1= a*x^(2) + b
F2 = c*x
Where a, b and c are a constant and x is a variablem how do can I make matlab gives me a simplified version of F1*F2 so the answer may be
a*c*x^(3) + b*c*x
This is what I have in matlab
syms x a b c
F1 = a*x^(2) +b;
F2 = c*x^(2);
simplify(F1*F2)
ans =
c*x^2*(a*x^2 + b)
When I multiply in matlab it's just giving me (ax^(2) + b)(c*x)
Try this commands:
syms a x b c
F1= a*x^(2) + b
F2 = c*x
F=F1*F2
collect(F)
which will give you:
ans =
a*c*x^3 + b*c*x
The command collect is useful when working with polynoms. The opposite command is pretty. It will give you c*x*(a*x^2 + b)

Can someone explain the simplification of this boolean algebra equation?

I think I missed reading a theorem or postulate or something..
The equation is: wy + wxz + xyz
According to my professor, the simplification is (which she didn't explain how):
wy + xz(w'y + wy')
= wy + xz (w XOR y)
Where did that (w'y + wy') came from??
I tried to calculate it but so far I only got: (w+x)(w+y)(w+z)(x+y)(y+z)
In a Boolean expression + is XOR and * is AND. This is the same as identifying true with 1 and false with 0, with the only special convention that 1 + 1 = 0 (or 2 = 0 if you wish.)
With these definitions both + and * are commutative, i.e., a + b = b + a and a * b = b * a. In addition, the distributive law is also valid a * (b + c) = a * b + a * c. Note also that the * operator is usually implicit in the sense that we write ab instead of a * b.
Applying these properties to the expression wy + wxz + xyz, there are some few obvious transformations you can do:
wy + wxz + yxz (commute x with y)
wy + (w + y)xz (prev plus distribute xz)
wy + xz(w + y) (prev plus commute (w + y) with xz
Note that the last one is wy + xz(w XOR y) because + is nothing but XOR.
ADDENDUM
Regarding the expression of your professor, let's recall that a' = 1 - a by definition. So
w'y + wy' = (1 - w)y + w(1 - y) - def
= y - wy + w - wy - distribute
= y + w - simplify (a + a = 0 always)
= w + y - commute
which shows that s/he was right.