unable to plot a function graph

1 view (last 30 days)
Odien
Odien on 9 Aug 2015
Commented: Star Strider on 9 Aug 2015
this is the question. s = p(1+i)^n, Write a program that will plot the amount S as it increases through the years from 1 to n. The main script will call a function to prompt the user for the number of years (and error-check to make sure that the user enters a positive integer). The script will then call a function that win plot S for years 1 through n. It will use 0.05 for the i and $10,000 for P. my code(main script)
x = input('Please enter the number of year to cal the lumpsumS : ');
if(x > 0)
mylumpsum(x);
else
disp('Please enter again,the # of year has to be positive !')
end
sub script
function mylumpsum(x)
prinp = 10000;
itr = 0.05;
for i = 1:x;
y = prinp*((1+itr)^(i));
xaxis = 0 : 1 : x;
plot(xaxis,y,'-r')
xlabel('x-axis');
ylabel('y-axis');
title('The graph of lump sum S');
end
end
it was not working, can anyone identify the problems?
  1 Comment
Odien
Odien on 9 Aug 2015
Edited: Star Strider on 9 Aug 2015
function mylumpsum(x)
prinp = 10000;
itr = 0.05;
for i = 1:x;
y = prinp*((1+itr)^(i));
xaxis = 0 : 1 : x;
plot(xaxis,y,'-r')
xlabel('x-axis');
ylabel('y-axis');
title('The graph of lump sum S');
end
end

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 9 Aug 2015
Some slight changes in part of your code to make it work:
prinp = 10000;
itr = 0.05;
for i = 1:x;
y(i) = prinp*((1+itr)^(i)); % Save ‘y’ In Every Step
end
xaxis = 0 : 1 : x-1; % Since ‘x’ Begins At ‘1’, ‘xaxis’ Has To Go To ‘x-1’ To Be The Same Length As ‘x’
plot(xaxis,y,'-r') % Put The Plot After The Loop
xlabel('x-axis');
ylabel('y-axis');
title('The graph of lump sum S');
  2 Comments
Odien
Odien on 9 Aug 2015
its work! thank you for the correction!
Star Strider
Star Strider on 9 Aug 2015
My pleasure!
It’s all your code — I just rearranged it a bit.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!