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

In summary, the conversation is about a student seeking help with their script that calculates the value of "e" using Euler's method. They are having trouble with the output only displaying up to 4 decimal places and are asking for assistance in increasing the precision. The expert suggests changing the format of the strings or using the "fprintf" function for more precision.
  • #1
seto6
251
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
  • #2
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
 

1. What is the value of e in matlab?

The value of e in matlab is a mathematical constant that is approximately equal to 2.71828.

2. How do I calculate e in matlab?

To calculate e in matlab, you can use the command "exp(1)" or "exp(1.0)" to get the value of e. This function raises e to the power of the input argument.

3. How do I get the answer in double type when calculating e in matlab?

To get the answer in double type when calculating e in matlab, you can use the "double" function to convert the output to a double data type. For example, you can use "double(exp(1))" to get the value of e in double type.

4. Can I change the precision of the calculated e value in matlab?

Yes, you can change the precision of the calculated e value in matlab by using the "format" command. For example, you can use "format long" to display the e value with a higher precision or "format short" for a lower precision.

5. Is there a built-in function to calculate e in matlab?

Yes, there is a built-in function in matlab called "exp" that calculates e. This function can take in an input argument and raise e to that power, or you can simply use "exp(1)" to get the value of e itself.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
862
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Replies
31
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top