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

  • SEARCH
  • Home
  • 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 8847173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:08:28+00:00 2026-06-14T12:08:28+00:00

function [y]=AmericanPutClassic (St,t) % S0 = 100; K = 100; r = 0.05; sigma

  • 0
function [y]=AmericanPutClassic (St,t)
% S0 = 100;
K = 100;
r = 0.05;
sigma = 0.3;
T = 2;
nsteps = 5;
% St
dt = T / nsteps;
u=exp(sigma*sqrt(dt));
d=1/u;

Pu=(exp(r*dt)-d)/(u-d);
Pd=1-Pu;

if t==T
    y=max(K-St,0);
    return
elseif t<T
    upPrice=AmericanPutClassic(St*u,t+dt);
    downPrice=AmericanPutClassic(St*d,t+dt);
    PrevPrice=(Pu*upPrice+Pd*downPrice)*exp(-r*dt);
    if max(K-St,0) > PrevPrice
        y=max(K-St,0);
    else 
        y=PrevPrice;
    end
    return
end
end

I think my code does the job, but when I make ‘nsteps’ higher than 5, it crushes… I keep getting different errors… now it just says there is problem with my code when its higher than 5… before it would say:
"??? Maximum recursion limit of 500 reached. Use set(0,’RecursionLimit’,N)
to change the limit. Be aware that exceeding your available stack space can
crash MATLAB and/or your computer."

Can anybody spot the problem?
I start by calling AmericanPutClassic(100,0)…

  • 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-14T12:08:29+00:00Added an answer on June 14, 2026 at 12:08 pm

    I don’t know what you’re trying to do but this problem has been described so many time on SO it’s not funny anymore.

    Let’s paint a picture:

    you start off with

    dt = T / nsteps;
    

    which is my first red flag.

    then you do:

    if t==T
    

    where t = t + dt

    Why is that wrong? Because of this wonderful thing called quantization. In other words there will be a time when because of super small micro difference, the result will not be correct. The bigger nsteps is, the worse this will be.

    And then to add insult to injury, you put this line in

    elseif t<T ... end
    

    which means your code will skip everything, and you’ll return nothing, causing your code to crash.

    How does one solve this? If you can move integers around instead of floating values, you’ll be in a better position. So instead of dt, you can have currentStep, such that:

    function [y]=AmericanPutClassic (St, currentStep)
    if nargin < 2
        currentStep = 0;
    end
    ...
    if currentStep>=nsteps % if t==T
    ...
    else
    ...
        upPrice=AmericanPutClassic(St*u,currentStep+1);
    ...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function sendSms($toPhone,$message){ $toPhone=intval(trim($toPhone)); if(strlen($toPhone)== 8 && $toPhone{0}==9){ //sending sms }else{ return error; } }
function validate(form) { while ($('#img').attr('src')==../static/img/placeholder.png) { return false; } return true; } ^This code
function SanitizeInput(input) { input = input.replace(/</g, &lt;); input = input.replace(/>/g, &gt;); return input; }
function build_list($id=0,$collapsed=) //return an array with the categories ordered by position { $RootPos =
function MyFunc: Boolean; begin if eval then Result := True else Result := False;
function get_IfUnsetAlsoSet_SomeGlobalVariable() { if someGlobalVariable is not set { someGlobalVariable = somedata } return
function url_exists($url) { $return = false; $content=@file_get_contents($url, FALSE, NULL, 0, 20); if($content == false
function f() { var b = b; return function() { alert(b); return b; };
function getValues($string, $tagname) { $pattern = /<$tagname ?.*>([^<>]*)<\/$tagname>/; preg_match_all($pattern, $string, $matches, PREG_PATTERN_ORDER); var_dump($matches); return
Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift(); return

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.