MatLab - 9.45 representation and round - matlab

>> a = 12.5 * 9.45
a =
1.181250000000000e+02
>> round(a * 100) /100
ans =
1.181200000000000e+02
The rounded value should be 118.13, not 118.12.
If you type 9.45 in MatLab command line, it can't be represented :
>> 9.45
ans =
9.449999999999999
If I set the numeric format to short, the final result is the same.
>> a = 12.5 * 9.45
a =
118.1250
>> round(a * 100) / 100
ans =
118.1200
Can someone explain that? Any workaround?

You could try something like John D'Errico's hpf class.
This will give the result you are expecting
round(hpf('12.5') * hpf('9.45') * 100)/100
ans =
118.13
F = hpf('9.45')
F =
9.45

Sounds like you've got some settings that are messing with your output precision. It looks like your current settings want 15 decimal places(possibly longG)?, and with MATLABs floating point numbers you won't get 100% precision that far.
You can change the format of your display output, but the precision is going to be an issue to that level regardless.
The relevant documentation.

Related

Simplify symbolic expression in matlab and get only the coeeficents

I'm looking for finding the coefficients from the Taylor's series in Matlab. The way that I'm doing is:
% Declare symbolic expression and function:
syms x;
f = exp(x);
% Calculate the taylor expansions in a concrete point:
T = taylor(f, x, 0.5);
% And finally I simplify the expression:
coefs = simplify(T)
But the returned expression is:
Yes, the expression it's simplified, but actually what I want is:
Where each term is multiplied by his coefficient. How can I this? Options suchs as simplify(f, x, 0.5, 10, where 10 refers to the simplification step,doesn't work in my case. Also I've been seeing this question and the problem is the same:
How get to simplify a symbolic and numeric mixed expression in Matlab
Depending on how many digits you want and in what exact format you want the result, here is an example:
>> c = double(coeffs(T))
c =
Columns 1 through 4
0.999966624859531 1.000395979357109 0.498051217190664 0.171741799031263
Columns 5 through 6
0.034348359806253 0.013739343922501
>> digits 15
>> x.^(0:numel(c)-1) * sym(c,'d').'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531
EDIT
Note that you could also use vpa( ) instead of converting to double first:
>> c = coeffs(T)
c =
[ (2329*exp(1/2))/3840, (233*exp(1/2))/384, (29*exp(1/2))/96, (5*exp(1/2))/48, exp(1/2)/48, exp(1/2)/120]
>> x.^(0:numel(c)-1) * vpa(c).'
ans =
0.0137393439225011*x^5 + 0.0343483598062527*x^4 + 0.171741799031263*x^3 + 0.498051217190664*x^2 + 1.00039597935711*x + 0.999966624859531

How can I stop Matlab rounding decimals when subtracting Datetime variables

I am having some issues in Matlab to do with rounding errors with datetime typed variables.
I have an array, lets call it 't', and it is of type datetime.
Say for example, t(2) = 00:01:35.6889999, and t(1) = 00:01:35.3549042.
If I try to do t(2)-t(1), all I get is an answer of type 'duration' of 00:00:00 .
I would like to find the difference between these times and keep the precision!
Any help, or directions to links that directly cater/relate to an issue like this would be appreciated! I'm not that familiar with using datetime & duration typed variables in Matlab!
Extra info: I am using Matlab R2017a
Edit: I have Format Long; written in my script.
Precision is not being lost, you just need to change the display format.
The default display format is HH:MM:SS:
>> A = duration(0, 0, 0, 1.25) % 1.25 MS
A =
duration
00:00:00
You can modify the format to display fractional parts. For example:
>> A.Format = 's' % Seconds only
A =
duration
0.00125 sec
>> A.Format = 'hh:mm:ss.SSSSSSSS' % HMS, up to 9 fractional second digits
A =
duration
00:00:00.00125000
You can also use helper functions like milliseconds or seconds to return double arrays:
>> seconds(A)
ans =
0.0013
>> milliseconds(A)
ans =
1.2500
This should work if only seconds vary in the two dates
second(t(1))-second(t(2))
The duration object actually has the proper precision. It just doesn't display it unless you set the format.
>> dur = duration(t(2) - t(1), 'Format', 's')
dur =
duration
0.3341 sec
Whether you set the format or not, you can grab the seconds directly from the duration object.
>> format long
>> seconds(dur)
ans =
0.334095700000000
>> seconds(t(2) - t(1))
ans =
0.334095700000000

Inf*0 in Matlab

I have the following line in my code:
1 - sqrt(pi/2)*sig*sqrt(Eb)*theta_l*exp(theta_l^2*sig^2*Eb/2).*(1 + erf(-theta_l*sig*sqrt(Eb)/sqrt(2)));
When I evaluate this expression for the following parameters:
Eb = 6324.6;
sig = 1/sqrt(2);
theta = 0.7;, I get Nan. I know that this comes from the product of Infinity by 0.
However when I tested the same line in Mathematica, the result was a finite value. How can I solve this issue? Thanks.
The problematic part of your function is exp(Eb/2). The value of Eb is so large, that the result of its exponentiation cannot be represented by a double precision floating point number (The numerical precision in Mathematica is obviously higher, or dynamic probably at the cost of performance), so you get Inf.
However, you can just change the input units to your function to stop this happening. For example, if we define your function as an anonymous function ...
funky = #(Eb, sig, theta_l) ...
1 - sqrt(pi/2)*sig*sqrt(Eb)*theta_l*exp(theta_l^2*sig^2*Eb/2) .* ...
(1 + erf(-theta_l*sig*sqrt(Eb)/sqrt(2)));
Then
funky(6324.6 / 1000, (1/sqrt(2))/1000, 0.7 / 1000) == ...
funky(6324.6 / 1e6, (1/sqrt(2))/1e6, 0.7 / 1e6) == ...
funky(6324.6 / 1e10, (1/sqrt(2))/1e10, 0.7 / 1e10) % etc

Why is MatLab making a real number complex?

I'm a little confused by what MatLab is doing here ... why does multiplying a real expression by a real constant suddenly make it complex?
x = -1.1451e+02 - 1.1317e+02i;
x*conj(x)
>> 2.5920e+04
10*x*conj(x)
>> 2.5920e+05 - 1.4552e-11i
It is definitely a rounding error. Note that if you add parentheses, your results change:
>> 10 * (x * conj(x));
ans =
2.5920e+05

numerical computation problems in matlab

in this question i am addressing to numerical computation problems in matlab and want to get experience how to avoid this problems/errors in future
for example let consider following simple codes
t = 0.4 + 0.1 - 0.5
t =
0
it works fine,but
u = 0.4 - 0.5 + 0.1
u =
2.7756e-17
of course in mind it is also 0,but why not in first calculation got the same result?or what is difference?also please look
v = (sin(2*pi) = = sin(4*pi))
v = (sin(2*pi)==sin(4*pi))
v =
0
it shows that sine function is not periodic,so what is general advice in this case?introduce some epsilon?like
V=((sin(2*pi)-sin(4*pi))<eps)
V =
0
or
EPS=0.000000000000001
EPS =
1.0000e-15
>> V=((sin(2*pi)-sin(4*pi))<EPS)
V =
1
please help me
It's normal you get these results, because floating-point relative accuracy in Matlab is
eps('double')
ans =
2.2204e-16
For V=((sin(2*pi)-sin(4*pi))<eps), because
sin(2*pi)-sin(4*pi)
ans =
2.4493e-16
which is larger than eps('double'), so its result will be V=0.
And for V=((sin(2*pi)-sin(4*pi))<EPS), because EPS>2.4493e-16, so its result will be V=1.