why I cannot get just a number [duplicate] - matlab

This question already has answers here:
Convert output from symbolic math (sym) to float
(2 answers)
Closed 8 years ago.
I did following:
clc
clear all
I0=1.2e12;
FWHM=10e-12;
c=FWHM./2.35482;
t=0:1e-12:50e-12;
syms t
int(I0.*exp(-1.*(t-5e-12).^2./(2.*c.^2)),t,0,40e-12)
but it does not give me a simple number (just a number)

The reason why matlab does not automatically give you a number is that precision could be lost.
Suppose you have a symbolic variable with value 1/3. That has infinite accuracy at this point. Yet if you evaluate it, you would lose this precision, so that is why it is not evaluated directly.
If you want to evaluate it, you could do that of course. Try doc double,doc vpa, doc eval or doc subs. I think the first one is what you need.

Related

Why is my output value in exponential format 1.04e+05? [duplicate]

This question already has an answer here:
Why did MATLAB delete my decimals?
(1 answer)
Closed 4 years ago.
I am calculating the area of objects in my image using bwarea like so:
i=imread('Z:\Azhagu project work\MRI\input.jpg');
imshow(i);
bwarea(i)
This gives the following output:
ans = 1.0428e+05
Why is the area in an exponential format?
It's not in exponential output; it's in scientific notation.
ans = 1.0428e+05 == 104280
You can force MATLAB to show all decimals using format long or format longG. This is just a display quirk of MATLAB, internally your number is stored as a double anyway, so for further calculations this is of no concern.
This area value is not in exponential format. For your given image, area is represented in Floating-Point numbers format. Hence the value 1.0428e+05 is actually 104280.

How to circumvent the extremely small output values? [duplicate]

This question already has an answer here:
Is there any way to increase 'realmax' in MATLAB?
(1 answer)
Closed 7 years ago.
For example in Matlab:
log(exp(-200)
-200
however
log(exp(-2000))
-inf
Naturally the input to the log function is passed as zero as exp(-2000) is insignificant and the log gives -inf. How can I fix this to receive -2000 instead?
If you have the Symbolic Math Toolbox, it's possible to do this with Variable Precision Arithmetic. Use the vpa function and place your mathematical expression as input into vpa wrapped in a string:
>> vpa('log(exp(-2000))')
ans =
-2000.0
However, this will be represented in symbolic format, so it may be prudent to convert back to a numerical value after you're done. Convert this result using double once you perform the calculation:
>> double(vpa('log(exp(-2000))'))
ans =
-2000
It is often possible to rewrite formulas in a way they do not exceed the range of floating point values. In your case, rewriting it would be trivially -2000. A more "real world" example can be found in this question where rewriting successfully avoided the problem.

How to access matrix alphabetically [duplicate]

This question already has answers here:
How to compute word scores in Scrabble using MATLAB
(2 answers)
Closed 7 years ago.
I'm new to matlab ... I simply would like to convert letter to numbers such that:
A=1
B=2
C=3
all my numbers are capital case. Off course, I could define constant for each char, but is there a shorter way?
Thanks!
See the char function. You can give it an integer argument.
http://www.mathworks.com/help/matlab/ref/char.html

exponential function with large argument in matlab [duplicate]

This question already has answers here:
How to compute an exponent in matlab without getting inf?
(3 answers)
Closed 7 years ago.
I've got one problem for a longer time and I'd be really grateful if you could help me somehow...
I have a code in MATLAB (version R2012a) where I compute some exponential functions using MATLAB's fuction exp. This means that I have something like this:
y = exp(x);
However, when this "x" is larger than a certain number, the result ("y") is infinity; when "x" is smaller than a certain number, the result is 0. As said on the MathWorks' website:
Numerical exceptions may happen, when the absolute value of the real
part of a floating-point argument x is large. If ℜ(x) < -7.4*10^8,
then exp(x) may return the truncated result 0.0 (protection against
underflow). If ℜ(x) > 7.4*10^8, then exp(x) may return the
floating-point equivalent RD_INF of infinity.
My problem is quite obvious - my "x" are pretty large so I receive infinities and zeros instead of results I need. My question is - how do I get the real results? Thanks in advance for help!
Use vpa with a string input:
>> exp(1000)
ans =
Inf
>> vpa('exp(1000)')
ans =
1.9700711140170469938888793522433*10^434
Note the result of vpa is of class sym.
A variable in any language is stored in a certain amount of bytes in the computer's memory. The more bytes used to hold a variable type, the more precise of a result that variable can hold. If you are using integers, the biggest type uses 64 bytes and is uint64. This is an unsigned integer (meaning it can only be positive) that can range from 0 to 18,446,744,073,709, 551,615. If you need decimals, try using vpa.

Matlab how to convert numbers to single precision floating point representation in binary [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Binary representation of a number in Matlab
I am using matlab and would like to convert a number such as 7546.456124865 to single precision.
I've used the single() command, but I would like to get the binary for the single precision floating point. How do I go about doing this?
>> num2hex(single(7546.456124865))
ans =
45ebd3a6