Hi, I am trying to make a mortgage calculator. I used the guide to build. There are two problem. First Question: When I enter the data for, example: amount = 10,000 interest = 10 and years = 30. It does not return me the correct value comparing the calculator by google. Second Question: For the remaining balance plotting, it only shows one horizontal line. I really can't find any problem with that function.
The main part of the code is here in the push botton function:
function cal_Callback(hObject, eventdata, handles) % hObject handle to cal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
p = str2num(get(handles.loan_amount,'String')); i = str2num(get(handles.interest_rate,'String')); T = str2num(get(handles.years,'String')); r = i / 12; % monthly interest rate;
N = T * 12; % terms of borrowing
payment = (r*p*(1+r)^N)/((1+r)^N-1); total = payment * N; set(handles.monthly_payment,'String',payment); set(handles.total_payment,'String',total); %Plotting the total balance
x = 0:1:N; y = payment * x; plot(x,y,'Color','red'); legend('Total'); hold on remaining_balance = p * (1- exp(-i*(N-x)))/(1-exp(-i*N)); plot(x,remaining_balance,'Color','blue'); legend('Balance');
Here is the whole code.
function varargout = MortageCalculator(varargin) % MORTAGECALCULATOR MATLAB code for MortageCalculator.fig
% MORTAGECALCULATOR, by itself, creates a new MORTAGECALCULATOR or raises the existing
% singleton*.
%
% H = MORTAGECALCULATOR returns the handle to a new MORTAGECALCULATOR or the handle to
% the existing singleton*.
% % MORTAGECALCULATOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORTAGECALCULATOR.M with the given input arguments.
% % MORTAGECALCULATOR('Property','Value',...) creates a new MORTAGECALCULATOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MortageCalculator_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MortageCalculator_OpeningFcn via varargin.
% % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
% % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help MortageCalculator
% Last Modified by GUIDE v2.5 23-Mar-2016 21:04:26
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @MortageCalculator_OpeningFcn, ... 'gui_OutputFcn', @MortageCalculator_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before MortageCalculator is made visible.
function MortageCalculator_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to MortageCalculator (see VARARGIN)
% Choose default command line output for MortageCalculator
handles.output = hObject; % Update handles structure
guidata(hObject, handles); % UIWAIT makes MortageCalculator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = MortageCalculator_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure
varargout{1} = handles.output; function loan_amount_Callback(hObject, eventdata, handles) % hObject handle to loan_amount (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of loan_amount as text
% str2double(get(hObject,'String')) returns contents of loan_amount as a double
% --- Executes during object creation, after setting all properties.
function loan_amount_CreateFcn(hObject, eventdata, handles) % hObject handle to loan_amount (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function interest_rate_Callback(hObject, eventdata, handles) % hObject handle to interest_rate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of interest_rate as text
% str2double(get(hObject,'String')) returns contents of interest_rate as a double
% --- Executes during object creation, after setting all properties.
function interest_rate_CreateFcn(hObject, eventdata, handles) % hObject handle to interest_rate (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end function years_Callback(hObject, eventdata, handles) % hObject handle to years (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of years as text
% str2double(get(hObject,'String')) returns contents of years as a double
% --- Executes during object creation, after setting all properties. function years_CreateFcn(hObject, eventdata, handles) % hObject handle to years (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in cal.
function cal_Callback(hObject, eventdata, handles) % hObject handle to cal (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) p = str2num(get(handles.loan_amount,'String')); i = str2num(get(handles.interest_rate,'String')); T = str2num(get(handles.years,'String')); r = i / 12; % monthly interest rate; N = T * 12; % terms of borrowing payment = (r*p*(1+r)^N)/((1+r)^N-1); total = payment * N; set(handles.monthly_payment,'String',payment); set(handles.total_payment,'String',total); %Plotting the total balance x = 0:1:N; y = payment * x; plot(x,y,'Color','red'); legend('Total'); remaining_balance = p * (1- exp(-i*(N-x)))/(1-exp(-i*N)); plot(x,remaining_balance);
Best Answer