Creating a Double Precision Matlab Function from Scratch

In summary, the conversation is about writing a MATLAB function that evaluates a specific function accurately without using any built-in functions except for double precision. The function in question involves a Maclaurin series for e^x and the discussion mentions using a graphing calculator to understand the behavior of the function and using conditional statements in the code. The conversation also mentions the importance of handling rounding errors and using a for loop to implement the infinite sum of the Maclaurin series.
  • #1
ver_mathstats
260
21
Homework Statement
Write a matlab function that evaluates f(x) as accurately as possible when |x|<1. You can only use double precision and must not use any matlab built in functions,
Relevant Equations
The function we are required to code is got (f(x)=e^x-x-1)/(x^2)
I am confused at how to code this without using any of matlab's already built in functions except for using double. Is this question just asking me to write out the function and then make sure it's double precision?
 
Physics news on Phys.org
  • #2
So we're not allowed to give direct answers on this forum, but I would suggest starting off to plot the function on a graphing calculator and see how it looks in general. This is just to get you started to understand how this function behaves overall.

For example, plotting it on something like Symbolab could help you:
https://www.symbolab.com/graphing-calculatorNow, let's start with the "|x| <1" condition. Since you can't use a built-in function from MATLAB, how would you be able to implement this in MATLAB? Essentially, what this is asking is to take in all inputs of x in which the absolute value of that input is less than "1". Think of what "condition" means when you're writing code.
 
Last edited:
  • #3
ver_mathstats said:
Homework Statement:: Write a MATLAB function that evaluates f(x) as accurately as possible when |x|<1. You can only use double precision and must not use any MATLAB built in functions,
Relevant Equations:: The function we are required to code is got (f(x)=e^x-x-1)/(x^2)

I am confused at how to code this without using any of matlab's already built in functions except for using double. Is this question just asking me to write out the function and then make sure it's double precision?
No. These are two separate requirements.
  • Built-in MATLAB functions are not allowed.
  • Calculations are to be done using double precision.
Since you aren't allowed to use exp(), it seems to me that the first requirement presumes that you know about the Maclaurin series for ##e^x##.
 
  • Like
Likes pbuk
  • #4
ammarb32 said:
So we're not allowed to give direct answers on this forum, but I would suggest starting off to plot the function on a graphing calculator and see how it looks in general. This is just to get you started to understand how this function behaves overall.

For example, plotting it on something like Symbolab could help you:
https://www.symbolab.com/graphing-calculatorNow, let's start with the "|x| <1" condition. Since you can't use a built-in function from MATLAB, how would you be able to implement this in MATLAB? Essentially, what this is asking is to take in all inputs of x in which the absolute value of that input is less than "1". Think of what "condition" means when you're writing code.
Okay, so we are essentially replacing e^x with its Maclaurin series and then when you say condition, you mean writing out the conditional statements?
 
  • #5
ver_mathstats said:
Okay, so we are essentially replacing e^x with its Maclaurin series and then when you say condition, you mean writing out the conditional statements?
I'm afraid @ammarb32 may be confusing you: the ## |x|<1 ## condition is only there to make it easier for you (can you think why?), it is not something you need to test for. I should ignore that post and carry on with rewriting ## f(x) ## using the Maclaurin expansion.
 
  • #6
ver_mathstats said:
Write a MATLAB function that evaluates f(x) as accurately as possible when |x|<1.
Note that as accurately as possible is quite a big ask: I hope the marker is going to allow something that is reasonably accurate.
 
  • Like
Likes ver_mathstats
  • #7
pbuk said:
Note that as accurately as possible is quite a big ask: I hope the marker is going to allow something that is reasonably accurate.
It's only a homework practice problem to help us code in MATLAB luckily :)
 
  • Like
Likes pbuk
  • #8
pbuk said:
I'm afraid @ammarb32 may be confusing you: the ## |x|<1 ## condition is only there to make it easier for you (can you think why?), it is not something you need to test for. I should ignore that post and carry on with rewriting ## f(x) ## using the Maclaurin expansion.
I'm working on this question right now, would it make sense to write a for or while loop for the maclaurin series of e^x to obtain a value for a certain number of terms and then input that number into the rest of the function? Or am I way off?
 
  • #9
ver_mathstats said:
I'm working on this question right now, would it make sense to write a for or while loop for the maclaurin series of e^x to obtain a value for a certain number of terms and then input that number into the rest of the function? Or am I way off?
You have to be careful about rounding errors, so I wouldn't simply replace the exponential with its Maclaurin series. Write it on paper first and see if there is a way to simplify the function mathematically first.

You will eventually end up with an infinite sum, which can be implemented in a truncated form using a for loop. You might want to figure out first how many terms you need to keep to get a valid result for all |x| < 1. Tip: start by summing the smallest terms first to reduce the truncation errors.
 
  • #10
ver_mathstats said:
I'm working on this question right now, would it make sense to write a for or while loop for the maclaurin series of e^x to obtain a value for a certain number of terms and then input that number into the rest of the function? Or am I way off?
You are not way off at all. I would write a while loop which will enable you to make a better choice for termination than after a fixed number of iterations.

One thing though, you are trying to calculate f(x) not ## e^x ## so don't forget to change your series to reflect this
it should end up with fewer terms and converge much faster
.
 
  • #11
pbuk said:
I'm afraid @ammarb32 may be confusing you: the ## |x|<1 ## condition is only there to make it easier for you (can you think why?), it is not something you need to test for. I should ignore that post and carry on with rewriting ## f(x) ## using the Maclaurin expansion.
Whoops sorry about that. I misunderstood the problem
 

FAQ: Creating a Double Precision Matlab Function from Scratch

What is a double precision Matlab function?

A double precision Matlab function is a function that can handle and manipulate numerical data with a higher level of precision than single precision functions. It uses 64-bit floating point numbers, allowing for more accurate calculations and a wider range of values.

Why would I need to create a double precision Matlab function from scratch?

You may need to create a double precision Matlab function from scratch if you are working with large datasets or performing complex calculations that require a higher level of precision. It can also be useful if you need to ensure consistency and accuracy in your results.

What are the steps for creating a double precision Matlab function from scratch?

The steps for creating a double precision Matlab function from scratch are as follows:
1. Define the function name and input/output arguments
2. Write the code for the function using double precision variables
3. Test the function with different input values to ensure accuracy
4. Save the function as a .m file in your Matlab directory
5. Call the function in your main Matlab script or command window.

How can I convert an existing single precision function to a double precision function?

To convert an existing single precision function to a double precision function, you will need to change the variable types from single to double. This can be done by adding the suffix "d" to the end of the variable name (e.g. "x" becomes "xd"). You may also need to make adjustments to the code to account for the change in precision.

Are there any limitations to using double precision Matlab functions?

While double precision Matlab functions offer a higher level of precision, they also require more memory and processing power. This can lead to slower performance and may not be necessary for all applications. Additionally, some functions and operations may not be compatible with double precision variables, so it is important to test and verify the accuracy of your results.

Similar threads

Replies
3
Views
1K
Replies
13
Views
3K
Replies
1
Views
933
Replies
16
Views
1K
Replies
7
Views
1K
Replies
4
Views
2K
Back
Top