Borrar filtros
Borrar filtros

unable to plot a function graph

1 visualización (últimos 30 días)
Odien
Odien el 9 de Ag. de 2015
Comentada: Star Strider el 9 de Ag. de 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 comentario
Odien
Odien el 9 de Ag. de 2015
Editada: Star Strider el 9 de Ag. de 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

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by