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

1 回表示 (過去 30 日間)
Iana Ladygina
Iana Ladygina 2019 年 9 月 24 日
コメント済み: Iana Ladygina 2019 年 9 月 25 日
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 日
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 件のコメント
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)
Iana Ladygina
Iana Ladygina 2019 年 9 月 25 日
Thank you Jon for your comments and your assistance! Now it's clear what mistakes were done before :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by