# Coding a Matlab Function

• ver_mathstats
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. f

#### ver_mathstats

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?

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.

https://www.symbolab.com/graphing-calculator

Now, 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:
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##.

pbuk
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.

https://www.symbolab.com/graphing-calculator

Now, 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?

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.

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.

ver_mathstats
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 :)

pbuk
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?

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.

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
.

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