See this code below, from Octave's Command Prompt:
>> log( 0.1 ) * 5e-8
ans = -0.00000011513
As can be seen, there are quite a few zeros ( 6 to be exact). I would like if the number is displayed as 1.1513e-7. Or, scientific notation, by default. How does one do that?
You can change the format of the ouput displayed by octave with the function format,
e.g. format short e forces short length exponential notation, which seems that is what you want.
Related
I've been trying to display in my console an exponential equation like the following one:
y(t) = a*e^t + b*e^t + c*e^t
I would write it as a string, however the coefficients a,b and c, are numbers in a vector V = [a b c]. So I was trying to concatenate the numbers with strings "e^t", but I failed to do it. I know scilab displays polynomial equations, but I don't know it is possible to display exponential one. Anyone can help?
Usually this kind of thing is done with mprintf command, which places given numerical arguments into a string with formatting instructions.
V = [3 5 -7]
mprintf("y(t) = %f*e^t + %f*e^t + %f*e^t", V)
The output is
y(t) = 3.000000*e^t + 5.000000*e^t + -7.000000*e^t
which isn't ideal, and can be improved in some ways by tweaking the formatters, but is readable regardless.
Notice we don't have to list every entry V(1), V(2), ... individually; the vector V gets "unpacked" automatically.
If you wanted to have 2D output like what we get for polynomials,
then no, this kind of thing is what Scilab does for polynomials and rational functions only, not for general expressions.
There is also prettyprint but its output is LaTeX syntax, like $1+s+s^{2}-s^{123}$. It works for a few things: polynomials, rational functions, matrices... but again, Scilab is not meant for symbolic manipulations, and does not really support symbolic expressions.
I am trying to print a long table of numbers in octave terminal.
disp(vec);
What I get
7.0931e-01
6.2041e-05
9.7740e-01
9.9989e-01
8.8428e-01
9.0524e-01
...
Such numerical notation is a pain to read. How can I set octave terminal to output numbers normally as 0.7, 0.014, 0.95?
You can use format short g to display each number is a more logical format
format short g
disp(vec)
% 0.70931
% 6.2041e-05
% 0.9774
% 0.99989
% 0.88428
% 0.90524
Using 'fprintf' could help in such cases
a=0.0001234;
fprintf('%.3f\n',a)
But here the limitation is that number of decimal points would be fixed so in some numbers it will display zeros at the end while for some numbers it might cut off the number.
I can get maxima to solve an equation but can't figure out why it won't show it's numerical value without typing the extra command/step of float(%). Is there away to automatically convert a solved variable to a numerical format.
Example of equation below:
kill(all); alpha:float(.0014931); endfreq:50; dursec:1200; solve(alpha=log(startfreq/endfreq)/dursec,float(startfreq));
what comes back is
startfreq=50%e(44793/25000)
I would like it to say 299.988 instead
Well, Maxima prefers exact results (i.e., integers, rational numbers, and symbolic constants) instead of inexact (i.e., float and bigfloat numbers). If you want to work only with numerical solutions, take a look at find_root. E.g.:
(%i1) [alpha, endfreq, dursec] : [0.0014931, 50, 1200] $
(%i2) find_root (alpha = log(startfreq / endfreq)/dursec, startfreq, 1, 500);
(%o2) 299.9881594652534
Note that to use find_root you must know an interval (here 1 to 500) which contains a root of the equation.
In octave what is the difference between log(a) and log a?
>>a
a =
1 2
>>log(a)
ans =
0.00000 0.69315
>>log a
ans = 4.5747
In the second example, Octave is interpreting 'a' as a char, converting 'a' to its ASCII representation (97) and then getting the natural logarithm.
log(97) = 4.5747
In general you have two ways to call functions: as a function or as a command. E.g.
save('test.txt')
save test.txt
When a function is used as a command, it assumes the input is a string.
Anyway newer version of Matlab and Octave have an error check for character input (there is little reason to compute the logarithm of the ASCII equivalent of a character).
I need to do some operation on MAC addresses, coded on 48 bits, but format truncates the results :
format 0x%x 0x100000000 ;# --> 0x0
Is it possible to do something for this, or must I adapt my code to use smaller numbers ?
In Tcl 8.4 just give the size modifier l to the field specificator of format. This way, you tell format to interpret the value as (at least) 64-bit number (same size of wide(), which is machine dependent):
format 0x%lx 0x100000000
(Note that it is a lower case el letter, not the one digit.)
In Tcl 8.5 and later, integer math is done with arbitrary precision and the ll size modifier tells format to not truncate the value:
format 0x%llx 0x100000000
(Again, they are two lower case el letters, not two one digits.)