Creating a Double Precision Matlab Function from Scratch

Click For Summary

Discussion Overview

The discussion revolves around creating a MATLAB function to evaluate the expression f(x) = (e^x - x - 1) / (x^2) under the condition |x| < 1, while adhering to the constraints of using only double precision and avoiding built-in MATLAB functions. Participants explore various approaches to implementing this function, including the use of the Maclaurin series for e^x.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants express confusion about the requirements, questioning whether the task is simply to write the function in double precision.
  • Others suggest starting with plotting the function to understand its behavior, emphasizing the importance of the condition |x| < 1.
  • It is noted that the condition |x| < 1 is intended to simplify the problem, rather than being a strict requirement to test for in the code.
  • Participants discuss the need to replace e^x with its Maclaurin series expansion, with some proposing the use of loops to calculate the series to a certain number of terms.
  • Concerns are raised about rounding errors when using the Maclaurin series, with suggestions to simplify the function mathematically before implementation.
  • There is a discussion about the accuracy of the function and what constitutes "as accurately as possible" in the context of the homework assignment.
  • Some participants indicate that using a while loop may provide a better method for determining when to stop the series expansion, while others remind to focus on calculating f(x) rather than just e^x.
  • Clarifications are made regarding misunderstandings about the problem requirements, particularly concerning the condition |x| < 1.

Areas of Agreement / Disagreement

Participants generally agree on the need to use the Maclaurin series for e^x and the importance of avoiding built-in functions. However, there is no consensus on the best approach to implement the function, and various strategies are proposed and debated.

Contextual Notes

Participants express uncertainty regarding the number of terms needed for the Maclaurin series to achieve valid results for all |x| < 1, as well as concerns about rounding errors and the implications of the accuracy requirement.

ver_mathstats
Messages
258
Reaction score
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
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:
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   Reactions: pbuk
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?
 
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.
 
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   Reactions: ver_mathstats
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   Reactions: pbuk
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?
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K