Help needed with a matlab code - end missing.

26 views (last 30 days)
Monika Morawska
Monika Morawska on 29 Jan 2019
Commented: Rik on 26 May 2023
I wanted to use the code from a paper to calculate the marginal tax rate. Tried using it this is the main error that pops up: "159 An END might be missing, possibly matching FUNCTION". Could somoene help me fix this?
The code is from Sinha, Pankaj & Bansal, Vishakha. (2012). Algorithm for calculating corporate marginal tax rate using Monte Carlo simulation.
Here is the code:
function varargout = program(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @program_OpeningFcn, ...
'gui_OutputFcn', @program_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
function program_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = program_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
function edit2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton1_Callback(hObject, eventdata, handles)
J=get(handles.edit1,'String');
J=str2double(J);
Y=get(handles.edit2,'String');
Y=str2double(Y);
[num] = xlsread('income.xlsx');
p=num(1,:);
Ntemp=length(p);
N=Ntemp+2*J;
p=diff(p);
mu=mean(p);
sig=std(p);
[num] = xlsread('tr.xlsx');
tr=num(1,:);
temp=tr(Ntemp)*ones(1,2*J);
tr=[tr temp];
[num] = xlsread('amt.xlsx');
amt=num(1,:);
temp=amt(Ntemp)*ones(1,2*J);
amt=[amt temp];
[num] = xlsread('rr.xlsx');
rr=num(1,:);
temp=rr(Ntemp)*ones(1,2*J);
rr=[rr temp];
p=zeros(1,N);
mtr=zeros(1,N-J);
sum=zeros(1,N-J);
for z=1:10000
p=randn(1,N);
p=mu+sig*p;
p=cumsum(p);
n=b(p,tr,amt,J,N);
for i=1:N-J
pnew=p;
pnew(i)=pnew(i)+1;
nnew=b(pnew,tr,amt,J,N);
npv=n(i);
nnewpv=nnew(i);
for j=i+1:N
r=1;
for k=i:(j-1)
r=r/(1+rr(k)/100);
end
npv=npv+n(j)*r;
nnewpv=nnewpv+nnew(j)*r;
end
mtr(i)=(nnewpv-npv)*100;
end
sum=sum+mtr;
end
sum=sum/10000;
years=linspace(Y,Y+N-J-1,N-J);
xlswrite('output_algo1.xlsx',years,'A1:CV1');
xlswrite('output_algo1.xlsx',sum,'A2:CV2');
function pushbutton2_Callback(hObject, eventdata, handles)
J=get(handles.edit1,'String');
J=str2double(J);
Y=get(handles.edit2,'String');
Y=str2double(Y);
[num] = xlsread('income.xlsx');
p=num(1,:);
Ntemp=length(p);
N=Ntemp+2*J;
pp=diff(p);
mu=mean(pp);
sig=std(pp);
[num] = xlsread('tr.xlsx');
tr=num(1,:);
temp=tr(Ntemp)*ones(1,2*J);
tr=[tr temp];
[num] = xlsread('amt.xlsx');
amt=num(1,:);
temp=amt(Ntemp)*ones(1,2*J);
amt=[amt temp];
[num] = xlsread('rr.xlsx');
rr=num(1,:);
temp=rr(Ntemp)*ones(1,2*J);
rr=[rr temp];
pp=zeros(1,N);
mtr=zeros(1,N-J);
for i=1:N-J
for z=1:10000
pp=randn(1,N);
pp=mu+sig*pp;
pp=cumsum(pp);
if(i<=Ntemp)
pp=[p(1:i) pp((i+1):N)];
end
n=b(pp,tr,amt,J,N);
pp(i)=pp(i)+1;
nnew=b(pp,tr,amt,J,N);
npv=n(i);
nnewpv=nnew(i);
for j=i+1:N
r=1;
for k=i:(j-1)
r=r/(1+rr(k)/100);
end
npv=npv+n(j)*r;
nnewpv=nnewpv+nnew(j)*r;
end
mtr(i)=mtr(i)+(nnewpv-npv)*100;
end
end
mtr=mtr/10000;
years=linspace(Y,Y+N-J-1,N-J);
xlswrite('output_algo2.xlsx',years,'A1:CV1');
xlswrite('output_algo2.xlsx',mtr,'A2:CV2');
function edit3_Callback(hObject, eventdata, handles)
function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton3_Callback(hObject, eventdata, handles)
J=get(handles.edit1,'String');
J=str2double(J);
Y=get(handles.edit2,'String');
Y=str2double(Y);
JJ=get(handles.edit3,'String');
JJ=str2double(JJ);
[num] = xlsread('income.xlsx');
p=num(1,:);
Ntemp=length(p);
N=Ntemp+2*J;
p=diff(p);
mu=mean(p);
sig=std(p);
[num] = xlsread('tr.xlsx');
tr=num(1,:);
temp=tr(Ntemp)*ones(1,2*J);
tr=[tr temp];
[num] = xlsread('amt.xlsx');
amt=num(1,:);
temp=amt(Ntemp)*ones(1,2*J);
amt=[amt temp];
[num] = xlsread('rr.xlsx');
rr=num(1,:);
temp=rr(Ntemp)*ones(1,2*J);
rr=[rr temp];
p=zeros(1,N);
mtr=zeros(1,N-J);
sum=zeros(1,N-J);
for z=1:10000
p=randn(1,N);
p=mu+sig*p;
p=cumsum(p);
n=bb(p,tr,amt,J,N,JJ);
for i=1:N-J
pnew=p;
pnew(i)=pnew(i)+1;
nnew=bb(pnew,tr,amt,J,N,JJ);
npv=n(i);
nnewpv=nnew(i);
for j=i+1:N
r=1;
for k=i:(j-1)
r=r/(1+rr(k)/100);
end
npv=npv+n(j)*r;
nnewpv=nnewpv+nnew(j)*r;
end
mtr(i)=(nnewpv-npv)*100;
end
sum=sum+mtr;
end
sum=sum/10000;
years=linspace(Y,Y+N-J-1,N-J);
xlswrite('output_carryback_algo1.xlsx',years,'A1:CV1');
xlswrite('output_carryback_algo1.xlsx',sum,'A2:CV2');
function pushbutton4_Callback(hObject, eventdata, handles)
J=get(handles.edit1,'String');
J=str2double(J);
Y=get(handles.edit2,'String');
Y=str2double(Y);
JJ=get(handles.edit3,'String');
JJ=str2double(JJ);
[num] = xlsread('income.xlsx');
p=num(1,:);
Ntemp=length(p);
N=Ntemp+2*J;
pp=diff(p);
mu=mean(pp);
sig=std(pp);
[num] = xlsread('tr.xlsx');
tr=num(1,:);
temp=tr(Ntemp)*ones(1,2*J);
tr=[tr temp];
[num] = xlsread('amt.xlsx');
amt=num(1,:);
temp=amt(Ntemp)*ones(1,2*J);
amt=[amt temp];
[num] = xlsread('rr.xlsx');
rr=num(1,:);
temp=rr(Ntemp)*ones(1,2*J);
rr=[rr temp];
pp=zeros(1,N);
mtr=zeros(1,N-J);
for i=1:N-J
for z=1:10000
pp=randn(1,N);
pp=mu+sig*pp;
pp=cumsum(pp);
if(i<=Ntemp)
pp=[p(1:i) pp((i+1):N)];
end
n=bb(pp,tr,amt,J,N,JJ);
pp(i)=pp(i)+1;
nnew=bb(pp,tr,amt,J,N,JJ);
npv=n(i);
nnewpv=nnew(i);
for j=i+1:N
r=1;
for k=i:(j-1)
r=r/(1+rr(k)/100);
end
npv=npv+n(j)*r;
nnewpv=nnewpv+nnew(j)*r;
end
mtr(i)=mtr(i)+(nnewpv-npv)*100;
end
end
end
mtr=mtr/10000;
years=linspace(Y,Y+N-J-1,N-J);
xlswrite('output_carryback_algo2.xlsx',years,'A1:CV1');
xlswrite('output_carryback_algo2.xlsx',mtr,'A2:CV2');
Function for calculating tax with only carry forward losses :
function [n] = b(p,tr,amt,J,N)
v=p;
c=zeros(1,J);
l=zeros(1,N);
for i=1:N
for j=J:-1:1
c(j+1)=c(j);
end
c(1)=0;
if(v(i)<=0)
c(1)=v(i);
v(i)=0;
else
for j=J+1:-1:1
if(c(j)<0)
if(-c(j)>=v(i))
c(j)=c(j)+v(i);
if(p(i)>0)
l(i)=p(i);
end
v(i)=0;
else
v(i)=c(j)+v(i);
c(j)=0;
end
end
end
end
end
for i=1:N
if(l(i)==0 && p(i)>0)
l(i)=p(i);
end
end
v=(v.*tr)/100;
l=(l.*amt)/100;
n=max(v,l);
end
Function for calculating tax with both carry forward & backward losses :
function [n] = bb(p,tr,amt,J,N,JJ)
v=p;
c=zeros(1,J);
l=zeros(1,N);
bck=zeros(1,JJ);
for i=1:N
if(v(i)<0)
for j=1:JJ
if(-v(i)>bck(j))
v(i)=v(i)+bck(j);
bck(j)=0;
else
bck(j)=bck(j)+v(i);
v(i)=0;
end
end
end
for j=J:-1:1
c(j+1)=c(j);
end
c(1)=0;
if(v(i)<=0)
c(1)=v(i);
v(i)=0;
else
for j=J+1:-1:1
if(-c(j)>=v(i))
c(j)=c(j)+v(i);
if(p(i)>0)
l(i)=p(i);
end
v(i)=0;
else
v(i)=c(j)+v(i);
c(j)=0;
end
end
end
for j=1:(JJ-1)
bck(j)=bck(j+1);
end
if(l(i)==0 && p(i)>0)
l(i)=p(i);
end
v(i)=v(i)*tr(i)/100;
l(i)=l(i)*amt(i)/100;
n(i)=max(v(i),l(i));
if(v(i)==n(i))
bck(JJ)=100*v(i)/tr(i);
else
bck(JJ)=0;
end
end
end
  1 Comment
Kevin Phung
Kevin Phung on 29 Jan 2019
Edited: Kevin Phung on 29 Jan 2019
'an END might be missing'
I would check my ends..or since you arent ending your functions, check to see if youve accidentally ended one of them. I believe you need to be consistent
also please dont post your entire 1000 lined function but instead attach them as files.

Sign in to comment.

Answers (1)

Luna
Luna on 29 Jan 2019
I haven't seen such a badly written code ever since in my life. How they put that in a paper?
Zero comments, too many nested functions and nested for loops. First it must needed to be understood that if b and bb functions are written nested in a purpose.
Also program.fig does not exist how do you expect to run this app?
I have deleted 3 ends in some lines in the new code attached. There are no global variables anymore.
But you still again need to check it if it works.
  2 Comments
Bismark
Bismark on 26 May 2023
@Luna can please share your email with me? i need assistance to with my code please
Rik
Rik on 26 May 2023
@Bismark, many people do not wish to sent emails, as that makes them an unpaid private consultant. You are generally much better off posting your question. Have a read here and here. It will greatly improve your chances of getting an answer.

Sign in to comment.

Categories

Find more on Web Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!