Format of a number

11 views (last 30 days)
Sania Nizamani
Sania Nizamani on 3 Feb 2024
Edited: Walter Roberson on 29 Feb 2024
How can write or convert the following number as 6.5192e-353 in MATLAB R2020a?
Thanks in advance!
Sania Nizamani
Sania Nizamani on 3 Feb 2024
Edited: Walter Roberson on 29 Feb 2024
The number was the output of the following code of an iterative method called the Newton method. The number shown above is the absolute error at the last iteration:
clc;clear;close all;
% Set the precision to a large number using VPA digits(1000);
% Define your function and its derivative using VPA
f = @(x) vpa(sin(x)^2-x^2+1);
% Replace with your actual function
f_prime = @(x) vpa(sin(2*x)-2*x);
% Replace with the derivative of your function
% Initial guess
initial_guess = 6.5;
tolerance = 1e-300;
max_iterations = 200;
% Initialize variables
x = vpa(initial_guess);
% Perform iterations
for iteration = 1:max_iterations
% Compute the next estimate using the Newton-Raphson formula
x_next = x - f(x) / f_prime(x);
% Calculate the absolute error
AE = abs(x_next - x);
disp( 'Error is = ' + string(AE) + char(9) );
% Check if the change is smaller than the tolerance
if AE < tolerance
root = x_next;
% Update the current estimate for the next iteration
x = x_next;
% If the loop reaches here, the method did not converge within max_iterations
error('Newton-Raphson method did not converge within the specified number of iterations');
Sania Nizamani
Sania Nizamani on 3 Feb 2024
Moved: Dyuman Joshi on 3 Feb 2024
Dear Stephen23, I have attached the .m file. Please have a look. I look forward to hearing from you. Thank you.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 3 Feb 2024
Moved: John D'Errico on 29 Feb 2024
You need to understand that MATLAB can represent numbers as large as
ans = 1.7977e+308
and as small in magnitude as
ans = 2.2251e-308
as double precision numbers. Actually, it can go a little smaller than that, because of something called de-normalized numbers, but that is an aside that I won't go into. Regardless, the number you have there is too small by a large amount. It underflows to ZERO. As far as MATLAB is concerned, it does not exist as a double.
However, if you have created it, then you are using symbolic tools to create it. And symbolic floats can handle that with nary a problem. As such, if you have the number...
x = sym('0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006519189212084746229776610798014015843782113571066818006946457413769773297894931346117308775461249994927327158400893029327734069232681151773609865266731521520054375903565338010423727080339452649084922482509849273285567974490982577103240751064484991178667240948437033727548535692375294238872288406967117777615757244524019518397825957547275970242462715854779648975320964429805929292231364557099949366197615233659670288768272563638001796519438847757405012852185872992289542013224335055773245806454313412733757091902202174306446270378638302539811037703945271596412702870934289523078309661834975228374662651691916691844575389604650486112874690677474198715593749538378138098722106119539312955520393459310616851829420920453027031099134852992191414078925320755874886661755203134433583041067365856197990407535151294849597815149662533367217765732422274124039425622331728003788521934226574093175659329930102209493669914675156094756791723604103890639375296094056543847670814774909966692607698458476102455720781979')
x = 
As you can see, MATLAB has no problem expressing it as you ask. And if you want to see only 5 significant digits, then tell vpa to do so.
ans = 
So just learn to use vpa.
Sania Nizamani
Sania Nizamani on 3 Feb 2024
Moved: John D'Errico on 29 Feb 2024
Thank you.
Stephen23 on 29 Feb 2024
Accepted, as the OP seemed satisfied.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!