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

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))])

 채택된 답변

Jon
Jon 2019년 9월 24일
편집: Jon 2019년 9월 24일

0 개 추천

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

Hi Jon! Thank you for your response. When I add
if balanceV(n) < 10000
instead of
if balanceV < 10000
the answer somehow changes to the wrong one. I also added
period = 2:31;
plot(period,balanceV,'r')
however, the plot still shows nothing, just emply
Jon
Jon 2019년 9월 24일
편집: Jon 2019년 9월 24일
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)
Thank you Jon for your comments and your assistance! Now it's clear what mistakes were done before :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2019년 9월 24일

댓글:

2019년 9월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by