why my graph doesnt start at 0, and how to fix it?

10 ビュー (過去 30 日間)
Marco Lung
Marco Lung 2019 年 3 月 3 日
回答済み: Cris LaPierre 2019 年 3 月 4 日
old_balance = 2000;
monthly_contri = 200;
monthly_interest = 0.06/12;
new_balance = zeros(1,216);
new_balance(1) = old_balance;
yearly_balance = zeros(1,18);
month=0;
for i=1:216
new_balance(i+1) = new_balance(i) + (new_balance(i) * monthly_interest) + monthly_contri;
end
total_savings = new_balance(216);
for i=1:18
month = month+12;
yearly_balance(i)= new_balance(month);
end
yearly_tution = zeros(1,18);
tut_rate = 0.0575;
prog_selc = input('Select a program: 1. Arts; 2. Science; 3.Engineering: ');
switch prog_selc
case 1
art(1) = 6000;
for i = 1:22
art(i+1) = art(i) + (art(i) * tut_rate);
end
tution = sum(art(19:22));
for i = 1:18
yearly_tution(i) = tution;
end
case 2
sci(1) = 6500;
for i = 1:22
sci(i+1) = sci(i) + (sci(i) * tut_rate);
end
tution = sum(sci(19:22));
for i=1:18
yearly_tution(i) = tution;
end
case 3
engg(1) = 7000;
for i = 1:22
engg(i+1)= engg(i) + (engg(i) * tut_rate);
end
tution = sum(engg(19:22));
for i = 1:18
yearly_tution(i) = tution;
end
end
fprintf('At the end of 18 years you will have saved $ %6.2f\n',total_savings);
fprintf('The cost of a 4-year college tution fee is $ %6.2f\n',tution);
if total_savings >= tution
disp('Congratulations!!!! You have saved enough.');
elseif tution > total_savings
short = tution - total_savings;
fprintf('The saving is $ %6.2f short.\n',short);
end
years=1:18;
plot(years,yearly_tution,'-b',years,yearly_balance,'-g')
title('College Savings vs. Tution fee')
xlabel('Year')
ylabel('Balance')

回答 (1 件)

Cris LaPierre
Cris LaPierre 2019 年 3 月 4 日
Which axis is supposed to start at 0? X or Y?
X is years. That does not start at 0 because you define it to start at 1:
years=1:18;
For the blue line, Y is yearly_tuition. You define a non-zero value for art(1), sci(1) and engg(1). You then add to it in the for loop, and then set every value of yearly_tuition equal to the sum of values 19-22.
For the green line, Y is the yearly_balance. You assign in a value from new_balance. The first value of new_balance is old_balance (2000), and then you add to it in the first for loop.
old_balance = 2000;
...
new_balance(1) = old_balance;

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by