Functions and Plotting in GUI

1 次查看(过去 30 天)
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);

采纳的回答

Geoff Hayes
Geoff Hayes 2016-3-24
编辑:Geoff Hayes 2016-3-24
Jovos - part of the problem is with the "units" of your interest rate. In your example, you state that the interest is 10. Presumably this is ten percent. If this is the case, then you must convert this to a decimal prior to using this value in your calculations.
annualInterestAsDecimal = str2num(get(handles.interest_rate,'String'))/100;
With the above change, and using the same inputs as you describe, the output is the same as that found at http://www.calculatorsoup.com/calculators/financial/car-loan-calculator.php.
Note that it is good practice to avoid using i (and j) in naming variables as MATLAB uses both to represent the imaginary number.
As for your remaining balance equation, what have you based it on?
  2 个评论
Jovos
Jovos 2016-3-24
编辑:Geoff Hayes 2016-3-25
Thank you for solving the interest error!! For the remaining balance equation, it is based on the initial amount of loan and a given time t at any period. I basically used the equation for wiki. https://en.wikipedia.org/wiki/Continuous-repayment_mortgage
By the way, how do I add a dollar sign before the calculated results? For now it only shows a number? Thanks again.
Geoff Hayes
Geoff Hayes 2016-3-25
编辑:Geoff Hayes 2016-3-25
Jovos - the equation (that I think that you are referencing from the above link) assumes that its input is in years, P(t), where t is in years. You are passing in months (the array x) and so need to ensure that you are using the interest rate per month. Try switching the equation to
remaining_balance = p * (1 - exp(-r*(N-x)))/(1-exp(-r*N));
and observe how it is drawn on your figure.
As for including a dollar sign, just do
set(handles.monthly_payment,'String',sprintf('$%.2f', payment));
set(handles.total_payment,'String',sprintf('$%.2f', total));
We use sprintf to create a string that has been properly formatted (to two decimal places).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by