Azzera filtri
Azzera filtri

unable to plot a function graph

1 visualizzazione (ultimi 30 giorni)
Odien
Odien il 9 Ago 2015
Commentato: Star Strider il 9 Ago 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 Commento
Odien
Odien il 9 Ago 2015
Modificato: Star Strider il 9 Ago 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

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by