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

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)

Related

Multiplication in matlab [duplicate]

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

Why if `t(n)==0.06`, in my command window cannot displaying 0.06? [duplicate]

This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have MATLAB code
clear all;
clc;
t=0:0.001:0.1;
N=length(t);
for n=1:N
if t(n)==0||t(n)==0.02||t(n)==0.04||t(n)==0.06||t(n)==0.1
fprintf('%5.5f\n',t(n));
end
end
The result is
Why if t(n)==0.06, in my command window cannot displaying 0.06?
Does my MATLAB have an error? I'm using MATLAB R2014b
It's a numerical error because the 0.001 increments will not be precisely 0.001 under the hood.
The same thing happens in 2020b:
>> t = 0:0.001:0.1;
>> t(61)
ans =
0.0600
>> t(61) == 0.06
ans =
logical
0
If you want to compare floats, use some kind of tolerance like eps instead of ==:
>> (t(61) - 0.06) < eps
ans =
logical
1

Why does MATLAB sprintf('%.15g',-1*0) print '-0'? [duplicate]

This question already has answers here:
Small negative number maintains sign after rounding
(2 answers)
Closed 3 years ago.
Why am I getting the following in MATLAB? Tried in 2015b and 2019a and same result.
sprintf('%.15g',-1*0)
ans =
'-0'
format hex shows you what's going on - IEEE double precision numbers can have negative zero.
>> format hex
>> 0
ans =
0000000000000000
>> -0
ans =
8000000000000000

Error about Subscript indices of sum function for float matrix matlab [duplicate]

This question already has an answer here:
matlab strange behaviour of function sum()
(1 answer)
Closed 7 years ago.
I have a matrix 202x141x3 that called M matrix. I want to perform the code
sum(M,3);
However, I got error such as
Subscript indices must either be real positive integers or logicals.
I debugged and saw the content in M matrix, it has not problem. But I cannot use the above function. Could you see my M.mat matrix at here and let me know how can I use above function for my M.mat matrix
You did set a variable under the name sum
a = [1 2 3 ; 4 -5 6; 7 8 9]
sum = 1;
>> sum(a)
Subscript indices must either be real positive integers or logicals.
clear sum;
>> sum(a)
ans =
12 5 18

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