converting long decimal to scientific notation in symbolic expression Matlab - matlab

I know that if I want to convert a numerical expression to scientific notation, I shall use eps(). However, I have a symbolic expression where the results shall be displayed with scientific notation. For example:
the expression is stored in result which is:
result = 0.000002*x^2 + 0.000005*x + 0.000001
But, the desired result is:
2.0e-6*x^2 + 5.0e-6*x + 1.0e-6
When applying
result = eps(result);
I am getting this error:
Error using eps
Class must be 'single' or 'double'.
Notice that I have used syms x; to generate the previous expression.
My experience in Matlab is very shallow. Sorry if this question is so basic.

I have found the solution while searching. The solution is found here.
For short. It is by using vpa(result,k) where k is the number of significant digits.

Related

Matlab symbolic function conversion without dot for matrix operation

When converting symbolic expression to matlabFunction, expression like
x=sym('x')
f=- x^3/6 + x
g=matlabFunction(f)
-> #(x)x-x.^3.*(1.0./6.0)
which is not what I want because x is gonna be a matrix and my application requires actual matrix multiplication such as x^3 instead of the dot product form of x.^3
The only way to get it working is to use anonymous function, i.e.
g=#(x) - x^3/6 + x
->#(x)-x^3/6+x
However, the issue with anonymous function is that I cannot use substitution but to type the entire formulation, i.e.
g=#(x) f
-> #(x)f which shows that expression substitution does not work
In short, I will need to solve either one of the technical difficulties: (1) If I use matlabFunction, how do I remove all the dot after the conversion? or (2) If I use anonymous function, how do I bypass typing the symbolic expression if I have already defined 'f' for the expression?
I am totally lost here and I hope someone familiar with matlab can give me 2 cents.
Thank you!
You can convert the sym object to a string when calculating the anonymous function:
g=#(x)eval(char(f))
Alternatively, you can use the following code
h=eval(['#(x)' char(f)])
instead of matlabFunction

Matlab: trouble converting symbolic expression to numeric

I've noticed that after the substitution of numeric 'float' values in a symbolic matrix, the conversion is in a fractional form with very large values, that I want to avoid:
A=(uvw(1,:)-bMat(1,:)).^2+(uvw(2,:)-bMat(2,:)).^2+(uvw(3,:)-bMat(3,:)).^2-legStroke.^2;
A=subs(A,[x,y,z,phi,theta,psi],...
[1.37,0.0,0.0,degtorad(0.0),degtorad(-1.32),degtorad(0.0)]);
A=simplify(A)
and I get as result the following:
A=[((9004809005642893*p1x)/9007199254740992 - b1x + 137/100)^2-l1^2 +(44086462975326147772185208371001*p1x^2)/83076749736557242056487941267521536 + ...
I tried to use sym(A,'d') or similar, following some tips from the web, but I got this error message:
Error in sym>tomupad (line 2232)
assumptions(S,x.s,a);
You cannot apply sym(A,'d') if A is already a symbolic expression (i.e., in terms of unknown symbolic variables, class sym or symfun). The purpose of sym(A,'d') is to convert a numeric values and arrays into symbolic form.
You have two options.
1. Use something like sym(A,'d') to convert your numeric values to inexact symbolic decimal form before they are incorporated into your symbolic expressions. This will result in a loss of accuracy in some cases.
2. Convert your exact symbolic expression to floating-point form at the very end using variable precision arithmetic, i.e., vpa(A). This method should be more accurate as your symbolic calculations will be be done exactly (you still need be careful how you do your initial conversion to symbolic, however).

Why does "pi" become symbolic in MATLAB?

In the Matlab command window I type:
syms f;
s = 2*pi*f*j;
s
which returns
s =
pi*f*2*j
Why is pi is not calculated as 3.141592...?What's wrong with the code I entered into the command window?
Welcome to symbolic math where you get exact answers as opposed to floating-point approximations. If you just want to "get a number" you can use non-symbolic functions and operations or you can convert symbolic results back to floating-point.
For example:
syms f
s = pi*f*2*j
s2 = subs(s,f,2)
s3 = double(s2)
Alternatively, you can use variable precision arithmetic to represent pi as a decimal approximation of a specified level in a symbolic expression:
syms f
s = vpa(pi)*f*j
See the documentation for vpa for further details. You can also use the sym function to achieve similar things.
However, you can lose some of the power of symbolic math if you convert to a decimal or floating point representation too soon. For example, compare the difference between the following expressions:
sin(pi) % 1.224646799147353e-16
sin(vpa(pi)) % -3.2101083013100396069547145883568e-40
sin(sym(pi)) % 0, sin(sym(1)*pi) and sin(sym(pi,'r')) also return zero
Only the last one will be fully cancelled out of an expression, thus simplifying it.

Format of MatLab Integration Result

I have been trying to integrate a function with matlab but it keeps giving me strange
results.
>> syms w x;
>> w = 0.8335*(cosh(52.42*x)-cos(52.42*x)-sinh(52.42*x)+sin(52.42*x));
>> int(w, 0, 1)
>> (1667*sinh(2621/50))/104840 ... /*Some long expression*/
Instead of giving me some final numerical value it leaves the integration with sinh(2621/50). Am I using the wrong functions? Any help would be great.
If you want to get the numerical value you can just evaluate the resulting expression.
Note that sinh(2621/50) is just a number.
I do not have access to the symbolic toolbox, but if you want precise control of the output it seems like the function vpa() (mathworks variable-precision arithmetic) is an alternative to evaluating the expression.
For example vpa(int(w, 0, 1),5) would give you 5 significant digits.

MATLAB: computations involving large numbers

How can one perform computations in MATLAB that involve large numbers. As a simple example, an arbitrary precision calculator would show that ((1/120)^132)*(370!)/(260!) is approximately 1.56, but MATLAB is not able to perform such a computation (power(120,-132)*factorial(370)/factorial(260) = NaN).
I have also tried the following, which does not work:
syms a b c d;
a=120; b=-132; c=370; d=260;
f=sym('power(a,b)*gamma(c+1)/gamma(d+1)')
double(f); % produces error that instructs use of `vpa`
vpa(f) % produces (gamma(c + 1.0)*power(a, b))/gamma(d + 1.0)
If you just want to calculate the factorial of some large numbers, you can use the Java arbitrary precision tools, like so:
result = java.math.BigDecimal(1);
for ix = 1:300
result = result.multiply(java.math.BigDecimal(ix));
end
disp(result)
306057512216440636035370461297268629388588804173576999416776741259476533176716867465515291422477573349939147888701726368864263907759003154226842927906974559841225476930271954604008012215776252176854255965356903506788725264321896264299365204576448830388909753943489625436053225980776521270822437639449120128678675368305712293681943649956460498166450227716500185176546469340112226034729724066333258583506870150169794168850353752137554910289126407157154830282284937952636580145235233156936482233436799254594095276820608062232812387383880817049600000000000000000000000000000000000000000000000000000000000000000000000000
The value result in this case is a java object. You can see the available methods here: http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html
I'm still not sure that I would trust this method for (1e6)! though. You'll have to experiment and see.
Depending on what you're trying to do, then you may be able to evaluate the expression you're interested in in log-space:
log_factorial = sum(log(1:300));
You can use Stirling's approximation to approximate large factorials and simplify your expression before computing it numerically.
This will work:
vpa('120^-132*370!/260!')
and the result is
1.5625098001612564605522837520443