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?
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.
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)
My question is similar to this question but I believe it to be more general.
I use Matlab's symbolic math toolbox to solve an equation:
MAZ = 0.5;
MAU = 1.0;
XI = 1.0;
ALPHA = 2.0;
DRG = 0.5;
SRG = 1.0;
PHI = 1 / (2 * MAU);
syms L;
f = 1 - DRG - sqrt(1 + (ALPHA * XI - L / (2 * XI * PHI) ) ^ 2 ) / ...
sqrt(1 + (ALPHA * XI) ^ 2) + L / (4 * PHI * SRG * sqrt(1 + (ALPHA * XI)^2));
a = solve(f,L,'Real',true);
The answer is:
a =
5^(1/2)/3 + (10*((4*5^(1/2))/25 + 6/25)^(1/2))/3 + 8/3
5^(1/2)/3 - (10*((4*5^(1/2))/25 + 6/25)^(1/2))/3 + 8/3
How do I automatically convert these expressions for a - which do not contain any symbolic expressions - to floats so that I can access them later in my code?
Matlab's symbolic math toolbox contains a function called double.
This function converts the result of the solve function - which is a sym - to a double:
double(a)
ans =
5.98921078320145
0.834834535131742
While double is correct, it is limited to the precision of 64bit floating points. Use vpa if a higher precision is needed:
>> vpa(a)
ans =
5.9892107832014511063435699584181
0.83483453513174202459587915406938
>> digits(99)
>> vpa(a)
ans =
5.98921078320145110634356995841813862213621375395128614574627036653958858547362556353272837962692249
0.834834535131742024595879154069378868157531819123064337100994463734092031618244369410214559292265698
I´m trying to solve this set of equations in MATLAB and I get an empty sym object:
equations = {'I2B+I2EQAB=I22B+I2EQBC',...
'I2A=I2EQAB+I2EQAC+I22A',...
'I2C+I2EQBC+I2EQAC=I22C',...
'I22B=IZB+IC1B',...
'IZB=IC2B+IZBB',...
'I22C=-I2C*Z2C*YC/2+IZC',...
'IZC=IC2C+IZCC',...
'I22A=IC1A+IZA1',...
'IC4A+IZA2=IZBB+IZCC',...
'IZB*Z2LB+IC2B*2/YB=IC1B*2/YB',...
'I2C*Z2C=-IC2C*2/YC+IZC*Z2LC',...
'IZA1*m*Z2LA+IC2A*2/(m*YA)=IC1A*2/(m*YA)',...
'IC4A*2/((1-m)*YA)=IC2A*2/(m*YA)+IZA2*(1-m)*Z2LA',...
'I2EQBC*Z2EQBC+IZC*Z2LC=IZB*Z2LB',...
'I2B*Z2B+IC1B*2/YB',...
'I2C*Z2C+IC1C*2/YC',...
'I2A*Z2A+IC1A*2/(m*YA)',...
'IZB*Z2LB+(1-m)*Z2LA*IZA2=IZA1*m*ZL2A-I2EQAB*Z2EQAB',...
'IZA1*m*Z2LA=IZA2*(1-m)*Z2LA+IZC*Z2LC+I2EQAC*Z2EQAC',...
'IC4A/((1-m)*YA)=IC2C/YC'};
variables = {'m','I2A','I2B','I2C','I2EQAB','I2EQAC','I2EQBC',...
'IZA1','IC1A','IC2A','IZA2','IC4A','IC1B','IZB',...
'IC2B','IZBB','IZC','IC2C','IZCC'};
LL = solve(equations{:},variables{:})
Can you help me figure out what's going wrong?
Warning: 20 equations in 19 variables.
> In solve at 139
Warning: Explicit solution could not be found.
> In solve at 170
LL =
[ empty sym ]
I think that's self explanatory, if not check out the documentation related to DSOLVE where:
Diagnostics If dsolve cannot find an
analytic solution for an equation, it
prints the warning: Warning: Explicit
solution could not be found. and
returns an empty sym object.
solve() has issues with variable names in capital letters, as yours.
Please refer to http://www.mathworks.com/matlabcentral/newsreader/view_thread/303201
I tried reformatting the equations and inputting directly into the symbolic toolbox, and the solve function just spits out all the equations, so it cannot solve for those variables as the current equations stand.
Do you have any knowledge about the domains or constraints for all those variables? If you do I'd look at specifying all those, perhaps it would allow the solver to find a solution for you.
To get you quickly up and running in the symbolic toolbox, here's your equations reformatted to fit:
equations := {
I2B + I2EQAB = I22B + I2EQBC,
I2A = I2EQAB + I2EQAC + I22A,
I2C + I2EQBC + I2EQAC = I22C,
I22B = IZB + IC1B,
IZB = IC2B + IZBB,
I22C = -I2C * Z2C * YC / 2 + IZC,
IZC = IC2C + IZCC,
I22A = IC1A + IZA1,
IC4A + IZA2 = IZBB + IZCC,
IZB * Z2LB + IC2B * 2 / YB = IC1B * 2 / YB,
I2C * Z2C = -IC2C * 2 / YC + IZC * Z2LC,
IZA1 * m * Z2LA + IC2A * 2 / (m * YA) = IC1A * 2 / (m * YA),
IC4A * 2 / ((1 - m) * YA) = IC2A * 2 / (m * YA) + IZA2 * (1 - m) * Z2LA,
I2EQBC * Z2EQBC + IZC * Z2LC = IZB * Z2LB,
I2B * Z2B + IC1B * 2 / YB,
I2C * Z2C + IC1C * 2 / YC,
I2A * Z2A + IC1A * 2 / (m * YA),
IZB * Z2LB + (1 - m) * Z2LA * IZA2 = IZA1 * m * ZL2A - I2EQAB * Z2EQAB,
IZA1 * m * Z2LA = IZA2 * (1 - m) * Z2LA + IZC * Z2LC + I2EQAC * Z2EQAC,
IC4A / ((1 - m) * YA) = IC2C / YC
}:
variables := {
m, I2A, I2B, I2C, I2EQAB, I2EQAC ,I2EQBC,
IZA1, IC1A, IC2A, IZA2, IC4A, IC1B, IZB,
IC2B, IZBB, IZC, IC2C, IZCC
}:
solve(equations, variables)
To specify that all your known variables are real numbers, use this command:
assume(variables, Type::Real)
Also note that I count 36 unique variables (unless I made a mistake) in the equations, you'd be getting a huge list of "what-if's" for those equations if the solver was able to produce a result. I'd look at your equations and see if you could group them out and solve them in smaller sets.
Matlab, symbolic solve: solve()
I think has issues with symbolic variables who's names are more than one character.
a-z works, but anytime i try to solve something with two letters or more it just spits
back out the empty set.
For instance, something as simple as solve('xy*10 = 1', 'xy') doesn't work :(