How can I plot these values (result vs. time)?

1 visualización (últimos 30 días)
Iana Ladygina
Iana Ladygina el 24 de Sept. de 2019
Comentada: Iana Ladygina el 25 de Sept. de 2019
I have written the code, however, the plot function doesn't work. Do you know what could be wrong?
clc
clear all
close all
format bank
balanceV = 2000;
IR1 = 0.07;
IR2 = 0.09;
for n = 2:31
if balanceV < 10000
balanceV(n) = balanceV(n-1)*(1+IR1)
else
balanceV(n) = balanceV(n-1)*(1+IR2)
end
end
figure
plot(n,balanceV,'r')
xlim([0 30])
ylim([0 ceil(balanceV(n))])

Respuesta aceptada

Jon
Jon el 24 de Sept. de 2019
Editada: Jon el 24 de Sept. de 2019
Your call to plot would just use the last value of n from the for loop as the x variable. Instead use something like
period = 2:31;
plot(period,balanceV,'r')
It looks like you also have another problem with your if statement
if balanceV < 10000
this will have the same value for every loop iteration.
I think you probably want to have
if balanceV(n) < 10000
It would be clearer if you initialized the first element of balance using
balanceV(1) = 2000;
so you could see that balanceV was going to be a vector of values.
Even better practice, for memory management and to be sure that you are not using any old values if you rerun the script without always clearing the memory would be to preallocate the array
% preallocate array of zeros to hold the running balance
balanceV = zeros(32,1)
% initialize running balance
balanceV(1) = 2000;
If you paste more code into future questions you can use the code button on the Answers toolbar to insert code and have it be nicely formatted.
  3 comentarios
Jon
Jon el 24 de Sept. de 2019
Editada: Jon el 24 de Sept. de 2019
Two problems are happening.
First, I realized you must infact preallocate (initialize) the balanceV array. Otherwise on the first pass through the loop, n = 2 and there is no balanceV(2) yet, so you get an error.
Second, sorry I didn't look closely enough at your code, you need to define a vector of "x" values which goes from 1 to 32, with my previous suggestion it went from 2 to 32
I tried putting both of these fixes for example as shown below and found that it ran without errors
clc
clear all
close all
format bank
% preallocate array to hold running balance
balanceV = zeros(31,1);
balanceV(1) = 2000;
IR1 = 0.07;
IR2 = 0.09;
for n = 2:31
if balanceV(n) < 10000
balanceV(n) = balanceV(n-1)*(1+IR1)
else
balanceV(n) = balanceV(n-1)*(1+IR2)
end
end
figure
period = 1:31
plot(period,balanceV,'r')
xlim([0 30])
ylim([0 ceil(balanceV(n))])
Finally, you set your xlim to a max of 30. Do you really want to plot only up to 30 when n goes to 31?
If you want to set your maximum y value in the plot based on the final balance then it would be better to use
ylim = ([0 ceil(balanceV(end))])
using the last value of the loop counter, makes harder to read, you have to realize that n will be the final value of n = 2:31, its better just to specify the last element in the array if that's what you want so balanceV(end)
Iana Ladygina
Iana Ladygina el 25 de Sept. de 2019
Thank you Jon for your comments and your assistance! Now it's clear what mistakes were done before :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by