Coding a Matlab Function

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
  • #1
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?
 
  • #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-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:
  • #3
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##.
 
  • #4
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-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?
 
  • #5
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
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
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 :)
 
  • #8
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
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
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
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
 

Suggested for: Coding a Matlab Function

Replies
3
Views
538
Replies
1
Views
708
Replies
5
Views
831
Replies
1
Views
723
Replies
1
Views
574
Replies
2
Views
900
Back
Top