Ask your own question, for FREE!
Engineering 20 Online
OpenStudy (anonymous):

I need help with a MATLAB problem. Write a MATLAB function to compute the exponential function directly from the Taylor series: e^x=1+x+X^2/2!+x^3/3!.... The series should end when the last term is less than 10^-6. Test your function against the built in function exp, but be careful not to make x too large-this could cause rounding error. Can anyone help me??

OpenStudy (mathmate):

If we notice that the terms of the Taylor's series are such that the nth term is the previous term multiplied by x/n, considering the initial "1" as the zeroth term. This means that instead of calculating each term from scratch, we only need to multiply the previous term by x/n to get the present term. 1 (term zero) x (term zero * x /1 ) x^2/2! = x* (x/2) x^3/3! = x^2/2! * (x/3) ... the running sum calculated will be the various approximation for exp(x). A check should be incorporated to stop calculations when the term is less than 10^(-6)

OpenStudy (anonymous):

I kind of understand what you are saying but I'm confused on how to write the actual code.

OpenStudy (mathmate):

Hint: Pseudocode: Function Exp(x) initialize term, sum, n while (term>10^(-6)) term=term*(x/n) sum=sum+term n=n+1 end while return sum Give it a try with Matlab and post your code if you need help.

OpenStudy (anonymous):

OK thanks I will post my code if I need more help

OpenStudy (mathmate):

You're welcome! :)

OpenStudy (anonymous):

I am still having issues with this code. Can someone help me correct it so that I can run right. Thanks

OpenStudy (mathmate):

This is where defining variables (in English) is important when programming so that we do not confuse the values. In your code, y is used to return the value of the function, which is the sum of the series. That's ok. In the lines while y>10^-6 y=y*(x/n); y represents the value of each term of the series, which is not the sum of the series. Also, y must assume a value before we can do the recursive statement y=y*(x/n) This initial value must be defined before entering the while loop. Similarly, n and sum must be initialized to appropriate values before entering the loop. (n=1, sum=0), Finally, y=sum (because y is the return value equal to sum) must be placed at the end. Try to make corrections and post if you can.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!