unable to plot a function graph

조회 수: 7 (최근 30일)
Odien
Odien 2015년 8월 9일
댓글: Star Strider 2015년 8월 9일
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
Odien
Odien 2015년 8월 9일
편집: Star Strider 2015년 8월 9일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2015년 8월 9일
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
Odien
Odien 2015년 8월 9일
its work! thank you for the correction!
Star Strider
Star Strider 2015년 8월 9일
My pleasure!
It’s all your code — I just rearranged it a bit.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by