{"id":4438,"date":"2013-03-22T09:01:00","date_gmt":"2013-03-22T13:01:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4438"},"modified":"2013-12-05T14:00:41","modified_gmt":"2013-12-05T19:00:41","slug":"all-purpose-mortgage-calculator-including-mortgage-schedule","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/03\/22\/all-purpose-mortgage-calculator-including-mortgage-schedule\/","title":{"rendered":"All Purpose Mortgage Calculator including mortgage schedule"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/3208495-sean-de-wolski\">Sean's<\/a> pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/8009-all-purpose-mortgage-calculator-including-mortgage-schedule\">All Purpose Mortgage Calculator including mortgage schedule<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/17355\">S B<\/a>.\r\n      <\/p>\r\n      <p>I am currently in the <i>fun<\/i> process of buying a house.  My wife and I were going over the mortgage calculations the other day and figuring out our approximate\r\n         monthly payments and the amount of money we want to put down.  We know that the financial company we are using probably knows\r\n         how to do these calculations.  But we wanted to check them!  I also noticed that they were using Excel so that made me curious\r\n         to see how easy this would be in my choice software for <b>everything<\/b>, MATLAB.\r\n      <\/p>\r\n      <p>It's generally the case that if functionality for MATLAB seems obvious, it's probably built in to the product or one of the\r\n         toolboxes.  If this is not the case, then someone else has likely already written it and deposited it on the File Exchange.\r\n          I vaguely remembered this App from a few months ago when my friend <a href=\"https:\/\/blogs.mathworks.com\/videos\/\">Doug<\/a> and I were talking about the great low interests rates available right now.\r\n      <\/p>\r\n      <p><b>NOTE<\/b> <i>All numbers below have been obfuscated to hide the actual financial details.<\/i><\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Apps<\/a><\/li>\r\n         <li><a href=\"#2\">This App<\/a><\/li>\r\n         <li><a href=\"#3\">The Code<\/a><\/li>\r\n         <li><a href=\"#5\">Some Suggested Future Enhancements<\/a><\/li>\r\n         <li><a href=\"#6\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Apps<a name=\"1\"><\/a><\/h3>\r\n   <p>In MATLAB R2012b, we introduced <a href=\"\">MATLAB Apps<\/a> and the Apps Toolbar:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/appsTab.PNG\"> <\/p>\r\n   <p>Using MATLAB Apps you can package your GUIs and their dependencies into single files that can be distributed and installed\r\n      easily.  This will help your users avoid the standard path issues.  Apps also enable you to have easy version control and\r\n      descriptions for your GUIs.\r\n   <\/p>\r\n   <p><b>Side Note:<\/b>  I know that during the R2012b release many of you wanted to be able to change the App icon.  In R2013a, the ability to select\r\n      an icon from one of a dozen is available.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/appsicons.PNG\"> <\/p>\r\n   <h3>This App<a name=\"2\"><\/a><\/h3>\r\n   <p>So let's take a look at this App:<\/p>\r\n   <p>We'll first try 20% down:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/app1.PNG\"> <\/p>\r\n   <p>How does that compare to 5% down (not including Private Mortgage Insurance (PMI)):<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/app2.PNG\"> <\/p>\r\n   <p>So the total interest saved by putting down 20% instead of 5% is about $28K, not including PMI.<\/p>\r\n   <p>What if we could shorten it to 15 years?<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/app3.PNG\"> <\/p>\r\n   <p>This just about halves the amount of interest!<\/p>\r\n   <p>Unfortunately, the plot does not work for any number of years less than 30 (enhancement!)<\/p>\r\n   <p>For every scenario, the numbers lined up with those that the financial firm gave us verifying both their calculations and\r\n      this App.\r\n   <\/p>\r\n   <h3>The Code<a name=\"3\"><\/a><\/h3>\r\n   <p>Though we have a big suite of Financial Products, none of them were used here.  The engine behind this appears to be about\r\n      20 lines of MATLAB code and everything else is just for the GUI.\r\n   <\/p>\r\n   <p>One thing I noticed is that S B did a lot of work for selecting the colors for the plot lines. Here's what he did:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">color_map = [<span style=\"color: #A020F0\">'rbgrbmbrgr'<\/span>];\r\nz1 = 0;\r\nz2 = 0;\r\n\r\n<span style=\"color: #0000FF\">while<\/span> (z1 == 0) | (z1 &gt; 9) | (z2 == 0) | (z2 &gt; 9)\r\n\r\n    z1 = fix(rand(1)*10);\r\n    z2 = fix(rand(1)*10);\r\n\r\n    <span style=\"color: #228B22\">% What are the chances of get the same random number twice!?<\/span>\r\n    <span style=\"color: #0000FF\">if<\/span> (z1 == z2)\r\n        z1 = fix(rand(1)*10);\r\n        z2 = fix(rand(1)*10);\r\n    <span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">end<\/span>\r\nz1 = color_map(z1);\r\nz2 = color_map(z2);<\/pre><p>I would recommend just using <a href=\"\"><tt>randperm()<\/tt><\/a> to guarantee two unique integers between one and nine.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">color_map = <span style=\"color: #A020F0\">'rgbcmyk'<\/span>;\r\nidx       = randperm(numel(color_map),2);\r\ncolor1    = color_map(idx(1))\r\ncolor2    = color_map(idx(2))<\/pre><pre style=\"font-style:oblique\">\r\ncolor1 =\r\n\r\nk\r\n\r\n\r\ncolor2 =\r\n\r\nb\r\n\r\n<\/pre><h3>Some Suggested Future Enhancements<a name=\"5\"><\/a><\/h3>\r\n   <p>There are a few enhancements that would make this App even better!<\/p>\r\n   <div>\r\n      <ol>\r\n         <li>The ability for it to plot for a term different than 30 years.<\/li>\r\n         <li>The ability to account for overpayments (e.g. by just changing the   monthly amount).<\/li>\r\n         <li>The ability to account for PMI.<\/li>\r\n      <\/ol>\r\n   <\/div>\r\n   <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>So the answer to my original question: <b>How easy would it be to do this in MATLAB?<\/b> <i>Really easy!<\/i><\/p>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4438#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/8009-all-purpose-mortgage-calculator-including-mortgage-schedule#comments\">comment<\/a> for S B.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_5df4065f140f4e1eb657f0e9a78a8b56() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='5df4065f140f4e1eb657f0e9a78a8b56 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5df4065f140f4e1eb657f0e9a78a8b56';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'guest';\r\n        copyright = 'Copyright 2013 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_5df4065f140f4e1eb657f0e9a78a8b56()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p>\r\n<\/div>\r\n<!--\r\n5df4065f140f4e1eb657f0e9a78a8b56 ##### SOURCE BEGIN #####\r\n%% All Purpose Mortgage Calculator including mortgage schedule\r\n% Sean's pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/8009-all-purpose-mortgage-calculator-including-mortgage-schedule\r\n% All Purpose Mortgage Calculator including mortgage schedule> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/17355 S B>.\r\n%\r\n% I am currently in the _fun_ process of buying a house.  My wife and I\r\n% were going over the mortgage calculations the other day and figuring out\r\n% our approximate monthly payments and the amount of money we want to put\r\n% down.  We know that the financial company we are using probably knows how\r\n% to do these calculations.  But we wanted to check them!  I also noticed\r\n% that they were using Excel so that made me curious to see how easy this\r\n% would be in my choice software for *everything*, MATLAB.\r\n% \r\n% It's generally the case that if functionality for MATLAB seems obvious,\r\n% it's probably built in to the product or one of the toolboxes.  If this\r\n% is not the case, then someone else has likely already written it and\r\n% deposited it on the File Exchange.  I vaguely remembered this App from a\r\n% few months ago when my friend <https:\/\/blogs.mathworks.com\/videos\/ Doug>\r\n% and I were talking about the great low interests rates available right\r\n% now.\r\n%\r\n% *NOTE* _All numbers below have been obfuscated to hide the actual financial\r\n% details._\r\n%\r\n\r\n\r\n%% Apps\r\n% In MATLAB R2012b, we introduced\r\n% <\r\n% MATLAB Apps> and the Apps Toolbar:\r\n%\r\n% <<.\/guest\/main\/APPSTAB.PNG>>\r\n%\r\n% Using MATLAB Apps you can package your GUIs and their dependencies into\r\n% single files that can be distributed and installed easily.  This will\r\n% help your users avoid the standard path issues.  Apps also enable you to\r\n% have easy version control and descriptions for your GUIs.\r\n%\r\n% *Side Note:*  I know that during the R2012b release many of you wanted to\r\n% be able to change the App icon.  In R2013a, the ability to select an icon\r\n% from one of a dozen is available. \r\n%\r\n% <<APPSICONS.PNG>>\r\n%\r\n\r\n%% This.App\r\n% So let's take a look at this App:\r\n%\r\n% We'll first try 20% down:\r\n%\r\n% <<.\/guest\/main\/APP1.PNG>>\r\n%\r\n% How does that compare to 5% down (not including Private Mortgage Insurance (PMI)):\r\n% \r\n% <<APP2.PNG>>\r\n%\r\n% So the total interest saved by putting down 20% instead of 5% is about\r\n% $28K, not including PMI.\r\n%\r\n% What if we could shorten it to 15 years?\r\n%\r\n% <<APP3.PNG>>\r\n%\r\n% This just about halves the amount of interest!\r\n%\r\n% Unfortunately, the plot does not work for any number of years less than\r\n% 30 (enhancement!)\r\n%\r\n% For every scenario, the numbers lined up with those that the financial\r\n% firm gave us verifying both their calculations and this App.\r\n%\r\n\r\n\r\n%% The Code\r\n%\r\n% Though we have a big suite of Financial Products, none of them were used\r\n% here.  The engine behind this appears to be about 20 lines of MATLAB code\r\n% and everything else is just for the GUI.  \r\n%\r\n% One thing I noticed is that S B did a lot of work for selecting the\r\n% colors for the plot lines. Here's what he did:\r\n% \r\n\r\ncolor_map = ['rbgrbmbrgr'];\r\nz1 = 0;\r\nz2 = 0;\r\n\r\nwhile (z1 == 0) | (z1 > 9) | (z2 == 0) | (z2 > 9)\r\n    \r\n    z1 = fix(rand(1)*10);\r\n    z2 = fix(rand(1)*10);\r\n    \r\n    % What are the chances of get the same random number twice!?\r\n    if (z1 == z2)\r\n        z1 = fix(rand(1)*10);\r\n        z2 = fix(rand(1)*10);\r\n    end\r\nend\r\nz1 = color_map(z1);\r\nz2 = color_map(z2);\r\n\r\n%%\r\n%\r\n% I would recommend just using\r\n% <\r\n% |randperm()|> to guarantee two unique integers between one and nine.\r\n%\r\n\r\ncolor_map = 'rgbcmyk';\r\nidx       = randperm(numel(color_map),2);\r\ncolor1    = color_map(idx(1))\r\ncolor2    = color_map(idx(2))\r\n\r\n\r\n\r\n%% Some Suggested Future Enhancements\r\n% There are a few enhancements that would make this App even better!\r\n%\r\n% # The ability for it to plot for a term different than 30 years.\r\n% # The ability to account for overpayments (e.g. by just changing the\r\n%   monthly amount).\r\n% # The ability to account for PMI.\r\n%\r\n\r\n%% Comments\r\n%\r\n% So the answer to my original question: *How easy would it be to do this\r\n% in MATLAB?* _Really easy_\r\n% \r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4438#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/8009-all-purpose-mortgage-calculator-including-mortgage-schedule#comments\r\n% comment> for S B.\r\n\r\n\r\n\r\n##### SOURCE END ##### 5df4065f140f4e1eb657f0e9a78a8b56\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/guest\/main\/appsTab.PNG\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is All Purpose Mortgage Calculator including mortgage schedule by S B.\r\n      \r\n      I am currently in the fun process of buying a house.  My wife... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/03\/22\/all-purpose-mortgage-calculator-including-mortgage-schedule\/\">read more >><\/a><\/p>","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4438"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4438"}],"version-history":[{"count":21,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4438\/revisions"}],"predecessor-version":[{"id":4951,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4438\/revisions\/4951"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}