Calculating e, using matlab (how to get answer in double type)

  • Thread starter Thread starter seto6
  • Start date Start date
  • Tags Tags
    Matlab Type
Click For Summary
SUMMARY

The discussion focuses on calculating the mathematical constant "e" (approximately 2.718281828) using MATLAB. The user’s script successfully computes "e" but only displays results up to four decimal places. The solution involves utilizing MATLAB's formatting functions, specifically the 'format' command and 'fprintf' for enhanced precision in output. Implementing these changes will allow for more accurate representation of the calculated value.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with numerical methods for calculating constants
  • Knowledge of MATLAB's output formatting functions
  • Experience with loops and conditional statements in MATLAB
NEXT STEPS
  • Explore MATLAB's 'format' command for output precision
  • Learn how to use 'fprintf' for formatted output in MATLAB
  • Investigate alternative methods for calculating mathematical constants in MATLAB
  • Review MATLAB documentation on numerical data types and precision
USEFUL FOR

Students, educators, and professionals in mathematics or engineering who are using MATLAB for numerical computations and require precise output formatting.

seto6
Messages
248
Reaction score
0

Homework Statement



ok i am using Euler's way, i have to calculate "e"=2.718281828.

my script(see below) works but it only prints up to 4 decimal places, i have no idea why, help me please, thank you in advance.

Homework Equations



N/A

The Attempt at a Solution



Code:
function [eis] = euler1(in)
c=in-1;

while c>=1,
    
    if c == double(in-1),
        Q = double(c+(c/in));
    end
    
    Q= double(c + (c/Q));
    
    alpha = double(Q);
    c=c-1;
end
eis = 2+ 1/(double(alpha));
 
Last edited:
Physics news on Phys.org
There's several ways to do this. The easy way is to just change the way strings are formatted.

>> help format

For more precision, try

>> help fprintf
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
1
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K