Filter löschen
Filter löschen

unable to plot a function graph

1 Ansicht (letzte 30 Tage)
Odien
Odien am 9 Aug. 2015
Kommentiert: Star Strider am 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 Kommentar
Odien
Odien am 9 Aug. 2015
Bearbeitet: Star Strider am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by