Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:41:43+00:00 2026-06-02T10:41:43+00:00

Does anyone know how to make the following Matlab code approximate the exponential function

  • 0

Does anyone know how to make the following Matlab code approximate the exponential function more accurately when dealing with large and negative real numbers?

For example when x = 1, the code works well, when x = -100, it returns an answer of 8.7364e+31 when it should be closer to 3.7201e-44.

The code is as follows:

s=1
a=1;
y=1;
for k=1:40
    a=a/k;
    y=y*x;
    s=s+a*y;
end
s

Any assistance is appreciated, cheers.

EDIT:

Ok so the question is as follows:

Which mathematical function does this code approximate? (I say the exponential function.) Does it work when x = 1? (Yes.) Unfortunately, using this when x = -100 produces the answer s = 8.7364e+31. Your colleague believes that there is a silly bug in the program, and asks for your assistance. Explain the behaviour carefully and give a simple fix which produces a better result. [You must suggest a modification to the above code, or it’s use. You must also check your simple fix works.]

So I somewhat understand that the problem surrounds large numbers when there is 16 (or more) orders of magnitude between terms, precision is lost, but the solution eludes me.

Thanks

EDIT:

So in the end I went with this:

s = 1;
x = -100;
a = 1;
y = 1;
x1 = 1;

for k=1:40
    x1 = x/10;
    a = a/k;
    y = y*x1;
    s = s + a*y;
end

s = s^10;
s

Not sure if it’s completely correct but it returns some good approximations.

exp(-100) = 3.720075976020836e-044
s = 3.722053303838800e-044

After further analysis (and unfortunately submitting the assignment), I realised increasing the number of iterations, and thus increasing terms, further improves efficiency. In fact the following was even more efficient:

s = 1;
x = -100;
a = 1;
y = 1;
x1 = 1;

for k=1:200
    x1 = x/200;
    a = a/k;
    y = y*x1;
    s = s + a*y;
end

s = s^200;
s

Which gives:

exp(-100) = 3.720075976020836e-044
s = 3.720075976020701e-044

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:41:44+00:00Added an answer on June 2, 2026 at 10:41 am

    As John points out in a comment, you have an error inside the loop. The y = y*k line does not do what you need. Look more carefully at the terms in the series for exp(x).

    Anyway, I assume this is why you have been given this homework assignment, to learn that series like this don’t converge very well for large values. Instead, you should consider how to do range reduction.

    For example, can you use the identity

    exp(x+y) = exp(x)*exp(y)
    

    to your advantage? Suppose you store the value of exp(1) = 2.7182818284590452353…

    Now, if I were to ask you to compute the value of exp(1.3), how would you use the above information?

    exp(1.3) = exp(1)*exp(0.3)
    

    But we KNOW the value of exp(1) already. In fact, with a little thought, this will let you reduce the range for an exponential down to needing the series to converge rapidly only for abs(x) <= 0.5.

    Edit: There is a second way one can do range reduction using a variation of the same identity.

    exp(x) = exp(x/2)*exp(x/2) = exp(x/2)^2
    

    Thus, suppose you wish to compute the exponential of large number, perhaps 12.8. Getting this to converge acceptably fast will take many terms in the simple series, and there will be a great deal of subtractive cancellation happening, so you won’t get good accuracy anyway. However, if we recognize that

    12.8 = 2*6.4 = 2*2*3.2 = ... = 16*0.8
    

    then IF you could efficiently compute the exponential of 0.8, then the desired value is easy to recover, perhaps by repeated squaring.

    exp(12.8)
    ans =
              362217.449611248
    
    a = exp(0.8)
    a =
              2.22554092849247
    a = a*a;
    a = a*a;
    a = a*a;
    a = a*a
              362217.449611249
    
    exp(0.8)^16
    ans =
              362217.449611249
    

    Note that WHENEVER you do range reduction using methods like this, while you may incur numerical problems due to the additional computations necessary, you will usually come out way ahead due to the greatly enhanced convergence of your series.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a cleaner solution for the following: I'm running a matlab
Does anyone know why the following code void foo(const int X) { #pragma omp
Does anyone know how to make labels, or text, smoother? At the moment they
Does anyone know of a way to make an AIR app display over top
Does anyone know how you can make a cocoa sheet with rounded corners like
Does anyone know an algorithm for the following problem: Given a undirected connected graph
(Split from an earlier question). Does anyone know what's going on here? The following
Does anyone know why I'm getting a NullReference Expception at following line: var field
Does anyone know if there is a way to make an Excel 2003 Document
Does anyone know how to get IntelliSense to work reliably when working in C/C++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.