What should I do in order to make my code plot like this?
2 views (last 30 days)
Show older comments
sap = input('Select a program: 1. Arts; 2. Science; 3. Engineering: ');
nb(1) = 2000;
mir = 0.0625/12;
mc = 200;
for B = 2 : 216
nb(B) = (nb(B-1)+1) + ((nb(B-1)+1) * mir) + mc;
end
fprintf('At the end of 18 years you will have saved $ %f \n',nb(B))
if sap == 1
os1(1) = 5550;
air = 0.07;
for A = 2 : 22
os1(A) = (os1(A-1) + 1) + ((os1(A-1) + 1) * air);
end
x1 = os1(19)+ os1(20) + os1(21) + os1(22);
fprintf('The cose of a 4-year college tuition fee is $ %f\n',x1)
if nb(216) > x1
disp('Congratulations!!!! You have saved enough.')
elseif nb(216) < x1
s1 = x1 - nb(216);
fprintf('The saving is $ %f\n',s1)
end
end
I need the year be exactly 0 to 18 years, so I can compare two lines like this.
I tried to use
time = 1:18
plot(time,nb(1:216),time,os1(1:22))
But this will cause erroe message 'Vector must be the same length.'
Can someone please help me with this?
0 Comments
Answers (1)
Mathieu NOE
on 12 Oct 2020
hi
this is the modified code
sap = input('Select a program: 1. Arts; 2. Science; 3. Engineering: ');
nb(1) = 2000;
mir = 0.0625/12;
mc = 200;
for B = 2 : 216 % 216 = nb of monthes for 18 years
nb(B) = (nb(B-1)+1) + ((nb(B-1)+1) * mir) + mc;
end
fprintf('At the end of 18 years you will have saved $ %f \n',nb(B))
if sap == 1
os1(1) = 5550;
air = 0.07;
for A = 2 : 22
os1(A) = (os1(A-1) + 1) + ((os1(A-1) + 1) * air);
end
x1 = os1(19)+ os1(20) + os1(21) + os1(22);
fprintf('The cose of a 4-year college tuition fee is $ %f\n',x1)
if nb(216) > x1
disp('Congratulations!!!! You have saved enough.')
elseif nb(216) < x1
s1 = x1 - nb(216);
fprintf('The saving is $ %f\n',s1)
end
% plot
x_axis_years = 1:18; % define x axis
ind_nb_years = (1:18)*12;
nb_years = nb(ind_nb_years);
plot(x_axis_years,nb_years,'-+b',x_axis_years,x1*ones(1,length(x_axis_years)),'r'); grid on
title('my Title');
xlabel('Years');
end
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!