Multiplication in matlab [duplicate] - matlab

This question already has an answer here:
Displaying rational numbers in Matlab
(1 answer)
Closed 1 year ago.
4.082*2118
When I write this multiplication in MATLAB software, I get this answer: 1.705868e+04
but I want to show the answer like this: 17058.68
what is my wrong?

In the Matlab console, type format rational. Example:
>> 4.082 * 2118
ans =
8.6457e+03
>> format rational
>> 4.082 * 2118
ans =
319890/37
You can also use the rat and rats functions. The rat function can also give you the numerator and the denominator as numbers:
>> [num, denom] = rat(4.082 * 2118)
num =
319890
denom =
37
Note that the result is only an approximation. You can improve it by decreasing the tolerance:
>> [num, denom] = rat(4.082 * 2118, 0)
num =
2161419
denom =
250

Related

Compare symbolic variables

I am trying to compare two symbolic variables (numbers). The whole problem boils down to the following code.
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))
Both comparisons should return 1, but the second returns 0.
My solution:
digits(51);
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))
Why you encounter this problem
vpa evaluates symbolic inputs with variable-precision floating-point arithmetic (32 significant digits by default)... So what's happening in your case is
>> R = vpa(0.555555555555555555555555555)
R =
0.55555555555555555555555555555556
>> R*(1-sym(10^(-50)))
ans =
0.55555555555555555555555555555556
32 digits are definitely not enough to store the actual value of 1-10^(-50).
How to fix it
Without stressing with vpa() you can declare both R and R * (1 - 10^(-50)) as symbolics (in fact 0.5555555... = 5/9), and compare them:
>> R = str2sym('5/9');
>> X = str2sym('5/9 * (1 - 10^(-50))');
>> isAlways(R > X)
ans =
logical
1

Summing double variables in matlab [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 5 years ago.
I'm having trouble summing 3 double numbers in MATLAB Command Window, here are my variables
>> a = 0.45;
>> b = 0.05;
>> c = -0.5;
when I sum them like this, I get 1.3878e-17
>> c + a + b
ans =
1.3878e-17
But when I use parentheses, it returns 0
>> c + (a + b)
ans =
0
Also summing them in this order returns 0 again
>> a + b + c
ans =
0
In usual storage of floats in programming languages using IEEE 754. Therefore, for storage of some floating numbers which cannot be shown by sum of some 2^i, there wouldbe some errors (in base 2) such as 0.3.
As mentioned in comments, you can use digits to find these errors:
digits(100);
vpa(a)
>> 0.4500000000000000111022302462515654042363166809082031250000000000000000000000000000000000000000000000
vpa(b)
>> 0.05000000000000000277555756156289135105907917022705078125000000000000000000000000000000000000000000000
vpa(c)
>> -0.5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
vpa(a+b)
>> 0.5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
vpa(c+a)
>> -0.04999999999999998889776975374843459576368331909179687500000000000000000000000000000000000000000000000
As you can see in the above, because of this error, sum of a and c is not as exactly as you've expected (because of error in storage of floating points in base 2 in IEEE 754).
Therefore, there order of + is important as you found through these expressions. Hence, the preference of plus is from left to right, and preference of parenthesis is higher than plus, in the c + (a + b) and a + b + c, the a + b is done first,. Then, you can see the sum is exact in the above. However, in c + a + b, the c + a is happened sooner and you can see this sum is not exact, so the result of this sum with b could have some error.
In this way, you can find a + c + b is not exact as you want. And:
vpa(c + b)
>> -0.4500000000000000111022302462515654042363166809082031250000000000000000000000000000000000000000000000
So,
c + b + a
>> 0

How to round symbolic expressions to N digits in Matlab [duplicate]

This question already has an answer here:
Rounding to n significant digits
(1 answer)
Closed 5 years ago.
When you try rounding a symbolic expression to N digits you get the error messages below:
>> format long
>> syms x;
>> round(x, 10)
Error using sym/round
Too many input arguments.
>> round(vpa(pi), 10)
Error using sym/round
Too many input arguments.
So how do you make this work ?
Here it is how you do
>> syms x; N = 6;
>> round(pi*10^N)/vpa(10^N)
ans =
3.141593
>> round(x*10^N)/vpa(10^N)
ans =
0.000001*round(1000000*x)

Convert array elements to single number [duplicate]

This question already has answers here:
Return vector elements as a single integer
(2 answers)
Closed 8 years ago.
I have an array A=[1,0,1,1,1,0]. I want to convert it to a decimal number B = 101110. I have tried all the conversion functions, but couldn't find an appropriate solution.
This can be done quite simply this way:
B = sum(A.*10.^(numel(A)-1:-1:0))
B =
101110
What it does is take each number in A and multiply it with 10^n where n corresponds to the value appropriated with that place in the vector. By taking the sum of this new vector you'll get your answer.
It's the equivalent of:
1*10^5 + 0*10^4 + 1*10^3 + 1*10^2 + 1*10^1 + 0*10^0
As Luis commented, it can also be done as
B = 10.^(numel(A)-1:-1:0) * A(:);
you can make a function
function decimal = array2dec(A)
nA = length(A);
decimal = 0;
for i = 1:nA
decimal = decimal + A(i)*10^(nA-i);
end
save this.
>> A = [1,0,1,1,1,0];
>> dec = array2dec(A)
>> dec =
>> 101110
Yet another approach. Probably not very fast, though:
base2dec(A+'0',10)

transcedental equations in MATLAB [duplicate]

This question already has answers here:
solving nonlinear equations in Octave
(2 answers)
Closed 9 years ago.
How to solve a equation like 3^x + 4^x = 6^x in MATLAB . I want the solution exact to eight decimal digits .
I tried a very simplistic way but there is not enough memory for that . Since I know the solution is between 1 and 2 , I thought of creating an array x = [1:10^-9:2] and then use these arrays to find the value of correct x . I know this is very naive method .
How does one go about solving such equations in MATLAB ?
Use fzero:
>> f = #(x) 3^x + 4^x - 6^x
f =
#(x)3^x+4^x-6^x
>> x0 = [1 2]
x0 =
1 2
>> format long g
>> fzero(f,x0)
ans =
1.293174075673